-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PHPUnitExtraConstraints\Constraint; | ||
|
||
use PHPUnit\Framework\Constraint\Constraint; | ||
|
||
use function is_string; | ||
use function strlen; | ||
use function substr_compare; | ||
|
||
/** | ||
* Constraint that asserts that a string ends with another string. | ||
*/ | ||
final class StringEndsWith extends Constraint | ||
{ | ||
/** @var string */ | ||
private $needle; | ||
|
||
public function __construct(string $needle) | ||
{ | ||
$this->needle = $needle; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function matches($other): bool | ||
{ | ||
return is_string($other) | ||
&& self::endsWith($other, $this->needle); | ||
} | ||
|
||
private static function endsWith(string $haystack, string $needle): bool | ||
{ | ||
return '' === $needle || ('' !== $haystack && 0 === substr_compare($haystack, $needle, -strlen($needle))); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function toString(): string | ||
{ | ||
return 'ends with ' . $this->needle; | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\PHPUnitExtraConstraints\Constraint; | ||
|
||
use PHPUnitExtraConstraints\Constraint\StringEndsWith; | ||
use Tests\PHPUnitExtraConstraints\CustomTestCase; | ||
|
||
class StringEndsWithTest extends CustomTestCase | ||
{ | ||
/** | ||
* @dataProvider provideValidStrings | ||
* @testdox testWithValidString: $value ends with $needle | ||
* | ||
* @param string $needle | ||
* @param string $value | ||
*/ | ||
public function testWithValidString(string $needle, string $value): void | ||
{ | ||
$constraint = new StringEndsWith($needle); | ||
self::assertThat($value, $constraint); | ||
} | ||
|
||
/** | ||
* @return iterable<array<string>> | ||
*/ | ||
public function provideValidStrings(): iterable | ||
{ | ||
yield ['c', 'abc']; | ||
yield ['bc', 'abc']; | ||
yield ['abc', 'abc']; | ||
yield ['', 'abc']; | ||
yield ['0', '00']; | ||
} | ||
|
||
/** | ||
* @dataProvider provideInvalidStrings | ||
* @testdox testWithInvalidString: $value doesn't end with $needle | ||
* | ||
* @param string $needle | ||
* @param mixed $value | ||
*/ | ||
public function testWithInvalidString(string $needle, $value): void | ||
{ | ||
$constraint = new StringEndsWith($needle); | ||
$this->expectAssertionFailedError('ends with ' . $needle); | ||
self::assertThat($value, $constraint); | ||
} | ||
|
||
/** | ||
* @return iterable<array<mixed>> | ||
*/ | ||
public function provideInvalidStrings(): iterable | ||
{ | ||
yield ['zzz', null]; | ||
yield ['zzz', true]; | ||
yield ['zzz', false]; | ||
yield ['zzz', 0]; | ||
yield ['zzz', 1]; | ||
yield ['zzz', []]; | ||
yield ['zzz', (object) []]; | ||
yield ['zzz', 'abc']; | ||
yield ['zzz', 'z']; | ||
} | ||
} |