Skip to content

Commit

Permalink
add toBeBetween() expectation
Browse files Browse the repository at this point in the history
  • Loading branch information
JonPurvis committed Sep 22, 2023
1 parent 6bc9da3 commit be0d9e9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Mixins/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1128,4 +1128,17 @@ public function toBeUuid(string $message = ''): self

return $this;
}

/**
* Asserts that the value is between 2 specified values
*
* @return self<TValue>
*/
public function toBeBetween(int|float|DateTimeInterface $lowestValue, int|float|DateTimeInterface $highestValue, string $message = ''): self
{
Assert::assertGreaterThanOrEqual($lowestValue, $this->value, $message);
Assert::assertLessThanOrEqual($highestValue, $this->value, $message);

return $this;
}
}
43 changes: 43 additions & 0 deletions tests/Features/Expect/toBeBetween.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use PHPUnit\Framework\ExpectationFailedException;

test('passes with int', function () {
expect(2)->toBeBetween(1, 3);
});

test('passes with float', function () {
expect(1.5)->toBeBetween(1.25, 1.75);
});

test('passes with float and int', function () {
expect(1.5)->toBeBetween(1, 2);
});

test('passes with DateTime', function () {
expect(new DateTime('2023-09-22'))->toBeBetween(new DateTime('2023-09-21'), new DateTime('2023-09-23'));
});

test('failure with int', function () {
expect(4)->toBeBetween(1, 3);
})->throws(ExpectationFailedException::class);

test('failure with float', function () {
expect(2)->toBeBetween(1.5, 1.75);
})->throws(ExpectationFailedException::class);

test('failure with float and int', function () {
expect(2.1)->toBeBetween(1, 2);
})->throws(ExpectationFailedException::class);

test('failure with DateTime', function () {
expect(new DateTime('2023-09-20'))->toBeBetween(new DateTime('2023-09-21'), new DateTime('2023-09-23'));
})->throws(ExpectationFailedException::class);

test('failures with custom message', function () {
expect(4)->toBeBetween(1, 3, 'oh no!');
})->throws(ExpectationFailedException::class, 'oh no!');

test('not failures', function () {
expect(2)->not->toBeBetween(1, 3);
})->throws(ExpectationFailedException::class);

0 comments on commit be0d9e9

Please sign in to comment.