feat: allow string type in gt/lt expectations

This commit is contained in:
Pauls Nartišs
2024-01-05 16:06:54 +02:00
parent dc1e4f040d
commit b1c59ec2e6
6 changed files with 28 additions and 4 deletions

View File

@ -131,7 +131,7 @@ final class Expectation
* *
* @return self<TValue> * @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); Assert::assertGreaterThan($expected, $this->value, $message);
@ -143,7 +143,7 @@ final class Expectation
* *
* @return self<TValue> * @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); Assert::assertGreaterThanOrEqual($expected, $this->value, $message);
@ -155,7 +155,7 @@ final class Expectation
* *
* @return self<TValue> * @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); Assert::assertLessThan($expected, $this->value, $message);
@ -167,7 +167,7 @@ final class Expectation
* *
* @return self<TValue> * @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); Assert::assertLessThanOrEqual($expected, $this->value, $message);

View File

@ -426,6 +426,7 @@
PASS Tests\Features\Expect\toBeGreaterThan PASS Tests\Features\Expect\toBeGreaterThan
✓ passes ✓ passes
✓ passes with DateTime and DateTimeImmutable ✓ passes with DateTime and DateTimeImmutable
✓ passes with strings
✓ failures ✓ failures
✓ failures with custom message ✓ failures with custom message
✓ not failures ✓ not failures
@ -433,6 +434,7 @@
PASS Tests\Features\Expect\toBeGreaterThanOrEqual PASS Tests\Features\Expect\toBeGreaterThanOrEqual
✓ passes ✓ passes
✓ passes with DateTime and DateTimeImmutable ✓ passes with DateTime and DateTimeImmutable
✓ passes with strings
✓ failures ✓ failures
✓ failures with custom message ✓ failures with custom message
✓ not failures ✓ not failures
@ -490,6 +492,7 @@
PASS Tests\Features\Expect\toBeLessThan PASS Tests\Features\Expect\toBeLessThan
✓ passes ✓ passes
✓ passes with DateTime and DateTimeImmutable ✓ passes with DateTime and DateTimeImmutable
✓ passes with strings
✓ failures ✓ failures
✓ failures with custom message ✓ failures with custom message
✓ not failures ✓ not failures
@ -497,6 +500,7 @@
PASS Tests\Features\Expect\toBeLessThanOrEqual PASS Tests\Features\Expect\toBeLessThanOrEqual
✓ passes ✓ passes
✓ passes with DateTime and DateTimeImmutable ✓ passes with DateTime and DateTimeImmutable
✓ passes with strings
✓ failures ✓ failures
✓ failures with custom message ✓ failures with custom message
✓ not failures ✓ not failures

View File

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

View File

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

View File

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

View File

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