Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions tests/Testo/AsserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
49 changes: 49 additions & 0 deletions tests/Testo/Assert/AssertGreaterThan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tests\Testo\Assert;

use Testo\Assert;
use Testo\Attribute\Test;

/**
* Assertion examples.
*/
final class AssertGreaterThan
{
#[Test]
public function numbers(): void
{
Assert::greaterThan(1, 2);
Assert::greaterThan(1, 1.1);
Assert::greaterThan('1', 2); //'1' coerces to 1
}

#[Test]
public function strings(): void
{
Assert::greaterThan("42", "43"); //numeric strings are cast to numbers
Assert::greaterThan("aaa", "aaaa"); // longer string is bigger
Assert::greaterThan("beers", "beerz"); // byte-wise comparison (first different letters are compared as byte values)
}

#[Test]
public function arrays(): void
{
Assert::greaterThan([1, 2], [1, 2, 3]); // prefix rule: longer wins
Assert::greaterThan([1, 3], [1, 4]); // first differing element decides: 4 > 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
}
}
56 changes: 56 additions & 0 deletions tests/Testo/Assert/AssertGreaterThanOrEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Tests\Testo\Assert;

use Testo\Assert;
use Testo\Attribute\Test;

/**
* Assertion examples.
*/
final class AssertGreaterThanOrEqual
{
#[Test]
public function numbers(): void
{
Assert::greaterThanOrEqual(1, 2);
Assert::greaterThanOrEqual(1, 1.1);
Assert::greaterThanOrEqual('1', 2); //'1' coerces to 1
Assert::greaterThanOrEqual('1', 1);
}

#[Test]
public function strings(): void
{
Assert::greaterThanOrEqual("42", "42"); //numeric strings are cast to numbers
Assert::greaterThanOrEqual("42", "43");
Assert::greaterThanOrEqual("aaa", "aaaa"); // longer string is bigger
Assert::greaterThanOrEqual("aaa", "aaa");
Assert::greaterThanOrEqual("beers", "beerz"); // byte-wise comparison (first different letters are compared as byte values)
}

#[Test]
public function arrays(): void
{
Assert::greaterThanOrEqual([1, 2], [1, 2]); // same arrays considered equal
Assert::greaterThanOrEqual([1, 2], [1, 2, 3]); // prefix rule: longer wins
Assert::greaterThanOrEqual([1, 3], [1, 4]); // first differing element decides: 4 > 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
}
}