Skip to content

Commit 2bc1aea

Browse files
Improve failure message for assertIsList()
1 parent 85c0730 commit 2bc1aea

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/Framework/Constraint/Traversable/IsList.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
namespace PHPUnit\Framework\Constraint;
1111

1212
use function array_is_list;
13+
use function gettype;
1314
use function is_array;
15+
use function strtolower;
1416

1517
/**
1618
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
@@ -22,7 +24,7 @@ final class IsList extends Constraint
2224
*/
2325
public function toString(): string
2426
{
25-
return 'is list';
27+
return 'is a list';
2628
}
2729

2830
/**
@@ -46,6 +48,20 @@ protected function matches(mixed $other): bool
4648
*/
4749
protected function failureDescription(mixed $other): string
4850
{
49-
return 'an array ' . $this->toString();
51+
$type = strtolower(gettype($other));
52+
53+
if ($type === 'double') {
54+
$type = 'float';
55+
}
56+
57+
if ($type === 'resource (closed)') {
58+
$type = 'closed resource';
59+
}
60+
61+
return match ($type) {
62+
'array', 'integer', 'object' => 'an ' . $type . ' ' . $this->toString(),
63+
'boolean', 'float', 'null', 'resource', 'string' => 'a ' . $type . ' ' . $this->toString(),
64+
default => 'a value of ' . $type . ' ' . $this->toString(),
65+
};
5066
}
5167
}

tests/unit/Framework/Constraint/IsListTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public function testConstraintIsNotList(): void
3131
$constraint = new IsList;
3232

3333
$this->assertFalse($constraint->evaluate([1 => 1], '', true));
34-
$this->assertEquals('is list', $constraint->toString());
34+
$this->assertEquals('is a list', $constraint->toString());
3535
$this->assertCount(1, $constraint);
3636

3737
try {
3838
$constraint->evaluate([1 => 1]);
3939
} catch (ExpectationFailedException $e) {
4040
$this->assertEquals(
4141
<<<'EOF'
42-
Failed asserting that an array is list.
42+
Failed asserting that an array is a list.
4343

4444
EOF
4545
,
@@ -57,15 +57,15 @@ public function testConstraintIsNotListWithFilteredArray(): void
5757
$constraint = new IsList;
5858

5959
$this->assertFalse($constraint->evaluate([0 => 0, 1 => 1, 3 => 3], '', true));
60-
$this->assertEquals('is list', $constraint->toString());
60+
$this->assertEquals('is a list', $constraint->toString());
6161
$this->assertCount(1, $constraint);
6262

6363
try {
6464
$constraint->evaluate([1 => 1]);
6565
} catch (ExpectationFailedException $e) {
6666
$this->assertEquals(
6767
<<<'EOF'
68-
Failed asserting that an array is list.
68+
Failed asserting that an array is a list.
6969

7070
EOF
7171
,
@@ -88,7 +88,7 @@ public function testConstraintIsNotListWithCustomMessage(): void
8888
$this->assertEquals(
8989
<<<'EOF'
9090
custom message
91-
Failed asserting that an array is list.
91+
Failed asserting that an array is a list.
9292

9393
EOF
9494
,
@@ -110,7 +110,7 @@ public function testConstraintIsNotListWhenNotArray(): void
110110
} catch (ExpectationFailedException $e) {
111111
$this->assertEquals(
112112
<<<'EOF'
113-
Failed asserting that an array is list.
113+
Failed asserting that a string is a list.
114114

115115
EOF
116116
,
@@ -131,7 +131,7 @@ public function testConstraintArrayIsNotList(): void
131131
$this->assertEquals(
132132
<<<'EOF'
133133
custom message
134-
Failed asserting that an array is not list.
134+
Failed asserting that an array is not a list.
135135

136136
EOF
137137
,

0 commit comments

Comments
 (0)