From 17cde34b672d416ec49038eb8c06931d35d146ff Mon Sep 17 00:00:00 2001 From: Yuichiro Luke Smith Date: Sat, 16 Oct 2021 12:17:40 +0900 Subject: [PATCH 1/4] [8.x] add a way to skip count check but check $callback at the same time --- src/Illuminate/Testing/Fluent/Concerns/Has.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Testing/Fluent/Concerns/Has.php b/src/Illuminate/Testing/Fluent/Concerns/Has.php index e1575d606cc6..abaf9d45e443 100644 --- a/src/Illuminate/Testing/Fluent/Concerns/Has.php +++ b/src/Illuminate/Testing/Fluent/Concerns/Has.php @@ -63,9 +63,13 @@ public function has($key, $length = null, Closure $callback = null): self $this->interactsWith($key); - if (is_int($length) && ! is_null($callback)) { + if (! is_null($callback)) { return $this->has($key, function (self $scope) use ($length, $callback) { - return $scope->count($length) + return $scope->tap(function (self $scope) use ($length) { + if (is_int($length)) { + $scope->count($length); + } + }) ->first($callback) ->etc(); }); From d15f6163ebcb5639ea57290faf1c07453661c600 Mon Sep 17 00:00:00 2001 From: Yuichiro Luke Smith Date: Sat, 16 Oct 2021 12:36:44 +0900 Subject: [PATCH 2/4] add tests --- tests/Testing/Fluent/AssertTest.php | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/Testing/Fluent/AssertTest.php b/tests/Testing/Fluent/AssertTest.php index 0f0ba7159076..723d5b97c59b 100644 --- a/tests/Testing/Fluent/AssertTest.php +++ b/tests/Testing/Fluent/AssertTest.php @@ -607,6 +607,24 @@ public function testScopeShorthand() $this->assertTrue($called, 'The scoped query was never actually called.'); } + public function testScopeShorthandWithoutCount() + { + $assert = AssertableJson::fromArray([ + 'bar' => [ + ['key' => 'first'], + ['key' => 'second'], + ], + ]); + + $called = false; + $assert->has('bar', null, function (AssertableJson $item) use (&$called) { + $item->where('key', 'first'); + $called = true; + }); + + $this->assertTrue($called, 'The scoped query was never actually called.'); + } + public function testScopeShorthandFailsWhenAssertingZeroItems() { $assert = AssertableJson::fromArray([ @@ -641,6 +659,38 @@ public function testScopeShorthandFailsWhenAmountOfItemsDoesNotMatch() }); } + public function testScopeShorthandFailsWhenAssertingEmptyArray() + { + $assert = AssertableJson::fromArray([ + 'bar' => [], + ]); + + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage( + 'Cannot scope directly onto the first element of property [bar] because it is empty.' + ); + + $assert->has('bar', 0, function (AssertableJson $item) { + $item->where('key', 'first'); + }); + } + + public function testScopeShorthandFailsWhenAssertingEmptyArrayWithoutCount() + { + $assert = AssertableJson::fromArray([ + 'bar' => [], + ]); + + $this->expectException(AssertionFailedError::class); + $this->expectExceptionMessage( + 'Cannot scope directly onto the first element of property [bar] because it is empty.' + ); + + $assert->has('bar', null, function (AssertableJson $item) { + $item->where('key', 'first'); + }); + } + public function testFirstScope() { $assert = AssertableJson::fromArray([ From 2919ab2bca847f11f698181cd8c269e3d04e0435 Mon Sep 17 00:00:00 2001 From: Yuichiro Luke Smith Date: Sat, 16 Oct 2021 13:07:24 +0900 Subject: [PATCH 3/4] reformat file --- src/Illuminate/Testing/Fluent/Concerns/Has.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Testing/Fluent/Concerns/Has.php b/src/Illuminate/Testing/Fluent/Concerns/Has.php index abaf9d45e443..c6da974e069b 100644 --- a/src/Illuminate/Testing/Fluent/Concerns/Has.php +++ b/src/Illuminate/Testing/Fluent/Concerns/Has.php @@ -65,11 +65,12 @@ public function has($key, $length = null, Closure $callback = null): self if (! is_null($callback)) { return $this->has($key, function (self $scope) use ($length, $callback) { - return $scope->tap(function (self $scope) use ($length) { - if (is_int($length)) { - $scope->count($length); - } - }) + return $scope + ->tap(function (self $scope) use ($length) { + if (is_int($length)) { + $scope->count($length); + } + }) ->first($callback) ->etc(); }); From 63dc738ed72c2919cdcb12ea8d73b8788a541a18 Mon Sep 17 00:00:00 2001 From: Yuichiro Luke Smith Date: Sat, 16 Oct 2021 13:14:15 +0900 Subject: [PATCH 4/4] broaden the condition to make invalid values fail instead of silently passing --- src/Illuminate/Testing/Fluent/Concerns/Has.php | 2 +- tests/Testing/Fluent/AssertTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Testing/Fluent/Concerns/Has.php b/src/Illuminate/Testing/Fluent/Concerns/Has.php index c6da974e069b..89cd2eafe5b4 100644 --- a/src/Illuminate/Testing/Fluent/Concerns/Has.php +++ b/src/Illuminate/Testing/Fluent/Concerns/Has.php @@ -67,7 +67,7 @@ public function has($key, $length = null, Closure $callback = null): self return $this->has($key, function (self $scope) use ($length, $callback) { return $scope ->tap(function (self $scope) use ($length) { - if (is_int($length)) { + if (! is_null($length)) { $scope->count($length); } }) diff --git a/tests/Testing/Fluent/AssertTest.php b/tests/Testing/Fluent/AssertTest.php index 723d5b97c59b..07f515a230ea 100644 --- a/tests/Testing/Fluent/AssertTest.php +++ b/tests/Testing/Fluent/AssertTest.php @@ -691,6 +691,22 @@ public function testScopeShorthandFailsWhenAssertingEmptyArrayWithoutCount() }); } + public function testScopeShorthandFailsWhenSecondArgumentUnsupportedType() + { + $assert = AssertableJson::fromArray([ + 'bar' => [ + ['key' => 'first'], + ['key' => 'second'], + ], + ]); + + $this->expectException(TypeError::class); + + $assert->has('bar', 'invalid', function (AssertableJson $item) { + $item->where('key', 'first'); + }); + } + public function testFirstScope() { $assert = AssertableJson::fromArray([