Merge pull request #1055 from mapon-com/feature/string-comparison-expectations

[2.x] Allow string type in greaterThan/lessThan expectations
This commit is contained in:
Nuno Maduro
2024-01-25 16:48:27 +00:00
committed by GitHub
6 changed files with 28 additions and 4 deletions

View File

@ -131,7 +131,7 @@ final class Expectation
*
* @return self<TValue>
*/
public function toBeGreaterThan(int|float|DateTimeInterface $expected, string $message = ''): self
public function toBeGreaterThan(int|float|string|DateTimeInterface $expected, string $message = ''): self
{
Assert::assertGreaterThan($expected, $this->value, $message);
@ -143,7 +143,7 @@ final class Expectation
*
* @return self<TValue>
*/
public function toBeGreaterThanOrEqual(int|float|DateTimeInterface $expected, string $message = ''): self
public function toBeGreaterThanOrEqual(int|float|string|DateTimeInterface $expected, string $message = ''): self
{
Assert::assertGreaterThanOrEqual($expected, $this->value, $message);
@ -155,7 +155,7 @@ final class Expectation
*
* @return self<TValue>
*/
public function toBeLessThan(int|float|DateTimeInterface $expected, string $message = ''): self
public function toBeLessThan(int|float|string|DateTimeInterface $expected, string $message = ''): self
{
Assert::assertLessThan($expected, $this->value, $message);
@ -167,7 +167,7 @@ final class Expectation
*
* @return self<TValue>
*/
public function toBeLessThanOrEqual(int|float|DateTimeInterface $expected, string $message = ''): self
public function toBeLessThanOrEqual(int|float|string|DateTimeInterface $expected, string $message = ''): self
{
Assert::assertLessThanOrEqual($expected, $this->value, $message);

View File

@ -430,6 +430,7 @@
PASS Tests\Features\Expect\toBeGreaterThan
✓ passes
✓ passes with DateTime and DateTimeImmutable
✓ passes with strings
✓ failures
✓ failures with custom message
✓ not failures
@ -437,6 +438,7 @@
PASS Tests\Features\Expect\toBeGreaterThanOrEqual
✓ passes
✓ passes with DateTime and DateTimeImmutable
✓ passes with strings
✓ failures
✓ failures with custom message
✓ not failures
@ -494,6 +496,7 @@
PASS Tests\Features\Expect\toBeLessThan
✓ passes
✓ passes with DateTime and DateTimeImmutable
✓ passes with strings
✓ failures
✓ failures with custom message
✓ not failures
@ -501,6 +504,7 @@
PASS Tests\Features\Expect\toBeLessThanOrEqual
✓ passes
✓ passes with DateTime and DateTimeImmutable
✓ passes with strings
✓ failures
✓ failures with custom message
✓ not failures

View File

@ -16,6 +16,11 @@ test('passes with DateTime and DateTimeImmutable', function () {
expect($past)->not->toBeGreaterThan($now);
});
test('passes with strings', function () {
expect('b')->toBeGreaterThan('a');
expect('a')->not->toBeGreaterThan('a');
});
test('failures', function () {
expect(4)->toBeGreaterThan(4);
})->throws(ExpectationFailedException::class);

View File

@ -18,6 +18,11 @@ test('passes with DateTime and DateTimeImmutable', function () {
expect($past)->not->toBeGreaterThanOrEqual($now);
});
test('passes with strings', function () {
expect('b')->toBeGreaterThanOrEqual('a');
expect('a')->toBeGreaterThanOrEqual('a');
});
test('failures', function () {
expect(4)->toBeGreaterThanOrEqual(4.1);
})->throws(ExpectationFailedException::class);

View File

@ -16,6 +16,11 @@ test('passes with DateTime and DateTimeImmutable', function () {
expect($now)->not->toBeLessThan($now);
});
test('passes with strings', function () {
expect('a')->toBeLessThan('b');
expect('a')->not->toBeLessThan('a');
});
test('failures', function () {
expect(4)->toBeLessThan(4);
})->throws(ExpectationFailedException::class);

View File

@ -18,6 +18,11 @@ test('passes with DateTime and DateTimeImmutable', function () {
expect($now)->not->toBeLessThanOrEqual($past);
});
test('passes with strings', function () {
expect('a')->toBeLessThanOrEqual('b');
expect('a')->toBeLessThanOrEqual('a');
});
test('failures', function () {
expect(4)->toBeLessThanOrEqual(3.9);
})->throws(ExpectationFailedException::class);