From b3d3b4485d9a008676c154f9399c5c13ac183804 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sun, 16 Jan 2022 22:32:20 +1000 Subject: [PATCH 1/3] Do not nest expectations --- src/Expectation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Expectation.php b/src/Expectation.php index f98638f7..8dc287a0 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -56,7 +56,7 @@ final class Expectation */ public function and(mixed $value): Expectation { - return new self($value); + return $value instanceof static ? $value : new self($value); } /** From d24091d224d52b8178be7ec5fc47d72def61f340 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sun, 16 Jan 2022 22:32:38 +1000 Subject: [PATCH 2/3] Fix return docblock --- src/Support/HigherOrderCallables.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/HigherOrderCallables.php b/src/Support/HigherOrderCallables.php index c6533929..9790d2cb 100644 --- a/src/Support/HigherOrderCallables.php +++ b/src/Support/HigherOrderCallables.php @@ -44,7 +44,7 @@ final class HigherOrderCallables * * @param callable|TValue $value * - * @return Expectation + * @return Expectation<(callable(): mixed)|TValue> */ public function and(mixed $value) { From 30b1f6cd0a5ef5f07251fc97a2d02b55fb69cb17 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sun, 16 Jan 2022 22:32:55 +1000 Subject: [PATCH 3/3] Add test for and() --- .../Features/Expect/HigherOrder/methodsAndProperties.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Features/Expect/HigherOrder/methodsAndProperties.php b/tests/Features/Expect/HigherOrder/methodsAndProperties.php index b98c9f16..c9ef45aa 100644 --- a/tests/Features/Expect/HigherOrder/methodsAndProperties.php +++ b/tests/Features/Expect/HigherOrder/methodsAndProperties.php @@ -48,6 +48,15 @@ it('can start a new higher order expectation using the and syntax in higher orde ->toBeArray() ->foo->toEqual('bar'); +it('can start a new higher order expectation using the and syntax without nesting expectations', function () { + expect(new HasMethodsAndProperties()) + ->toBeInstanceOf(HasMethodsAndProperties::class) + ->meta + ->sequence( + function ($value, $key) { $value->toBeArray()->and($key)->toBe('foo'); }, + ); +}); + class HasMethodsAndProperties { public $name = 'Has Methods and Properties';