Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add a way to skip count check but check $callback at the same time for AssertableJson->has() #39224

Merged
merged 4 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Illuminate/Testing/Fluent/Concerns/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ 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_null($length)) {
$scope->count($length);
}
})
->first($callback)
->etc();
});
Expand Down
66 changes: 66 additions & 0 deletions tests/Testing/Fluent/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -641,6 +659,54 @@ 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 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([
Expand Down