diff --git a/CHANGELOG.md b/CHANGELOG.md index bfe3338..74c400b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ - **[Breaking change](./UPGRADE.md#removed-deprecated-isdate-methods-from-moment)**: Removed deprecated `isDate*` methods from `Moment`. - Added `Clock` implementation with `SystemClock` and `FrozenClock`. -- Added guard methods for comparison methods to `Moment` and `Date` like `mustBeEqualTo` with option to throw custom exception. +- Added guard methods for comparison methods to `Moment`, `Date` and `Time` like `mustBeEqualTo` with option to throw custom exception. ## 0.10.0 diff --git a/src/Date.php b/src/Date.php index bfaeb08..b6dad92 100644 --- a/src/Date.php +++ b/src/Date.php @@ -162,7 +162,7 @@ public function weekday(): Weekday * @param ?callable(): \Throwable $otherwiseThrow * * @throws \Throwable - * @throws Exception\DateIsNotEqual + * @throws Exception\DateIsNotEqualTo */ public function mustBeEqualTo( self $date, @@ -171,7 +171,7 @@ public function mustBeEqualTo( if ($this->isNotEqualTo($date)) { throw $otherwiseThrow !== null ? $otherwiseThrow() - : new Exception\DateIsNotEqual(); + : new Exception\DateIsNotEqualTo(); } } @@ -179,7 +179,7 @@ public function mustBeEqualTo( * @param ?callable(): \Throwable $otherwiseThrow * * @throws \Throwable - * @throws Exception\DateIsEqual + * @throws Exception\DateIsEqualTo */ public function mustNotBeEqualTo( self $date, @@ -188,7 +188,7 @@ public function mustNotBeEqualTo( if ($this->isEqualTo($date)) { throw $otherwiseThrow !== null ? $otherwiseThrow() - : new Exception\DateIsEqual(); + : new Exception\DateIsEqualTo(); } } diff --git a/src/Exception/DateIsEqual.php b/src/Exception/DateIsEqualTo.php similarity index 81% rename from src/Exception/DateIsEqual.php rename to src/Exception/DateIsEqualTo.php index bc73273..6447838 100644 --- a/src/Exception/DateIsEqual.php +++ b/src/Exception/DateIsEqualTo.php @@ -9,7 +9,7 @@ * * @codeCoverageIgnore */ -final class DateIsEqual extends \InvalidArgumentException +final class DateIsEqualTo extends \InvalidArgumentException { public function __construct() { diff --git a/src/Exception/DateIsNotEqual.php b/src/Exception/DateIsNotEqualTo.php similarity index 80% rename from src/Exception/DateIsNotEqual.php rename to src/Exception/DateIsNotEqualTo.php index 7e5b857..12b7ac7 100644 --- a/src/Exception/DateIsNotEqual.php +++ b/src/Exception/DateIsNotEqualTo.php @@ -9,7 +9,7 @@ * * @codeCoverageIgnore */ -final class DateIsNotEqual extends \InvalidArgumentException +final class DateIsNotEqualTo extends \InvalidArgumentException { public function __construct() { diff --git a/src/Exception/MomentIsEqual.php b/src/Exception/MomentIsEqualTo.php similarity index 80% rename from src/Exception/MomentIsEqual.php rename to src/Exception/MomentIsEqualTo.php index a76ae0e..610cec1 100644 --- a/src/Exception/MomentIsEqual.php +++ b/src/Exception/MomentIsEqualTo.php @@ -9,7 +9,7 @@ * * @codeCoverageIgnore */ -final class MomentIsEqual extends \InvalidArgumentException +final class MomentIsEqualTo extends \InvalidArgumentException { public function __construct() { diff --git a/src/Exception/MomentIsNotEqual.php b/src/Exception/MomentIsNotEqualTo.php similarity index 80% rename from src/Exception/MomentIsNotEqual.php rename to src/Exception/MomentIsNotEqualTo.php index 2b69f62..165608f 100644 --- a/src/Exception/MomentIsNotEqual.php +++ b/src/Exception/MomentIsNotEqualTo.php @@ -9,7 +9,7 @@ * * @codeCoverageIgnore */ -final class MomentIsNotEqual extends \InvalidArgumentException +final class MomentIsNotEqualTo extends \InvalidArgumentException { public function __construct() { diff --git a/src/Exception/TimeIsAfter.php b/src/Exception/TimeIsAfter.php new file mode 100644 index 0000000..be92b81 --- /dev/null +++ b/src/Exception/TimeIsAfter.php @@ -0,0 +1,18 @@ +isNotEqualTo($moment)) { throw $otherwiseThrow !== null ? $otherwiseThrow() - : new Exception\MomentIsNotEqual(); + : new Exception\MomentIsNotEqualTo(); } } @@ -425,7 +425,7 @@ public function mustBeEqualTo( * @param ?callable(): \Throwable $otherwiseThrow * * @throws \Throwable - * @throws Exception\MomentIsNotEqual + * @throws Exception\MomentIsNotEqualTo */ public function mustBeEqualToInTimeZone( Time | Weekday | Date | Month | Year $moment, @@ -435,7 +435,7 @@ public function mustBeEqualToInTimeZone( if ($this->isNotEqualToInTimeZone($moment, $timeZone)) { throw $otherwiseThrow !== null ? $otherwiseThrow() - : new Exception\MomentIsNotEqual(); + : new Exception\MomentIsNotEqualTo(); } } @@ -443,7 +443,7 @@ public function mustBeEqualToInTimeZone( * @param ?callable(): \Throwable $otherwiseThrow * * @throws \Throwable - * @throws Exception\MomentIsEqual + * @throws Exception\MomentIsEqualTo */ public function mustNotBeEqualTo( self $moment, @@ -452,7 +452,7 @@ public function mustNotBeEqualTo( if ($this->isEqualTo($moment)) { throw $otherwiseThrow !== null ? $otherwiseThrow() - : new Exception\MomentIsEqual(); + : new Exception\MomentIsEqualTo(); } } @@ -460,7 +460,7 @@ public function mustNotBeEqualTo( * @param ?callable(): \Throwable $otherwiseThrow * * @throws \Throwable - * @throws Exception\MomentIsEqual + * @throws Exception\MomentIsEqualTo */ public function mustNotBeEqualToInTimeZone( Time | Weekday | Date | Month | Year $moment, @@ -470,7 +470,7 @@ public function mustNotBeEqualToInTimeZone( if ($this->isEqualToInTimeZone($moment, $timeZone)) { throw $otherwiseThrow !== null ? $otherwiseThrow() - : new Exception\MomentIsEqual(); + : new Exception\MomentIsEqualTo(); } } diff --git a/src/Time.php b/src/Time.php index 65f2d69..2186d9c 100644 --- a/src/Time.php +++ b/src/Time.php @@ -161,6 +161,178 @@ public function distanceInMinutesTo(self $time): int return $diff->h * self::MINUTES_IN_AN_HOUR + $diff->i; } + // -- Guards + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsNotEqualTo + */ + public function mustBeEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsNotEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsEqualTo + */ + public function mustNotBeEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsNotAfter + */ + public function mustBeAfter( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotAfter($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsNotAfter(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsAfter + */ + public function mustNotBeAfter( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isAfter($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsAfter(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsNotAfterOrEqualTo + */ + public function mustBeAfterOrEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotAfterOrEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsNotAfterOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsAfterOrEqualTo + */ + public function mustNotBeAfterOrEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isAfterOrEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsAfterOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsNotBefore + */ + public function mustBeBefore( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotBefore($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsNotBefore(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsBefore + */ + public function mustNotBeBefore( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isBefore($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsBefore(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsNotBeforeOrEqualTo + */ + public function mustBeBeforeOrEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotBeforeOrEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsNotBeforeOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\TimeIsBeforeOrEqualTo + */ + public function mustNotBeBeforeOrEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isBeforeOrEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\TimeIsBeforeOrEqualTo(); + } + } + // -- Mutations public function format(string $format): string diff --git a/tests/Date/MustBeEqualToTest.php b/tests/Date/MustBeEqualToTest.php index 9b8d279..6752b69 100644 --- a/tests/Date/MustBeEqualToTest.php +++ b/tests/Date/MustBeEqualToTest.php @@ -5,8 +5,8 @@ namespace DigitalCraftsman\DateTimePrecision\Date; use DigitalCraftsman\DateTimePrecision\Date; -use DigitalCraftsman\DateTimePrecision\Exception\DateIsNotEqual; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomDateIsNotEqual; +use DigitalCraftsman\DateTimePrecision\Exception\DateIsNotEqualTo; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomDateIsNotEqualTo; use PHPUnit\Framework\TestCase; /** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Date */ @@ -58,16 +58,16 @@ public static function dataProvider(): array null, ], 'default exception' => [ - DateIsNotEqual::class, + DateIsNotEqualTo::class, Date::fromString('2022-10-07'), Date::fromString('2022-10-08'), null, ], 'custom exception' => [ - CustomDateIsNotEqual::class, + CustomDateIsNotEqualTo::class, Date::fromString('2022-10-07'), Date::fromString('2022-10-08'), - static fn () => new CustomDateIsNotEqual(), + static fn () => new CustomDateIsNotEqualTo(), ], ]; } diff --git a/tests/Date/MustNotBeEqualToTest.php b/tests/Date/MustNotBeEqualToTest.php index 8cf3073..feeb1a4 100644 --- a/tests/Date/MustNotBeEqualToTest.php +++ b/tests/Date/MustNotBeEqualToTest.php @@ -5,8 +5,8 @@ namespace DigitalCraftsman\DateTimePrecision\Date; use DigitalCraftsman\DateTimePrecision\Date; -use DigitalCraftsman\DateTimePrecision\Exception\DateIsEqual; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomDateIsEqual; +use DigitalCraftsman\DateTimePrecision\Exception\DateIsEqualTo; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomDateIsEqualTo; use PHPUnit\Framework\TestCase; /** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Date */ @@ -58,16 +58,16 @@ public static function dataProvider(): array null, ], 'default exception' => [ - DateIsEqual::class, + DateIsEqualTo::class, Date::fromString('2022-10-08'), Date::fromString('2022-10-08'), null, ], 'custom exception' => [ - CustomDateIsEqual::class, + CustomDateIsEqualTo::class, Date::fromString('2022-10-08'), Date::fromString('2022-10-08'), - static fn () => new CustomDateIsEqual(), + static fn () => new CustomDateIsEqualTo(), ], ]; } diff --git a/tests/Moment/MustBeAfterInTimeZoneTest.php b/tests/Moment/MustBeAfterInTimeZoneTest.php index 37adf64..319d20b 100644 --- a/tests/Moment/MustBeAfterInTimeZoneTest.php +++ b/tests/Moment/MustBeAfterInTimeZoneTest.php @@ -8,7 +8,7 @@ use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotAfter; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotAfterInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotAfter; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -74,11 +74,11 @@ public static function dataProvider(): array null, ], 'custom exception' => [ - CustomMomentIsNotAfterInTimeZone::class, + CustomMomentIsNotAfter::class, Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('15:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsNotAfterInTimeZone(), + static fn () => new CustomMomentIsNotAfter(), ], ]; } diff --git a/tests/Moment/MustBeAfterOrEqualToInTimeZoneTest.php b/tests/Moment/MustBeAfterOrEqualToInTimeZoneTest.php index 98272a3..656346d 100644 --- a/tests/Moment/MustBeAfterOrEqualToInTimeZoneTest.php +++ b/tests/Moment/MustBeAfterOrEqualToInTimeZoneTest.php @@ -8,7 +8,7 @@ use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotAfterOrEqualTo; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotAfterOrEqualToInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotAfterOrEqualTo; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -74,11 +74,11 @@ public static function dataProvider(): array null, ], 'custom exception' => [ - CustomMomentIsNotAfterOrEqualToInTimeZone::class, + CustomMomentIsNotAfterOrEqualTo::class, Moment::fromStringInTimeZone('2022-10-08 14:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('15:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsNotAfterOrEqualToInTimeZone(), + static fn () => new CustomMomentIsNotAfterOrEqualTo(), ], ]; } diff --git a/tests/Moment/MustBeBeforeInTimeZoneTest.php b/tests/Moment/MustBeBeforeInTimeZoneTest.php index a10cf66..153b10e 100644 --- a/tests/Moment/MustBeBeforeInTimeZoneTest.php +++ b/tests/Moment/MustBeBeforeInTimeZoneTest.php @@ -8,7 +8,7 @@ use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotBefore; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotBeforeInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotBefore; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -74,11 +74,11 @@ public static function dataProvider(): array null, ], 'custom exception' => [ - CustomMomentIsNotBeforeInTimeZone::class, + CustomMomentIsNotBefore::class, Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('15:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsNotBeforeInTimeZone(), + static fn () => new CustomMomentIsNotBefore(), ], ]; } diff --git a/tests/Moment/MustBeBeforeOrEqualToInTimeZoneTest.php b/tests/Moment/MustBeBeforeOrEqualToInTimeZoneTest.php index bf497c7..40d6c37 100644 --- a/tests/Moment/MustBeBeforeOrEqualToInTimeZoneTest.php +++ b/tests/Moment/MustBeBeforeOrEqualToInTimeZoneTest.php @@ -8,7 +8,7 @@ use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotBeforeOrEqualTo; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotBeforeOrEqualToInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotBeforeOrEqualTo; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -74,11 +74,11 @@ public static function dataProvider(): array null, ], 'custom exception' => [ - CustomMomentIsNotBeforeOrEqualToInTimeZone::class, + CustomMomentIsNotBeforeOrEqualTo::class, Moment::fromStringInTimeZone('2022-10-08 16:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('15:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsNotBeforeOrEqualToInTimeZone(), + static fn () => new CustomMomentIsNotBeforeOrEqualTo(), ], ]; } diff --git a/tests/Moment/MustBeEqualToInTimeZoneTest.php b/tests/Moment/MustBeEqualToInTimeZoneTest.php index b7de706..da15779 100644 --- a/tests/Moment/MustBeEqualToInTimeZoneTest.php +++ b/tests/Moment/MustBeEqualToInTimeZoneTest.php @@ -5,10 +5,10 @@ namespace DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Date; -use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotEqual; +use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotEqualTo; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotEqualInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotEqualTo; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -67,18 +67,18 @@ public static function dataProvider(): array null, ], 'default exception' => [ - MomentIsNotEqual::class, + MomentIsNotEqualTo::class, Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('16:00:00'), new \DateTimeZone('Europe/Berlin'), null, ], 'custom exception' => [ - CustomMomentIsNotEqualInTimeZone::class, + CustomMomentIsNotEqualTo::class, Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('16:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsNotEqualInTimeZone(), + static fn () => new CustomMomentIsNotEqualTo(), ], ]; } diff --git a/tests/Moment/MustBeEqualToTest.php b/tests/Moment/MustBeEqualToTest.php index 8b4af9a..6651d96 100644 --- a/tests/Moment/MustBeEqualToTest.php +++ b/tests/Moment/MustBeEqualToTest.php @@ -4,9 +4,9 @@ namespace DigitalCraftsman\DateTimePrecision\Moment; -use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotEqual; +use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotEqualTo; use DigitalCraftsman\DateTimePrecision\Moment; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotEqual; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotEqualTo; use PHPUnit\Framework\TestCase; /** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Moment */ @@ -58,16 +58,16 @@ public static function dataProvider(): array null, ], 'default exception' => [ - MomentIsNotEqual::class, + MomentIsNotEqualTo::class, Moment::fromString('2022-10-08 15:00:00'), Moment::fromString('2023-10-08 16:00:00'), null, ], 'custom exception' => [ - CustomMomentIsNotEqual::class, + CustomMomentIsNotEqualTo::class, Moment::fromString('2022-10-08 15:00:00'), Moment::fromString('2022-11-08 15:00:00'), - static fn () => new CustomMomentIsNotEqual(), + static fn () => new CustomMomentIsNotEqualTo(), ], ]; } diff --git a/tests/Moment/MustNotBeAfterInTimeZoneTest.php b/tests/Moment/MustNotBeAfterInTimeZoneTest.php index b0652bc..ffe9f49 100644 --- a/tests/Moment/MustNotBeAfterInTimeZoneTest.php +++ b/tests/Moment/MustNotBeAfterInTimeZoneTest.php @@ -8,7 +8,7 @@ use DigitalCraftsman\DateTimePrecision\Exception\MomentIsAfter; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsAfterInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsAfter; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -74,11 +74,11 @@ public static function dataProvider(): array null, ], 'custom exception' => [ - CustomMomentIsAfterInTimeZone::class, + CustomMomentIsAfter::class, Moment::fromStringInTimeZone('2022-10-08 16:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('15:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsAfterInTimeZone(), + static fn () => new CustomMomentIsAfter(), ], ]; } diff --git a/tests/Moment/MustNotBeAfterOrEqualToInTimeZoneTest.php b/tests/Moment/MustNotBeAfterOrEqualToInTimeZoneTest.php index 63e5ff4..04a82cc 100644 --- a/tests/Moment/MustNotBeAfterOrEqualToInTimeZoneTest.php +++ b/tests/Moment/MustNotBeAfterOrEqualToInTimeZoneTest.php @@ -8,7 +8,7 @@ use DigitalCraftsman\DateTimePrecision\Exception\MomentIsAfterOrEqualTo; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsAfterOrEqualToInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsAfterOrEqualTo; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -74,11 +74,11 @@ public static function dataProvider(): array null, ], 'custom exception' => [ - CustomMomentIsAfterOrEqualToInTimeZone::class, + CustomMomentIsAfterOrEqualTo::class, Moment::fromStringInTimeZone('2022-10-08 16:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('15:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsAfterOrEqualToInTimeZone(), + static fn () => new CustomMomentIsAfterOrEqualTo(), ], ]; } diff --git a/tests/Moment/MustNotBeBeforeInTimeZoneTest.php b/tests/Moment/MustNotBeBeforeInTimeZoneTest.php index 5f79d7c..f85dc4a 100644 --- a/tests/Moment/MustNotBeBeforeInTimeZoneTest.php +++ b/tests/Moment/MustNotBeBeforeInTimeZoneTest.php @@ -8,7 +8,7 @@ use DigitalCraftsman\DateTimePrecision\Exception\MomentIsBefore; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsBeforeInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsBefore; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -74,11 +74,11 @@ public static function dataProvider(): array null, ], 'custom exception' => [ - CustomMomentIsBeforeInTimeZone::class, + CustomMomentIsBefore::class, Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('16:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsBeforeInTimeZone(), + static fn () => new CustomMomentIsBefore(), ], ]; } diff --git a/tests/Moment/MustNotBeBeforeOrEqualToInTimeZoneTest.php b/tests/Moment/MustNotBeBeforeOrEqualToInTimeZoneTest.php index 61fefd5..553c8de 100644 --- a/tests/Moment/MustNotBeBeforeOrEqualToInTimeZoneTest.php +++ b/tests/Moment/MustNotBeBeforeOrEqualToInTimeZoneTest.php @@ -8,7 +8,7 @@ use DigitalCraftsman\DateTimePrecision\Exception\MomentIsBeforeOrEqualTo; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsBeforeOrEqualToInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsBeforeOrEqualTo; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -74,11 +74,11 @@ public static function dataProvider(): array null, ], 'custom exception' => [ - CustomMomentIsBeforeOrEqualToInTimeZone::class, + CustomMomentIsBeforeOrEqualTo::class, Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('16:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsBeforeOrEqualToInTimeZone(), + static fn () => new CustomMomentIsBeforeOrEqualTo(), ], ]; } diff --git a/tests/Moment/MustNotBeEqualToInTimeZoneTest.php b/tests/Moment/MustNotBeEqualToInTimeZoneTest.php index 72234d2..c066fe6 100644 --- a/tests/Moment/MustNotBeEqualToInTimeZoneTest.php +++ b/tests/Moment/MustNotBeEqualToInTimeZoneTest.php @@ -5,10 +5,10 @@ namespace DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Date; -use DigitalCraftsman\DateTimePrecision\Exception\MomentIsEqual; +use DigitalCraftsman\DateTimePrecision\Exception\MomentIsEqualTo; use DigitalCraftsman\DateTimePrecision\Moment; use DigitalCraftsman\DateTimePrecision\Month; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsEqualInTimeZone; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsEqualTo; use DigitalCraftsman\DateTimePrecision\Time; use DigitalCraftsman\DateTimePrecision\Weekday; use DigitalCraftsman\DateTimePrecision\Year; @@ -67,18 +67,18 @@ public static function dataProvider(): array null, ], 'default exception' => [ - MomentIsEqual::class, + MomentIsEqualTo::class, Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('15:00:00'), new \DateTimeZone('Europe/Berlin'), null, ], 'custom exception' => [ - CustomMomentIsEqualInTimeZone::class, + CustomMomentIsEqualTo::class, Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')), Time::fromString('15:00:00'), new \DateTimeZone('Europe/Berlin'), - static fn () => new CustomMomentIsEqualInTimeZone(), + static fn () => new CustomMomentIsEqualTo(), ], ]; } diff --git a/tests/Moment/MustNotBeEqualToTest.php b/tests/Moment/MustNotBeEqualToTest.php index 1f7a5bd..6aa64fb 100644 --- a/tests/Moment/MustNotBeEqualToTest.php +++ b/tests/Moment/MustNotBeEqualToTest.php @@ -4,9 +4,9 @@ namespace DigitalCraftsman\DateTimePrecision\Moment; -use DigitalCraftsman\DateTimePrecision\Exception\MomentIsEqual; +use DigitalCraftsman\DateTimePrecision\Exception\MomentIsEqualTo; use DigitalCraftsman\DateTimePrecision\Moment; -use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsEqual; +use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsEqualTo; use PHPUnit\Framework\TestCase; /** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Moment */ @@ -58,16 +58,16 @@ public static function dataProvider(): array null, ], 'default exception' => [ - MomentIsEqual::class, + MomentIsEqualTo::class, Moment::fromString('2022-10-08 15:00:00'), Moment::fromString('2022-10-08 15:00:00'), null, ], 'custom exception' => [ - CustomMomentIsEqual::class, + CustomMomentIsEqualTo::class, Moment::fromString('2022-10-08 15:00:00'), Moment::fromString('2022-10-08 15:00:00'), - static fn () => new CustomMomentIsEqual(), + static fn () => new CustomMomentIsEqualTo(), ], ]; } diff --git a/tests/Test/Exception/CustomMomentIsEqual.php b/tests/Test/Exception/CustomDateIsEqualTo.php similarity index 61% rename from tests/Test/Exception/CustomMomentIsEqual.php rename to tests/Test/Exception/CustomDateIsEqualTo.php index 308d230..73baded 100644 --- a/tests/Test/Exception/CustomMomentIsEqual.php +++ b/tests/Test/Exception/CustomDateIsEqualTo.php @@ -4,6 +4,6 @@ namespace DigitalCraftsman\DateTimePrecision\Test\Exception; -final class CustomMomentIsEqual extends \InvalidArgumentException +final class CustomDateIsEqualTo extends \InvalidArgumentException { } diff --git a/tests/Test/Exception/CustomMomentIsNotEqual.php b/tests/Test/Exception/CustomDateIsNotEqualTo.php similarity index 61% rename from tests/Test/Exception/CustomMomentIsNotEqual.php rename to tests/Test/Exception/CustomDateIsNotEqualTo.php index 1d33877..6d317a9 100644 --- a/tests/Test/Exception/CustomMomentIsNotEqual.php +++ b/tests/Test/Exception/CustomDateIsNotEqualTo.php @@ -4,6 +4,6 @@ namespace DigitalCraftsman\DateTimePrecision\Test\Exception; -final class CustomMomentIsNotEqual extends \InvalidArgumentException +final class CustomDateIsNotEqualTo extends \InvalidArgumentException { } diff --git a/tests/Test/Exception/CustomMomentIsAfterInTimeZone.php b/tests/Test/Exception/CustomMomentIsAfterInTimeZone.php deleted file mode 100644 index 3efaba7..0000000 --- a/tests/Test/Exception/CustomMomentIsAfterInTimeZone.php +++ /dev/null @@ -1,9 +0,0 @@ - $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeAfterOrEqualTo + */ + public function must_be_after_or_equal_to_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustBeAfterOrEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('15:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'default exception' => [ + TimeIsNotAfterOrEqualTo::class, + Time::fromString('15:00:00'), + Time::fromString('16:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsNotAfterOrEqualTo::class, + Time::fromString('15:00:00'), + Time::fromString('16:00:00'), + static fn () => new CustomTimeIsNotAfterOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Time/MustBeAfterTest.php b/tests/Time/MustBeAfterTest.php new file mode 100644 index 0000000..cf9c130 --- /dev/null +++ b/tests/Time/MustBeAfterTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeAfter + */ + public function must_be_after_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustBeAfter( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('15:00:00'), + Time::fromString('14:00:00'), + null, + ], + 'default exception' => [ + TimeIsNotAfter::class, + Time::fromString('15:00:00'), + Time::fromString('16:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsNotAfter::class, + Time::fromString('15:00:00'), + Time::fromString('16:00:00'), + static fn () => new CustomTimeIsNotAfter(), + ], + ]; + } +} diff --git a/tests/Time/MustBeBeforeOrEqualToTest.php b/tests/Time/MustBeBeforeOrEqualToTest.php new file mode 100644 index 0000000..43dc432 --- /dev/null +++ b/tests/Time/MustBeBeforeOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeBeforeOrEqualTo + */ + public function must_be_before_or_equal_to_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustBeBeforeOrEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('14:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'default exception' => [ + TimeIsNotBeforeOrEqualTo::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsNotBeforeOrEqualTo::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + static fn () => new CustomTimeIsNotBeforeOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Time/MustBeBeforeTest.php b/tests/Time/MustBeBeforeTest.php new file mode 100644 index 0000000..37079c4 --- /dev/null +++ b/tests/Time/MustBeBeforeTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeBefore + */ + public function must_be_before_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustBeBefore( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('14:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'default exception' => [ + TimeIsNotBefore::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsNotBefore::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + static fn () => new CustomTimeIsNotBefore(), + ], + ]; + } +} diff --git a/tests/Time/MustBeEqualToTest.php b/tests/Time/MustBeEqualToTest.php new file mode 100644 index 0000000..7c34c41 --- /dev/null +++ b/tests/Time/MustBeEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeEqualTo + */ + public function must_be_equal_to_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustBeEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('15:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'default exception' => [ + TimeIsNotEqualTo::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsNotEqualTo::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + static fn () => new CustomTimeIsNotEqualTo(), + ], + ]; + } +} diff --git a/tests/Time/MustNotBeAfterOrEqualToTest.php b/tests/Time/MustNotBeAfterOrEqualToTest.php new file mode 100644 index 0000000..9165a23 --- /dev/null +++ b/tests/Time/MustNotBeAfterOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeAfterOrEqualTo + */ + public function must_not_be_after_or_equal_to_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustNotBeAfterOrEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('14:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'default exception' => [ + TimeIsAfterOrEqualTo::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsAfterOrEqualTo::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + static fn () => new CustomTimeIsAfterOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Time/MustNotBeAfterTest.php b/tests/Time/MustNotBeAfterTest.php new file mode 100644 index 0000000..a5c15b1 --- /dev/null +++ b/tests/Time/MustNotBeAfterTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeAfter + */ + public function must_not_be_after_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustNotBeAfter( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('14:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'default exception' => [ + TimeIsAfter::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsAfter::class, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + static fn () => new CustomTimeIsAfter(), + ], + ]; + } +} diff --git a/tests/Time/MustNotBeBeforeOrEqualToTest.php b/tests/Time/MustNotBeBeforeOrEqualToTest.php new file mode 100644 index 0000000..7556d81 --- /dev/null +++ b/tests/Time/MustNotBeBeforeOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeBeforeOrEqualTo + */ + public function must_not_be_before_or_equal_to_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustNotBeBeforeOrEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('15:00:00'), + Time::fromString('14:00:00'), + null, + ], + 'default exception' => [ + TimeIsBeforeOrEqualTo::class, + Time::fromString('15:00:00'), + Time::fromString('16:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsBeforeOrEqualTo::class, + Time::fromString('15:00:00'), + Time::fromString('16:00:00'), + static fn () => new CustomTimeIsBeforeOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Time/MustNotBeBeforeTest.php b/tests/Time/MustNotBeBeforeTest.php new file mode 100644 index 0000000..260a0bb --- /dev/null +++ b/tests/Time/MustNotBeBeforeTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeBefore + */ + public function must_not_be_before_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustNotBeBefore( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('15:00:00'), + Time::fromString('14:00:00'), + null, + ], + 'default exception' => [ + TimeIsBefore::class, + Time::fromString('15:00:00'), + Time::fromString('16:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsBefore::class, + Time::fromString('15:00:00'), + Time::fromString('16:00:00'), + static fn () => new CustomTimeIsBefore(), + ], + ]; + } +} diff --git a/tests/Time/MustNotBeEqualToTest.php b/tests/Time/MustNotBeEqualToTest.php new file mode 100644 index 0000000..33342b9 --- /dev/null +++ b/tests/Time/MustNotBeEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeEqualTo + */ + public function must_not_be_equal_to_works( + ?string $expectedResult, + Time $date, + Time $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $date->mustNotBeEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Time::fromString('16:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'default exception' => [ + TimeIsEqualTo::class, + Time::fromString('15:00:00'), + Time::fromString('15:00:00'), + null, + ], + 'custom exception' => [ + CustomTimeIsEqualTo::class, + Time::fromString('15:00:00'), + Time::fromString('15:00:00'), + static fn () => new CustomTimeIsEqualTo(), + ], + ]; + } +}