diff --git a/src/Expectation.php b/src/Expectation.php index 45b52891..85cb0094 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -60,6 +60,14 @@ final class Expectation return new self($value); } + /** + * Creates a new expectation with the decoded JSON value. + */ + public function json(): Expectation + { + return $this->toBeJson()->and(json_decode($this->value, true)); + } + /** * Dump the expectation value and end the script. * 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 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);