From c1b27579ca791d3728f211c862448c4398f3e44f Mon Sep 17 00:00:00 2001 From: Daniel Ang Date: Fri, 18 Jun 2021 12:43:17 +0200 Subject: [PATCH] Method json() to parse JSON strings - Parse a JSON string into array - Test --- src/Expectation.php | 11 +++++++++++ tests/Features/Expect/json.php | 14 ++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/Features/Expect/json.php diff --git a/src/Expectation.php b/src/Expectation.php index 45b52891..3c238805 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -60,6 +60,17 @@ final class Expectation return new self($value); } + /** + * Parses Json String to Array. + */ + public function json(): Expectation + { + Assert::assertIsString($this->value); + Assert::assertJson($this->value); + + return new self(json_decode($this->value, true)); + } + /** * Dump the expectation value and end the script. * diff --git a/tests/Features/Expect/json.php b/tests/Features/Expect/json.php new file mode 100644 index 00000000..2228fd56 --- /dev/null +++ b/tests/Features/Expect/json.php @@ -0,0 +1,14 @@ +json() + ->name + ->toBe('Nuno'); +}); + +test('fails with broken json string', function () { + expect('{":"Nuno"}')->json(); +})->throws(ExpectationFailedException::class);