-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Framework\Constraint; | ||
|
||
use function is_array; | ||
|
||
/** | ||
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final class ArrayIsList extends Constraint | ||
{ | ||
/** | ||
* Returns a string representation of the constraint. | ||
*/ | ||
public function toString(): string | ||
{ | ||
return 'is list'; | ||
} | ||
|
||
/** | ||
* Evaluates the constraint for parameter $other. Returns true if the | ||
* constraint is met, false otherwise. | ||
*/ | ||
protected function matches(mixed $other): bool | ||
{ | ||
if (!is_array($other)) { | ||
return false; | ||
} | ||
|
||
if ($other === []) { | ||
return true; | ||
} | ||
|
||
$expectedKey = 0; | ||
|
||
foreach ($other as $key => $value) { | ||
if ($key !== $expectedKey) { | ||
return false; | ||
} | ||
|
||
$expectedKey++; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns the description of the failure. | ||
* | ||
* The beginning of failure messages is "Failed asserting that" in most | ||
* cases. This method should return the second part of that sentence. | ||
*/ | ||
protected function failureDescription(mixed $other): string | ||
{ | ||
return 'an array ' . $this->toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Framework\Constraint; | ||
|
||
use PHPUnit\Framework\ExpectationFailedException; | ||
use PHPUnit\Framework\TestFailure; | ||
|
||
/** | ||
* @small | ||
*/ | ||
final class ArrayIsListTest extends ConstraintTestCase | ||
{ | ||
public function testConstraintArrayIsListWhenEmpty(): void | ||
{ | ||
$constraint = new ArrayIsList(); | ||
|
||
$this->assertTrue($constraint->evaluate([], '', true)); | ||
} | ||
|
||
public function testConstraintArrayIsNotList(): void | ||
{ | ||
$constraint = new ArrayIsList(); | ||
|
||
$this->assertFalse($constraint->evaluate([1 => 1], '', true)); | ||
$this->assertEquals('is 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. | ||
EOF | ||
, | ||
TestFailure::exceptionToString($e) | ||
); | ||
|
||
return; | ||
} | ||
|
||
$this->fail(); | ||
} | ||
|
||
public function testConstraintArrayIsNotListWithFilteredArray(): void | ||
{ | ||
$constraint = new ArrayIsList(); | ||
|
||
$this->assertFalse($constraint->evaluate([0 => 0, 1 => 1, 3 => 3], '', true)); | ||
$this->assertEquals('is 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. | ||
EOF | ||
, | ||
TestFailure::exceptionToString($e) | ||
); | ||
|
||
return; | ||
} | ||
|
||
$this->fail(); | ||
} | ||
|
||
public function testConstraintArrayIsNotListWithCustomMessage(): void | ||
{ | ||
$constraint = new ArrayIsList(); | ||
|
||
try { | ||
$constraint->evaluate([1 => 1], 'custom message'); | ||
} catch (ExpectationFailedException $e) { | ||
$this->assertEquals( | ||
<<<'EOF' | ||
custom message | ||
Failed asserting that an array is list. | ||
EOF | ||
, | ||
TestFailure::exceptionToString($e) | ||
); | ||
|
||
return; | ||
} | ||
|
||
$this->fail(); | ||
} | ||
|
||
public function testConstraintArrayIsNotListWhenNotArray(): void | ||
{ | ||
$constraint = new ArrayIsList(); | ||
|
||
try { | ||
$constraint->evaluate('not array'); | ||
} catch (ExpectationFailedException $e) { | ||
$this->assertEquals( | ||
<<<'EOF' | ||
Failed asserting that an array is list. | ||
EOF | ||
, | ||
TestFailure::exceptionToString($e) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters