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);