Skip to content

Commit

Permalink
Improve failure message for assertIsList()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 22, 2023
1 parent 85c0730 commit 2bc1aea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
20 changes: 18 additions & 2 deletions src/Framework/Constraint/Traversable/IsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace PHPUnit\Framework\Constraint;

use function array_is_list;
use function gettype;
use function is_array;
use function strtolower;

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

/**
Expand All @@ -46,6 +48,20 @@ protected function matches(mixed $other): bool
*/
protected function failureDescription(mixed $other): string
{
return 'an array ' . $this->toString();
$type = strtolower(gettype($other));

if ($type === 'double') {
$type = 'float';
}

if ($type === 'resource (closed)') {
$type = 'closed resource';
}

return match ($type) {
'array', 'integer', 'object' => 'an ' . $type . ' ' . $this->toString(),
'boolean', 'float', 'null', 'resource', 'string' => 'a ' . $type . ' ' . $this->toString(),
default => 'a value of ' . $type . ' ' . $this->toString(),
};
}
}
14 changes: 7 additions & 7 deletions tests/unit/Framework/Constraint/IsListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public function testConstraintIsNotList(): void
$constraint = new IsList;

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

try {
$constraint->evaluate([1 => 1]);
} catch (ExpectationFailedException $e) {
$this->assertEquals(
<<<'EOF'
Failed asserting that an array is list.
Failed asserting that an array is a list.
EOF
,
Expand All @@ -57,15 +57,15 @@ public function testConstraintIsNotListWithFilteredArray(): void
$constraint = new IsList;

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

try {
$constraint->evaluate([1 => 1]);
} catch (ExpectationFailedException $e) {
$this->assertEquals(
<<<'EOF'
Failed asserting that an array is list.
Failed asserting that an array is a list.
EOF
,
Expand All @@ -88,7 +88,7 @@ public function testConstraintIsNotListWithCustomMessage(): void
$this->assertEquals(
<<<'EOF'
custom message
Failed asserting that an array is list.
Failed asserting that an array is a list.
EOF
,
Expand All @@ -110,7 +110,7 @@ public function testConstraintIsNotListWhenNotArray(): void
} catch (ExpectationFailedException $e) {
$this->assertEquals(
<<<'EOF'
Failed asserting that an array is list.
Failed asserting that a string is a list.
EOF
,
Expand All @@ -131,7 +131,7 @@ public function testConstraintArrayIsNotList(): void
$this->assertEquals(
<<<'EOF'
custom message
Failed asserting that an array is not list.
Failed asserting that an array is not a list.
EOF
,
Expand Down

0 comments on commit 2bc1aea

Please sign in to comment.