diff --git a/src/Assert.php b/src/Assert.php index 7f0500a..eb00a49 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -91,6 +91,48 @@ public static function notEquals(mixed $expected, mixed $actual, string $message )); } + /** + * Asserts that actual value is greater than the defined minimum. + * + * @param mixed $minimum The defined minimum value. + * @param mixed $actual The actual value to compare against the minimum value. + * @param string $message Short description about what exactly is being asserted. + * @throws AssertException when the assertion fails. + */ + public static function greaterThan(mixed $minimum, mixed $actual, string $message = ''): void + { + $actual > $minimum + ? StaticState::log('Assert `' . Support::stringify($actual) . '` is greater than: `' . Support::stringify($minimum) . '`', $message) + : StaticState::fail(AssertException::compare( + $minimum, + $actual, + $message, + pattern: 'Failed asserting that `%2s` is greater than `%1s`', + showDiff: false, + )); + } + + /** + * Asserts that actual value is greater than or equal to the defined minimum. + * + * @param mixed $minimum The defined minimum value. + * @param mixed $actual The actual value to compare against the minimum value. + * @param string $message Short description about what exactly is being asserted. + * @throws AssertException when the assertion fails. + */ + public static function greaterThanOrEqual(mixed $minimum, mixed $actual, string $message = ''): void + { + $actual >= $minimum + ? StaticState::log('Assert `' . Support::stringify($actual) . '` is greater than or equal to: `' . Support::stringify($minimum) . '`', $message) + : StaticState::fail(AssertException::compare( + $minimum, + $actual, + $message, + pattern: 'Failed asserting that `%2s` is greater than or equal to `%1s`', + showDiff: false, + )); + } + /** * Asserts that the condition is true. * diff --git a/tests/Testo/AsserTest.php b/tests/Testo/AsserTest.php index 0f5bd09..850d7c4 100644 --- a/tests/Testo/AsserTest.php +++ b/tests/Testo/AsserTest.php @@ -27,6 +27,8 @@ public function simpleAssertions(): void Assert::instanceOf(\Exception::class, new \RuntimeException()); Assert::equals(1, '1'); Assert::notEquals(42, 43); + Assert::greaterThan(1, 2); + Assert::greaterThanOrEqual(1, 2); } #[Test] diff --git a/tests/Testo/Assert/AssertGreaterThan.php b/tests/Testo/Assert/AssertGreaterThan.php new file mode 100644 index 0000000..d6a5dc4 --- /dev/null +++ b/tests/Testo/Assert/AssertGreaterThan.php @@ -0,0 +1,49 @@ + 3 + Assert::greaterThan(['a' => 1, 'b' => 2], ['a' => 1, 'b' => 2, 'c' => 0]); // associative, same key order: longer wins + + // comparison of associative arrays with different keys won't work and throws AssertException: + // Assert::greaterThan(['a' => 1, 'x' => 1], ['a' => 1, 'b' => 999]); + } + + #[Test] + public function datetimes(): void + { + $min = new \DateTimeImmutable('2025-01-01 00:00:00+00:00'); + $actual = new \DateTimeImmutable('2025-01-02 00:00:00+00:00'); + Assert::greaterThan($min, $actual); // later time is bigger + } +} diff --git a/tests/Testo/Assert/AssertGreaterThanOrEqual.php b/tests/Testo/Assert/AssertGreaterThanOrEqual.php new file mode 100644 index 0000000..337e445 --- /dev/null +++ b/tests/Testo/Assert/AssertGreaterThanOrEqual.php @@ -0,0 +1,56 @@ + 3 + Assert::greaterThanOrEqual(['a' => 1, 'b' => 2], ['a' => 1, 'b' => 2, 'c' => 0]); // associative, same key order: longer wins + Assert::greaterThanOrEqual(['b' => 2, 'a' => 1], ['a' => 1, 'b' => 2]); // associative arrays, same elements, different order - considered equal + + // comparison of associative arrays with different keys won't work and throws AssertException: + // Assert::greaterThanOrEqual(['a' => 1, 'x' => 1], ['a' => 1, 'b' => 999]); + } + + #[Test] + public function datetimes(): void + { + $min = new \DateTimeImmutable('2025-01-01 00:00:00+00:00'); + $actual1 = new \DateTimeImmutable('2025-01-02 00:00:00+00:00'); + $actual2 = new \DateTimeImmutable('2025-01-01 00:00:00+00:00'); + Assert::greaterThanOrEqual($min, $actual1); // later time is bigger + Assert::greaterThanOrEqual($min, $actual2); // same time are equal + } +}