From c1b27579ca791d3728f211c862448c4398f3e44f Mon Sep 17 00:00:00 2001 From: Daniel Ang Date: Fri, 18 Jun 2021 12:43:17 +0200 Subject: [PATCH 1/4] 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); From ecb37fce91d6ddcf55bf9d5d6c4176127340ad35 Mon Sep 17 00:00:00 2001 From: Daniel Ang Date: Fri, 18 Jun 2021 13:04:31 +0200 Subject: [PATCH 2/4] Refactoring to use toBeJson() Refactoring according to @lukeraymonddowning suggestion --- src/Expectation.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 3c238805..e2d6369b 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -65,10 +65,7 @@ final class Expectation */ public function json(): Expectation { - Assert::assertIsString($this->value); - Assert::assertJson($this->value); - - return new self(json_decode($this->value, true)); + return $this->toBeJson()->and(json_decode($this->value, true)); } /** From 4bf69b97bdb20844b36f902535ba9a8eb2771f19 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 18 Jun 2021 12:06:41 +0100 Subject: [PATCH 3/4] feat(json): updates docs --- src/Expectation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Expectation.php b/src/Expectation.php index e2d6369b..85cb0094 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -61,7 +61,7 @@ final class Expectation } /** - * Parses Json String to Array. + * Creates a new expectation with the decoded JSON value. */ public function json(): Expectation { From 3afdedbd3f3e486483073fc0cacf87fb3ada454a Mon Sep 17 00:00:00 2001 From: Daniel Ang Date: Fri, 18 Jun 2021 13:11:37 +0200 Subject: [PATCH 4/4] Snapshots --- tests/.snapshots/success.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 05d2cc3a..422bfc2f 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -140,6 +140,10 @@ ✓ it macros true is true with argument ✓ it macros false is not true with argument + PASS Tests\Features\Expect\json + ✓ it properly parses json string + ✓ fails with broken json string + PASS Tests\Features\Expect\not ✓ not property calls @@ -569,5 +573,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 7 skipped, 355 passed + Tests: 4 incompleted, 7 skipped, 357 passed \ No newline at end of file