Skip to content

Commit c844b22

Browse files
authored
Improved exception message (#50517)
1 parent cf604d2 commit c844b22

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/Illuminate/Collections/Traits/EnumeratesValues.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public function ensure($type)
326326
{
327327
$allowedTypes = is_array($type) ? $type : [$type];
328328

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

332332
foreach ($allowedTypes as $allowedType) {
@@ -336,7 +336,7 @@ public function ensure($type)
336336
}
337337

338338
throw new UnexpectedValueException(
339-
sprintf("Collection should only include [%s] items, but '%s' found.", implode(', ', $allowedTypes), $itemType)
339+
sprintf("Collection should only include [%s] items, but '%s' found at position %d.", implode(', ', $allowedTypes), $itemType, $index)
340340
);
341341
});
342342
}

tests/Support/SupportCollectionTest.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -5193,6 +5193,7 @@ public function testEnsureForScalar($collection)
51935193

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)