diff --git a/src/Expectation.php b/src/Expectation.php index 300820c1..7703ebb8 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -62,7 +62,7 @@ final class Expectation /** * Creates a new expectation with the decoded JSON value. * - * @return self + * @return self|bool> */ public function json(): Expectation { @@ -70,7 +70,10 @@ final class Expectation InvalidExpectationValue::expected('string'); } - return $this->toBeJson()->and(json_decode($this->value, true)); + /** @var array|bool $value */ + $value = json_decode($this->value, true); + + return $this->toBeJson()->and($value); } /** diff --git a/src/Expectations/HigherOrderExpectation.php b/src/Expectations/HigherOrderExpectation.php index 83c43643..1879e167 100644 --- a/src/Expectations/HigherOrderExpectation.php +++ b/src/Expectations/HigherOrderExpectation.php @@ -79,6 +79,16 @@ final class HigherOrderExpectation return $this->expect($value); } + /** + * Creates a new expectation with the decoded JSON value. + * + * @return self|bool> + */ + public function json(): self + { + return new self($this->original, $this->expectation->json()->value); + } + /** * Dynamically calls methods on the class with the given arguments. * diff --git a/tests/Features/Expect/HigherOrder/methods.php b/tests/Features/Expect/HigherOrder/methods.php index d6da636b..da610cf9 100644 --- a/tests/Features/Expect/HigherOrder/methods.php +++ b/tests/Features/Expect/HigherOrder/methods.php @@ -74,8 +74,20 @@ it('works with higher order tests') ->name()->toEqual('Has Methods') ->books()->each->toBeArray; +it('works consistently with the json expectation method', function () { + expect(new HasMethods()) + ->jsonString()->json()->id->toBe(1) + ->jsonString()->json()->name->toBe('Has Methods')->toBeString() + ->jsonString()->json()->quantity->toBe(20)->toBeInt(); +}); + class HasMethods { + public function jsonString(): string + { + return '{ "id": 1, "name": "Has Methods", "quantity": 20 }'; + } + public function name() { return 'Has Methods';