From 0089e82cf754638c31f9a8bcf0db337621f2444f Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Mon, 17 Jun 2024 21:17:24 +0200 Subject: [PATCH] Added mustBeBeforeOrEqualTo and mustBeBeforeOrEqualToInTimeZone --- src/Exception/MomentIsNotBeforeOrEqualTo.php | 18 ++++ src/Moment.php | 35 ++++++++ .../MustBeBeforeOrEqualToInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustBeBeforeOrEqualToTest.php | 74 ++++++++++++++++ .../CustomMomentIsNotBeforeOrEqualTo.php | 9 ++ ...omMomentIsNotBeforeOrEqualToInTimeZone.php | 9 ++ 6 files changed, 230 insertions(+) create mode 100644 src/Exception/MomentIsNotBeforeOrEqualTo.php create mode 100644 tests/Moment/MustBeBeforeOrEqualToInTimeZoneTest.php create mode 100644 tests/Moment/MustBeBeforeOrEqualToTest.php create mode 100644 tests/Test/Exception/CustomMomentIsNotBeforeOrEqualTo.php create mode 100644 tests/Test/Exception/CustomMomentIsNotBeforeOrEqualToInTimeZone.php diff --git a/src/Exception/MomentIsNotBeforeOrEqualTo.php b/src/Exception/MomentIsNotBeforeOrEqualTo.php new file mode 100644 index 0000000..78a6a9a --- /dev/null +++ b/src/Exception/MomentIsNotBeforeOrEqualTo.php @@ -0,0 +1,18 @@ +isNotBeforeOrEqualTo($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotBeforeOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsNotBeforeOrEqualTo + */ + public function mustBeBeforeOrEqualToInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotBeforeOrEqualToInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotBeforeOrEqualTo(); + } + } } diff --git a/tests/Moment/MustBeBeforeOrEqualToInTimeZoneTest.php b/tests/Moment/MustBeBeforeOrEqualToInTimeZoneTest.php new file mode 100644 index 0000000..bf497c7 --- /dev/null +++ b/tests/Moment/MustBeBeforeOrEqualToInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeBeforeOrEqualToInTimeZone + */ + public function must_be_before_or_equal_to_in_time_zone_works( + ?string $expectedResult, + Moment $moment, + Time | Weekday | Date | Month | Year $comparator, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $moment->mustBeBeforeOrEqualToInTimeZone( + $comparator, + $timeZone, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromStringInTimeZone('2022-10-08 15:00:00', new \DateTimeZone('Europe/Berlin')), + Time::fromString('16:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'default exception' => [ + MomentIsNotBeforeOrEqualTo::class, + Moment::fromStringInTimeZone('2022-10-08 16:00:00', new \DateTimeZone('Europe/Berlin')), + Time::fromString('15:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'custom exception' => [ + CustomMomentIsNotBeforeOrEqualToInTimeZone::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(), + ], + ]; + } +} diff --git a/tests/Moment/MustBeBeforeOrEqualToTest.php b/tests/Moment/MustBeBeforeOrEqualToTest.php new file mode 100644 index 0000000..9456f46 --- /dev/null +++ b/tests/Moment/MustBeBeforeOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeBeforeOrEqualTo + */ + public function must_be_before_or_equal_to_works( + ?string $expectedResult, + Moment $moment, + Moment $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $moment->mustBeBeforeOrEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-10-08 16:00:00'), + null, + ], + 'default exception' => [ + MomentIsNotBeforeOrEqualTo::class, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsNotBeforeOrEqualTo::class, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + static fn () => new CustomMomentIsNotBeforeOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsNotBeforeOrEqualTo.php b/tests/Test/Exception/CustomMomentIsNotBeforeOrEqualTo.php new file mode 100644 index 0000000..b0f8663 --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsNotBeforeOrEqualTo.php @@ -0,0 +1,9 @@ +