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

[11.x] Improved exception message on 'ensure' method #50517

Merged
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
4 changes: 2 additions & 2 deletions src/Illuminate/Collections/Traits/EnumeratesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public function ensure($type)
{
$allowedTypes = is_array($type) ? $type : [$type];

return $this->each(function ($item) use ($allowedTypes) {
return $this->each(function ($item, $index) use ($allowedTypes) {
$itemType = get_debug_type($item);

foreach ($allowedTypes as $allowedType) {
Expand All @@ -336,7 +336,7 @@ public function ensure($type)
}

throw new UnexpectedValueException(
sprintf("Collection should only include [%s] items, but '%s' found.", implode(', ', $allowedTypes), $itemType)
sprintf("Collection should only include [%s] items, but '%s' found at position %d.", implode(', ', $allowedTypes), $itemType, $index)
);
});
}
Expand Down
10 changes: 8 additions & 2 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5193,6 +5193,7 @@ public function testEnsureForScalar($collection)

$data = $collection::make([1, 2, 3, 'foo']);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage("Collection should only include [int] items, but 'string' found at position 3.");
$data->ensure('int');
}

Expand All @@ -5204,6 +5205,7 @@ public function testEnsureForObjects($collection)

$data = $collection::make([new stdClass, new stdClass, new stdClass, $collection]);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage(sprintf('Collection should only include [%s] items, but \'%s\' found at position %d.', class_basename(new stdClass()), gettype($collection), 3));
$data->ensure(stdClass::class);
}

Expand All @@ -5213,8 +5215,10 @@ public function testEnsureForInheritance($collection)
$data = $collection::make([new \Error, new \Error]);
$data->ensure(\Throwable::class);

$data = $collection::make([new \Error, new \Error, new $collection]);
$wrongType = new $collection;
$data = $collection::make([new \Error, new \Error, $wrongType]);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage(sprintf("Collection should only include [%s] items, but '%s' found at position %d.", \Throwable::class, get_class($wrongType), 2));
$data->ensure(\Throwable::class);
}

Expand All @@ -5224,8 +5228,10 @@ public function testEnsureForMultipleTypes($collection)
$data = $collection::make([new \Error, 123]);
$data->ensure([\Throwable::class, 'int']);

$data = $collection::make([new \Error, new \Error, new $collection]);
$wrongType = new $collection;
$data = $collection::make([new \Error, new \Error, $wrongType]);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage(sprintf('Collection should only include [%s] items, but \'%s\' found at position %d.', implode(', ', [\Throwable::class, 'int']), get_class($wrongType), 2));
$data->ensure([\Throwable::class, 'int']);
}

Expand Down
Loading