diff --git a/CHANGELOG.md b/CHANGELOG.md index 65e6a72..bfe3338 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` like `mustBeEqualTo` with option to throw custom exception. +- Added guard methods for comparison methods to `Moment` and `Date` like `mustBeEqualTo` with option to throw custom exception. ## 0.10.0 diff --git a/src/Date.php b/src/Date.php index 4219ccb..bfaeb08 100644 --- a/src/Date.php +++ b/src/Date.php @@ -49,14 +49,14 @@ public static function fromString(string $date): self } } - // Stringable + // -- Stringable public function __toString(): string { return $this->format(self::DATE_FORMAT); } - // Accessors + // -- Accessors public function isEqualTo(self $date): bool { @@ -156,7 +156,179 @@ public function weekday(): Weekday return Weekday::fromDateTime($this->toDateTimeImmutable()); } - // Mutations + // -- Guards + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsNotEqual + */ + public function mustBeEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsNotEqual(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsEqual + */ + public function mustNotBeEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsEqual(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsNotAfter + */ + public function mustBeAfter( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotAfter($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsNotAfter(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsAfter + */ + public function mustNotBeAfter( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isAfter($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsAfter(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsNotAfterOrEqualTo + */ + public function mustBeAfterOrEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotAfterOrEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsNotAfterOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsAfterOrEqualTo + */ + public function mustNotBeAfterOrEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isAfterOrEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsAfterOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsNotBefore + */ + public function mustBeBefore( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotBefore($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsNotBefore(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsBefore + */ + public function mustNotBeBefore( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isBefore($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsBefore(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsNotBeforeOrEqualTo + */ + public function mustBeBeforeOrEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotBeforeOrEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsNotBeforeOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\DateIsBeforeOrEqualTo + */ + public function mustNotBeBeforeOrEqualTo( + self $date, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isBeforeOrEqualTo($date)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\DateIsBeforeOrEqualTo(); + } + } + + // -- Mutations public function format(string $format): string { diff --git a/src/Exception/DateIsAfter.php b/src/Exception/DateIsAfter.php new file mode 100644 index 0000000..0915f0c --- /dev/null +++ b/src/Exception/DateIsAfter.php @@ -0,0 +1,18 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeAfterOrEqualTo + */ + public function must_be_after_or_equal_to_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-08'), + null, + ], + 'default exception' => [ + DateIsNotAfterOrEqualTo::class, + Date::fromString('2022-10-07'), + Date::fromString('2022-10-08'), + null, + ], + 'custom exception' => [ + CustomDateIsNotAfterOrEqualTo::class, + Date::fromString('2022-10-07'), + Date::fromString('2022-10-08'), + static fn () => new CustomDateIsNotAfterOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Date/MustBeAfterTest.php b/tests/Date/MustBeAfterTest.php new file mode 100644 index 0000000..32c9b13 --- /dev/null +++ b/tests/Date/MustBeAfterTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeAfter + */ + public function must_be_after_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-09'), + Date::fromString('2022-10-08'), + null, + ], + 'default exception' => [ + DateIsNotAfter::class, + Date::fromString('2022-10-07'), + Date::fromString('2022-10-08'), + null, + ], + 'custom exception' => [ + CustomDateIsNotAfter::class, + Date::fromString('2022-10-07'), + Date::fromString('2022-10-08'), + static fn () => new CustomDateIsNotAfter(), + ], + ]; + } +} diff --git a/tests/Date/MustBeBeforeOrEqualToTest.php b/tests/Date/MustBeBeforeOrEqualToTest.php new file mode 100644 index 0000000..55845c1 --- /dev/null +++ b/tests/Date/MustBeBeforeOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeBeforeOrEqualTo + */ + public function must_be_before_or_equal_to_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-09'), + null, + ], + 'default exception' => [ + DateIsNotBeforeOrEqualTo::class, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-07'), + null, + ], + 'custom exception' => [ + CustomDateIsNotBeforeOrEqualTo::class, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-07'), + static fn () => new CustomDateIsNotBeforeOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Date/MustBeBeforeTest.php b/tests/Date/MustBeBeforeTest.php new file mode 100644 index 0000000..bfc2dea --- /dev/null +++ b/tests/Date/MustBeBeforeTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeBefore + */ + public function must_be_before_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-09'), + null, + ], + 'default exception' => [ + DateIsNotBefore::class, + Date::fromString('2022-10-09'), + Date::fromString('2022-10-08'), + null, + ], + 'custom exception' => [ + CustomDateIsNotBefore::class, + Date::fromString('2022-10-09'), + Date::fromString('2022-10-08'), + static fn () => new CustomDateIsNotBefore(), + ], + ]; + } +} diff --git a/tests/Date/MustBeEqualToTest.php b/tests/Date/MustBeEqualToTest.php new file mode 100644 index 0000000..9b8d279 --- /dev/null +++ b/tests/Date/MustBeEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeEqualTo + */ + public function must_be_equal_to_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-08'), + null, + ], + 'default exception' => [ + DateIsNotEqual::class, + Date::fromString('2022-10-07'), + Date::fromString('2022-10-08'), + null, + ], + 'custom exception' => [ + CustomDateIsNotEqual::class, + Date::fromString('2022-10-07'), + Date::fromString('2022-10-08'), + static fn () => new CustomDateIsNotEqual(), + ], + ]; + } +} diff --git a/tests/Date/MustNotBeAfterOrEqualToTest.php b/tests/Date/MustNotBeAfterOrEqualToTest.php new file mode 100644 index 0000000..bbf5f03 --- /dev/null +++ b/tests/Date/MustNotBeAfterOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeAfterOrEqualTo + */ + public function must_not_be_after_or_equal_to_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-09'), + null, + ], + 'default exception' => [ + DateIsAfterOrEqualTo::class, + Date::fromString('2022-10-09'), + Date::fromString('2022-10-08'), + null, + ], + 'custom exception' => [ + CustomDateIsAfterOrEqualTo::class, + Date::fromString('2022-10-09'), + Date::fromString('2022-10-08'), + static fn () => new CustomDateIsAfterOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Date/MustNotBeAfterTest.php b/tests/Date/MustNotBeAfterTest.php new file mode 100644 index 0000000..ddd5970 --- /dev/null +++ b/tests/Date/MustNotBeAfterTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeAfter + */ + public function must_not_be_after_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-09'), + null, + ], + 'default exception' => [ + DateIsAfter::class, + Date::fromString('2022-10-09'), + Date::fromString('2022-10-08'), + null, + ], + 'custom exception' => [ + CustomDateIsAfter::class, + Date::fromString('2022-10-09'), + Date::fromString('2022-10-08'), + static fn () => new CustomDateIsAfter(), + ], + ]; + } +} diff --git a/tests/Date/MustNotBeBeforeOrEqualToTest.php b/tests/Date/MustNotBeBeforeOrEqualToTest.php new file mode 100644 index 0000000..a5db78c --- /dev/null +++ b/tests/Date/MustNotBeBeforeOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeBeforeOrEqualTo + */ + public function must_not_be_before_or_equal_to_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-09'), + Date::fromString('2022-10-08'), + null, + ], + 'default exception' => [ + DateIsBeforeOrEqualTo::class, + Date::fromString('2022-10-07'), + Date::fromString('2022-10-08'), + null, + ], + 'custom exception' => [ + CustomDateIsBeforeOrEqualTo::class, + Date::fromString('2022-10-07'), + Date::fromString('2022-10-08'), + static fn () => new CustomDateIsBeforeOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Date/MustNotBeBeforeTest.php b/tests/Date/MustNotBeBeforeTest.php new file mode 100644 index 0000000..064cb16 --- /dev/null +++ b/tests/Date/MustNotBeBeforeTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeBefore + */ + public function must_not_be_before_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-09'), + Date::fromString('2022-10-08'), + null, + ], + 'default exception' => [ + DateIsBefore::class, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-09'), + null, + ], + 'custom exception' => [ + CustomDateIsBefore::class, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-09'), + static fn () => new CustomDateIsBefore(), + ], + ]; + } +} diff --git a/tests/Date/MustNotBeEqualToTest.php b/tests/Date/MustNotBeEqualToTest.php new file mode 100644 index 0000000..8cf3073 --- /dev/null +++ b/tests/Date/MustNotBeEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeEqualTo + */ + public function must_not_be_equal_to_works( + ?string $expectedResult, + Date $date, + Date $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, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-09'), + null, + ], + 'default exception' => [ + DateIsEqual::class, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-08'), + null, + ], + 'custom exception' => [ + CustomDateIsEqual::class, + Date::fromString('2022-10-08'), + Date::fromString('2022-10-08'), + static fn () => new CustomDateIsEqual(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomDateIsAfter.php b/tests/Test/Exception/CustomDateIsAfter.php new file mode 100644 index 0000000..b3bf30c --- /dev/null +++ b/tests/Test/Exception/CustomDateIsAfter.php @@ -0,0 +1,9 @@ +