From b1c59ec2e69213045f077a72b01d964ada0d0310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pauls=20Narti=C5=A1s?= Date: Fri, 5 Jan 2024 16:06:54 +0200 Subject: [PATCH] feat: allow string type in gt/lt expectations --- src/Mixins/Expectation.php | 8 ++++---- tests/.snapshots/success.txt | 4 ++++ tests/Features/Expect/toBeGreaterThan.php | 5 +++++ tests/Features/Expect/toBeGreaterThanOrEqual.php | 5 +++++ tests/Features/Expect/toBeLessThan.php | 5 +++++ tests/Features/Expect/toBeLessThanOrEqual.php | 5 +++++ 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Mixins/Expectation.php b/src/Mixins/Expectation.php index 9b1e07a8..9308866e 100644 --- a/src/Mixins/Expectation.php +++ b/src/Mixins/Expectation.php @@ -131,7 +131,7 @@ final class Expectation * * @return self */ - 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 */ - 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 */ - 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 */ - 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); diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index e3b529c9..5f308572 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -426,6 +426,7 @@ PASS Tests\Features\Expect\toBeGreaterThan ✓ passes ✓ passes with DateTime and DateTimeImmutable + ✓ passes with strings ✓ failures ✓ failures with custom message ✓ not failures @@ -433,6 +434,7 @@ PASS Tests\Features\Expect\toBeGreaterThanOrEqual ✓ passes ✓ passes with DateTime and DateTimeImmutable + ✓ passes with strings ✓ failures ✓ failures with custom message ✓ not failures @@ -490,6 +492,7 @@ PASS Tests\Features\Expect\toBeLessThan ✓ passes ✓ passes with DateTime and DateTimeImmutable + ✓ passes with strings ✓ failures ✓ failures with custom message ✓ not failures @@ -497,6 +500,7 @@ PASS Tests\Features\Expect\toBeLessThanOrEqual ✓ passes ✓ passes with DateTime and DateTimeImmutable + ✓ passes with strings ✓ failures ✓ failures with custom message ✓ not failures diff --git a/tests/Features/Expect/toBeGreaterThan.php b/tests/Features/Expect/toBeGreaterThan.php index a3ceafef..f59c6628 100644 --- a/tests/Features/Expect/toBeGreaterThan.php +++ b/tests/Features/Expect/toBeGreaterThan.php @@ -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); diff --git a/tests/Features/Expect/toBeGreaterThanOrEqual.php b/tests/Features/Expect/toBeGreaterThanOrEqual.php index 4a0f62ad..52aa3b18 100644 --- a/tests/Features/Expect/toBeGreaterThanOrEqual.php +++ b/tests/Features/Expect/toBeGreaterThanOrEqual.php @@ -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); diff --git a/tests/Features/Expect/toBeLessThan.php b/tests/Features/Expect/toBeLessThan.php index 802c1c08..f7de96fa 100644 --- a/tests/Features/Expect/toBeLessThan.php +++ b/tests/Features/Expect/toBeLessThan.php @@ -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); diff --git a/tests/Features/Expect/toBeLessThanOrEqual.php b/tests/Features/Expect/toBeLessThanOrEqual.php index e5643759..f25f0774 100644 --- a/tests/Features/Expect/toBeLessThanOrEqual.php +++ b/tests/Features/Expect/toBeLessThanOrEqual.php @@ -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);