From cb3a78bfcf809174c5ecac185d61ca9058da7a33 Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Tue, 11 Jun 2024 21:53:44 +0200 Subject: [PATCH 01/14] Add Moment::mustBeEqualTo guard --- .php-cs-fixer.dist.php | 3 + src/Exception/MomentIsNotEqual.php | 18 ++++ src/Moment.php | 37 ++++++++ tests/Moment/MustBeEqualToInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustBeEqualToTest.php | 74 ++++++++++++++++ .../Test/Exception/CustomMomentIsNotEqual.php | 9 ++ .../CustomMomentIsNotEqualInTimeZone.php | 9 ++ 7 files changed, 235 insertions(+) create mode 100644 src/Exception/MomentIsNotEqual.php create mode 100644 tests/Moment/MustBeEqualToInTimeZoneTest.php create mode 100644 tests/Moment/MustBeEqualToTest.php create mode 100644 tests/Test/Exception/CustomMomentIsNotEqual.php create mode 100644 tests/Test/Exception/CustomMomentIsNotEqualInTimeZone.php diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 6fb2528..d93c4e4 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -41,5 +41,8 @@ // Nullable types should be explicit even with default values 'nullable_type_declaration_for_default_null_value' => false, + + // Throw in a single line is worse to read when using ternary operator + 'single_line_throw' => false, ]) ->setFinder($finder); diff --git a/src/Exception/MomentIsNotEqual.php b/src/Exception/MomentIsNotEqual.php new file mode 100644 index 0000000..5bf1bd9 --- /dev/null +++ b/src/Exception/MomentIsNotEqual.php @@ -0,0 +1,18 @@ +midnight() ->toTimeZone($originalTimeZone); } + + // -- Guards + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsNotEqual + */ + public function mustBeEqualTo( + self $moment, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotEqualTo($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotEqual(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsNotEqual + */ + public function mustBeEqualToInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotEqualToInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotEqual(); + } + } } diff --git a/tests/Moment/MustBeEqualToInTimeZoneTest.php b/tests/Moment/MustBeEqualToInTimeZoneTest.php new file mode 100644 index 0000000..b7de706 --- /dev/null +++ b/tests/Moment/MustBeEqualToInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeEqualToInTimeZone + */ + public function must_be_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->mustBeEqualToInTimeZone( + $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('15:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'default exception' => [ + MomentIsNotEqual::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, + 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(), + ], + ]; + } +} diff --git a/tests/Moment/MustBeEqualToTest.php b/tests/Moment/MustBeEqualToTest.php new file mode 100644 index 0000000..8b4af9a --- /dev/null +++ b/tests/Moment/MustBeEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeEqualTo + */ + public function must_be_equal_to_works( + ?string $expectedResult, + Moment $moment, + Moment $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $moment->mustBeEqualTo( + $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 15:00:00'), + null, + ], + 'default exception' => [ + MomentIsNotEqual::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2023-10-08 16:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsNotEqual::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-11-08 15:00:00'), + static fn () => new CustomMomentIsNotEqual(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsNotEqual.php b/tests/Test/Exception/CustomMomentIsNotEqual.php new file mode 100644 index 0000000..1d33877 --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsNotEqual.php @@ -0,0 +1,9 @@ + Date: Thu, 13 Jun 2024 14:17:02 +0200 Subject: [PATCH 02/14] Added mustNotBeEqualTo and mustNotBeEqualToInTimeZone --- src/Exception/MomentIsEqual.php | 18 ++++ src/Exception/MomentIsNotEqual.php | 2 +- src/Moment.php | 35 ++++++++ .../Moment/MustNotBeEqualToInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustNotBeEqualToTest.php | 74 ++++++++++++++++ tests/Test/Exception/CustomMomentIsEqual.php | 9 ++ .../CustomMomentIsEqualInTimeZone.php | 9 ++ 7 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 src/Exception/MomentIsEqual.php create mode 100644 tests/Moment/MustNotBeEqualToInTimeZoneTest.php create mode 100644 tests/Moment/MustNotBeEqualToTest.php create mode 100644 tests/Test/Exception/CustomMomentIsEqual.php create mode 100644 tests/Test/Exception/CustomMomentIsEqualInTimeZone.php diff --git a/src/Exception/MomentIsEqual.php b/src/Exception/MomentIsEqual.php new file mode 100644 index 0000000..a76ae0e --- /dev/null +++ b/src/Exception/MomentIsEqual.php @@ -0,0 +1,18 @@ +isEqualTo($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsEqual(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsEqual + */ + public function mustNotBeEqualToInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isEqualToInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsEqual(); + } + } } diff --git a/tests/Moment/MustNotBeEqualToInTimeZoneTest.php b/tests/Moment/MustNotBeEqualToInTimeZoneTest.php new file mode 100644 index 0000000..72234d2 --- /dev/null +++ b/tests/Moment/MustNotBeEqualToInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeEqualToInTimeZone + */ + public function must_not_be_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->mustNotBeEqualToInTimeZone( + $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' => [ + MomentIsEqual::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, + 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(), + ], + ]; + } +} diff --git a/tests/Moment/MustNotBeEqualToTest.php b/tests/Moment/MustNotBeEqualToTest.php new file mode 100644 index 0000000..1f7a5bd --- /dev/null +++ b/tests/Moment/MustNotBeEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeEqualTo + */ + public function must_not_be_equal_to_works( + ?string $expectedResult, + Moment $moment, + Moment $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $moment->mustNotBeEqualTo( + $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' => [ + MomentIsEqual::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsEqual::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + static fn () => new CustomMomentIsEqual(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsEqual.php b/tests/Test/Exception/CustomMomentIsEqual.php new file mode 100644 index 0000000..308d230 --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsEqual.php @@ -0,0 +1,9 @@ + Date: Thu, 13 Jun 2024 15:07:49 +0200 Subject: [PATCH 03/14] Add mustBeAfter and mustBeAfterInTimeZone --- src/Exception/MomentIsNotAfter.php | 18 ++++ src/Moment.php | 35 ++++++++ tests/Moment/MustBeAfterInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustBeAfterTest.php | 74 ++++++++++++++++ .../Test/Exception/CustomMomentIsNotAfter.php | 9 ++ .../CustomMomentIsNotAfterInTimeZone.php | 9 ++ 6 files changed, 230 insertions(+) create mode 100644 src/Exception/MomentIsNotAfter.php create mode 100644 tests/Moment/MustBeAfterInTimeZoneTest.php create mode 100644 tests/Moment/MustBeAfterTest.php create mode 100644 tests/Test/Exception/CustomMomentIsNotAfter.php create mode 100644 tests/Test/Exception/CustomMomentIsNotAfterInTimeZone.php diff --git a/src/Exception/MomentIsNotAfter.php b/src/Exception/MomentIsNotAfter.php new file mode 100644 index 0000000..2656d2d --- /dev/null +++ b/src/Exception/MomentIsNotAfter.php @@ -0,0 +1,18 @@ +isNotAfter($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotAfter(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsNotAfter + */ + public function mustBeAfterInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotAfterInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotAfter(); + } + } } diff --git a/tests/Moment/MustBeAfterInTimeZoneTest.php b/tests/Moment/MustBeAfterInTimeZoneTest.php new file mode 100644 index 0000000..37adf64 --- /dev/null +++ b/tests/Moment/MustBeAfterInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeAfterInTimeZone + */ + public function must_be_after_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->mustBeAfterInTimeZone( + $comparator, + $timeZone, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromStringInTimeZone('2022-10-08 16:00:00', new \DateTimeZone('Europe/Berlin')), + Time::fromString('15:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'default exception' => [ + MomentIsNotAfter::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' => [ + CustomMomentIsNotAfterInTimeZone::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(), + ], + ]; + } +} diff --git a/tests/Moment/MustBeAfterTest.php b/tests/Moment/MustBeAfterTest.php new file mode 100644 index 0000000..88f6338 --- /dev/null +++ b/tests/Moment/MustBeAfterTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeAfter + */ + public function must_be_after_works( + ?string $expectedResult, + Moment $moment, + Moment $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $moment->mustBeAfter( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + null, + ], + 'default exception' => [ + MomentIsNotAfter::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2023-10-08 15:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsNotAfter::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + static fn () => new CustomMomentIsNotAfter(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsNotAfter.php b/tests/Test/Exception/CustomMomentIsNotAfter.php new file mode 100644 index 0000000..dec686c --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsNotAfter.php @@ -0,0 +1,9 @@ + Date: Thu, 13 Jun 2024 15:11:29 +0200 Subject: [PATCH 04/14] Update upload action --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8f4aed6..bd96f04 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,7 +39,7 @@ jobs: run: make php-8.2-tests-ci - name: Upload to Codecov - uses: codecov/codecov-action@v2 + uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODE_COV_TOKEN }} files: ./coverage.xml From 55be9da90c2557a19a7ed3a5f57b56aabc2b5ea1 Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Thu, 13 Jun 2024 15:16:10 +0200 Subject: [PATCH 05/14] Fix token name for upload --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bd96f04..4e7b3cd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,8 +41,9 @@ jobs: - name: Upload to Codecov uses: codecov/codecov-action@v4 with: - token: ${{ secrets.CODE_COV_TOKEN }} + fail_ci_if_error: true files: ./coverage.xml + token: ${{ secrets.CODECOV_TOKEN }} verbose: true test-8-3: From bc2d185a76f9bb16f4062f7db8fcec70b7cdedaa Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Thu, 13 Jun 2024 17:19:28 +0200 Subject: [PATCH 06/14] Add mustNotBeAfter and mustNotBeAfterInTimeZone --- src/Exception/MomentIsAfter.php | 18 ++++ src/Moment.php | 35 ++++++++ tests/Moment/MustNotBeAfterInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustNotBeAfterTest.php | 74 ++++++++++++++++ tests/Test/Exception/CustomMomentIsAfter.php | 9 ++ .../CustomMomentIsAfterInTimeZone.php | 9 ++ 6 files changed, 230 insertions(+) create mode 100644 src/Exception/MomentIsAfter.php create mode 100644 tests/Moment/MustNotBeAfterInTimeZoneTest.php create mode 100644 tests/Moment/MustNotBeAfterTest.php create mode 100644 tests/Test/Exception/CustomMomentIsAfter.php create mode 100644 tests/Test/Exception/CustomMomentIsAfterInTimeZone.php diff --git a/src/Exception/MomentIsAfter.php b/src/Exception/MomentIsAfter.php new file mode 100644 index 0000000..f6dc394 --- /dev/null +++ b/src/Exception/MomentIsAfter.php @@ -0,0 +1,18 @@ +isAfter($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsAfter(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsAfter + */ + public function mustNotBeAfterInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isAfterInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsAfter(); + } + } } diff --git a/tests/Moment/MustNotBeAfterInTimeZoneTest.php b/tests/Moment/MustNotBeAfterInTimeZoneTest.php new file mode 100644 index 0000000..b0652bc --- /dev/null +++ b/tests/Moment/MustNotBeAfterInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeAfterInTimeZone + */ + public function must_not_be_after_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->mustNotBeAfterInTimeZone( + $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('15:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'default exception' => [ + MomentIsAfter::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' => [ + CustomMomentIsAfterInTimeZone::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(), + ], + ]; + } +} diff --git a/tests/Moment/MustNotBeAfterTest.php b/tests/Moment/MustNotBeAfterTest.php new file mode 100644 index 0000000..ec5aa74 --- /dev/null +++ b/tests/Moment/MustNotBeAfterTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeAfter + */ + public function must_not_be_after_works( + ?string $expectedResult, + Moment $moment, + Moment $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $moment->mustNotBeAfter( + $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 15:00:00'), + null, + ], + 'default exception' => [ + MomentIsAfter::class, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsAfter::class, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + static fn () => new CustomMomentIsAfter(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsAfter.php b/tests/Test/Exception/CustomMomentIsAfter.php new file mode 100644 index 0000000..e81eb72 --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsAfter.php @@ -0,0 +1,9 @@ + Date: Thu, 13 Jun 2024 17:23:30 +0200 Subject: [PATCH 07/14] Add hint about amount of tests --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c4c692d..1be3539 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Additionally, the package provides a streamlined way to have the system running This Symfony bundle includes Symfony normalizers for automatic normalization and denormalization and Doctrine types to store the objects directly in the database. -As it's a central part of an application, it's tested thoroughly (including mutation testing). +As it's a central part of an application, it's tested thoroughly (including mutation testing). Currently, more than 80% of the lines of code in this repository are tests. [![Latest Stable Version](http://poser.pugx.org/digital-craftsman/date-time-precision/v)](https://packagist.org/packages/digital-craftsman/date-time-precision) [![PHP Version Require](http://poser.pugx.org/digital-craftsman/date-time-precision/require/php)](https://packagist.org/packages/digital-craftsman/date-time-precision) From ccbc7afd4026377524bb5e842503e6be3e80937a Mon Sep 17 00:00:00 2001 From: Christian Kolb Date: Fri, 14 Jun 2024 08:46:51 +0200 Subject: [PATCH 08/14] Added mustBeAfterOrEqualTo and mustBeAfterOrEqualToInTimeZone --- src/Exception/MomentIsNotAfterOrEqualTo.php | 18 ++++ src/Moment.php | 35 ++++++++ .../MustBeAfterOrEqualToInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustBeAfterOrEqualToTest.php | 74 ++++++++++++++++ .../CustomMomentIsNotAfterOrEqualTo.php | 9 ++ ...tomMomentIsNotAfterOrEqualToInTimeZone.php | 9 ++ 6 files changed, 230 insertions(+) create mode 100644 src/Exception/MomentIsNotAfterOrEqualTo.php create mode 100644 tests/Moment/MustBeAfterOrEqualToInTimeZoneTest.php create mode 100644 tests/Moment/MustBeAfterOrEqualToTest.php create mode 100644 tests/Test/Exception/CustomMomentIsNotAfterOrEqualTo.php create mode 100644 tests/Test/Exception/CustomMomentIsNotAfterOrEqualToInTimeZone.php diff --git a/src/Exception/MomentIsNotAfterOrEqualTo.php b/src/Exception/MomentIsNotAfterOrEqualTo.php new file mode 100644 index 0000000..bc9c7e3 --- /dev/null +++ b/src/Exception/MomentIsNotAfterOrEqualTo.php @@ -0,0 +1,18 @@ +isNotAfterOrEqualTo($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotAfterOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsNotAfterOrEqualTo + */ + public function mustBeAfterOrEqualToInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotAfterOrEqualToInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotAfterOrEqualTo(); + } + } } diff --git a/tests/Moment/MustBeAfterOrEqualToInTimeZoneTest.php b/tests/Moment/MustBeAfterOrEqualToInTimeZoneTest.php new file mode 100644 index 0000000..98272a3 --- /dev/null +++ b/tests/Moment/MustBeAfterOrEqualToInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeAfterOrEqualToInTimeZone + */ + public function must_be_after_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->mustBeAfterOrEqualToInTimeZone( + $comparator, + $timeZone, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromStringInTimeZone('2022-10-08 16:00:00', new \DateTimeZone('Europe/Berlin')), + Time::fromString('15:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'default exception' => [ + MomentIsNotAfterOrEqualTo::class, + Moment::fromStringInTimeZone('2022-10-08 14:00:00', new \DateTimeZone('Europe/Berlin')), + Time::fromString('15:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'custom exception' => [ + CustomMomentIsNotAfterOrEqualToInTimeZone::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(), + ], + ]; + } +} diff --git a/tests/Moment/MustBeAfterOrEqualToTest.php b/tests/Moment/MustBeAfterOrEqualToTest.php new file mode 100644 index 0000000..63bb4ac --- /dev/null +++ b/tests/Moment/MustBeAfterOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeAfterOrEqualTo + */ + public function must_be_after_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->mustBeAfterOrEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + null, + ], + 'default exception' => [ + MomentIsNotAfterOrEqualTo::class, + Moment::fromString('2022-10-08 14:00:00'), + Moment::fromString('2023-10-08 15:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsNotAfterOrEqualTo::class, + Moment::fromString('2022-10-08 14:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + static fn () => new CustomMomentIsNotAfterOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsNotAfterOrEqualTo.php b/tests/Test/Exception/CustomMomentIsNotAfterOrEqualTo.php new file mode 100644 index 0000000..01a1754 --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsNotAfterOrEqualTo.php @@ -0,0 +1,9 @@ + Date: Fri, 14 Jun 2024 13:09:29 +0200 Subject: [PATCH 09/14] Added mustNotBeAfterOrEqualTo and mustNotBeAfterOrEqualToInTimeZone --- src/Exception/MomentIsAfterOrEqualTo.php | 18 ++++ src/Moment.php | 35 ++++++++ .../MustNotBeAfterOrEqualToInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustNotBeAfterOrEqualToTest.php | 74 ++++++++++++++++ .../CustomMomentIsAfterOrEqualTo.php | 9 ++ ...CustomMomentIsAfterOrEqualToInTimeZone.php | 9 ++ 6 files changed, 230 insertions(+) create mode 100644 src/Exception/MomentIsAfterOrEqualTo.php create mode 100644 tests/Moment/MustNotBeAfterOrEqualToInTimeZoneTest.php create mode 100644 tests/Moment/MustNotBeAfterOrEqualToTest.php create mode 100644 tests/Test/Exception/CustomMomentIsAfterOrEqualTo.php create mode 100644 tests/Test/Exception/CustomMomentIsAfterOrEqualToInTimeZone.php diff --git a/src/Exception/MomentIsAfterOrEqualTo.php b/src/Exception/MomentIsAfterOrEqualTo.php new file mode 100644 index 0000000..37f8387 --- /dev/null +++ b/src/Exception/MomentIsAfterOrEqualTo.php @@ -0,0 +1,18 @@ +isAfterOrEqualTo($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsAfterOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsAfterOrEqualTo + */ + public function mustNotBeAfterOrEqualToInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isAfterOrEqualToInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsAfterOrEqualTo(); + } + } } diff --git a/tests/Moment/MustNotBeAfterOrEqualToInTimeZoneTest.php b/tests/Moment/MustNotBeAfterOrEqualToInTimeZoneTest.php new file mode 100644 index 0000000..63e5ff4 --- /dev/null +++ b/tests/Moment/MustNotBeAfterOrEqualToInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeAfterOrEqualToInTimeZone + */ + public function must_not_be_after_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->mustNotBeAfterOrEqualToInTimeZone( + $comparator, + $timeZone, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromStringInTimeZone('2022-10-08 14:00:00', new \DateTimeZone('Europe/Berlin')), + Time::fromString('15:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'default exception' => [ + MomentIsAfterOrEqualTo::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' => [ + CustomMomentIsAfterOrEqualToInTimeZone::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(), + ], + ]; + } +} diff --git a/tests/Moment/MustNotBeAfterOrEqualToTest.php b/tests/Moment/MustNotBeAfterOrEqualToTest.php new file mode 100644 index 0000000..c93d86f --- /dev/null +++ b/tests/Moment/MustNotBeAfterOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeAfterOrEqualTo + */ + public function must_not_be_after_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->mustNotBeAfterOrEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromString('2022-10-08 14:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + null, + ], + 'default exception' => [ + MomentIsAfterOrEqualTo::class, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsAfterOrEqualTo::class, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + static fn () => new CustomMomentIsAfterOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsAfterOrEqualTo.php b/tests/Test/Exception/CustomMomentIsAfterOrEqualTo.php new file mode 100644 index 0000000..4a0d825 --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsAfterOrEqualTo.php @@ -0,0 +1,9 @@ + Date: Mon, 17 Jun 2024 20:56:16 +0200 Subject: [PATCH 10/14] Add mustBeBefore and mustBeBeforeInTimeZone --- src/Exception/MomentIsNotBefore.php | 18 ++++ src/Moment.php | 35 ++++++++ tests/Moment/MustBeBeforeInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustBeBeforeTest.php | 74 ++++++++++++++++ .../Exception/CustomMomentIsNotBefore.php | 9 ++ .../CustomMomentIsNotBeforeInTimeZone.php | 9 ++ 6 files changed, 230 insertions(+) create mode 100644 src/Exception/MomentIsNotBefore.php create mode 100644 tests/Moment/MustBeBeforeInTimeZoneTest.php create mode 100644 tests/Moment/MustBeBeforeTest.php create mode 100644 tests/Test/Exception/CustomMomentIsNotBefore.php create mode 100644 tests/Test/Exception/CustomMomentIsNotBeforeInTimeZone.php diff --git a/src/Exception/MomentIsNotBefore.php b/src/Exception/MomentIsNotBefore.php new file mode 100644 index 0000000..99357c2 --- /dev/null +++ b/src/Exception/MomentIsNotBefore.php @@ -0,0 +1,18 @@ +isNotBefore($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotBefore(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsNotBefore + */ + public function mustBeBeforeInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isNotBeforeInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsNotBefore(); + } + } } diff --git a/tests/Moment/MustBeBeforeInTimeZoneTest.php b/tests/Moment/MustBeBeforeInTimeZoneTest.php new file mode 100644 index 0000000..a10cf66 --- /dev/null +++ b/tests/Moment/MustBeBeforeInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeBeforeInTimeZone + */ + public function must_be_before_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->mustBeBeforeInTimeZone( + $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' => [ + MomentIsNotBefore::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' => [ + CustomMomentIsNotBeforeInTimeZone::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(), + ], + ]; + } +} diff --git a/tests/Moment/MustBeBeforeTest.php b/tests/Moment/MustBeBeforeTest.php new file mode 100644 index 0000000..113b47b --- /dev/null +++ b/tests/Moment/MustBeBeforeTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustBeBefore + */ + public function must_be_before_works( + ?string $expectedResult, + Moment $moment, + Moment $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $moment->mustBeBefore( + $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' => [ + MomentIsNotBefore::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsNotBefore::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + static fn () => new CustomMomentIsNotBefore(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsNotBefore.php b/tests/Test/Exception/CustomMomentIsNotBefore.php new file mode 100644 index 0000000..d829e54 --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsNotBefore.php @@ -0,0 +1,9 @@ + Date: Mon, 17 Jun 2024 21:06:07 +0200 Subject: [PATCH 11/14] Added mustNotBeBefore and mustNotBeBeforeInTimeZone --- src/Exception/MomentIsBefore.php | 18 ++++ src/Moment.php | 35 ++++++++ .../Moment/MustNotBeBeforeInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustNotBeBeforeTest.php | 74 ++++++++++++++++ tests/Test/Exception/CustomMomentIsBefore.php | 9 ++ .../CustomMomentIsBeforeInTimeZone.php | 9 ++ 6 files changed, 230 insertions(+) create mode 100644 src/Exception/MomentIsBefore.php create mode 100644 tests/Moment/MustNotBeBeforeInTimeZoneTest.php create mode 100644 tests/Moment/MustNotBeBeforeTest.php create mode 100644 tests/Test/Exception/CustomMomentIsBefore.php create mode 100644 tests/Test/Exception/CustomMomentIsBeforeInTimeZone.php diff --git a/src/Exception/MomentIsBefore.php b/src/Exception/MomentIsBefore.php new file mode 100644 index 0000000..f9221f0 --- /dev/null +++ b/src/Exception/MomentIsBefore.php @@ -0,0 +1,18 @@ +isBefore($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsBefore(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsBefore + */ + public function mustNotBeBeforeInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isBeforeInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsBefore(); + } + } } diff --git a/tests/Moment/MustNotBeBeforeInTimeZoneTest.php b/tests/Moment/MustNotBeBeforeInTimeZoneTest.php new file mode 100644 index 0000000..5f79d7c --- /dev/null +++ b/tests/Moment/MustNotBeBeforeInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeBeforeInTimeZone + */ + public function must_not_be_before_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->mustNotBeBeforeInTimeZone( + $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('15:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'default exception' => [ + MomentIsBefore::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' => [ + CustomMomentIsBeforeInTimeZone::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(), + ], + ]; + } +} diff --git a/tests/Moment/MustNotBeBeforeTest.php b/tests/Moment/MustNotBeBeforeTest.php new file mode 100644 index 0000000..8f3038c --- /dev/null +++ b/tests/Moment/MustNotBeBeforeTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeBefore + */ + public function must_not_be_before_works( + ?string $expectedResult, + Moment $moment, + Moment $comparator, + ?callable $otherwiseThrow, + ): void { + // -- Act & Assert + if ($expectedResult !== null) { + $this->expectException($expectedResult); + } else { + $this->expectNotToPerformAssertions(); + } + + $moment->mustNotBeBefore( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 16:00:00'), + null, + ], + 'default exception' => [ + MomentIsBefore::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-10-08 16:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsBefore::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-10-08 16:00:00'), + static fn () => new CustomMomentIsBefore(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsBefore.php b/tests/Test/Exception/CustomMomentIsBefore.php new file mode 100644 index 0000000..98acfd6 --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsBefore.php @@ -0,0 +1,9 @@ + Date: Mon, 17 Jun 2024 21:17:24 +0200 Subject: [PATCH 12/14] 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 @@ + Date: Mon, 17 Jun 2024 21:23:31 +0200 Subject: [PATCH 13/14] Added mustNotBeBeforeOrEqualTo and mustNotBeBeforeOrEqualToInTimeZone --- src/Exception/MomentIsBeforeOrEqualTo.php | 18 ++++ src/Moment.php | 35 ++++++++ ...MustNotBeBeforeOrEqualToInTimeZoneTest.php | 85 +++++++++++++++++++ tests/Moment/MustNotBeBeforeOrEqualToTest.php | 74 ++++++++++++++++ .../CustomMomentIsBeforeOrEqualTo.php | 9 ++ ...ustomMomentIsBeforeOrEqualToInTimeZone.php | 9 ++ 6 files changed, 230 insertions(+) create mode 100644 src/Exception/MomentIsBeforeOrEqualTo.php create mode 100644 tests/Moment/MustNotBeBeforeOrEqualToInTimeZoneTest.php create mode 100644 tests/Moment/MustNotBeBeforeOrEqualToTest.php create mode 100644 tests/Test/Exception/CustomMomentIsBeforeOrEqualTo.php create mode 100644 tests/Test/Exception/CustomMomentIsBeforeOrEqualToInTimeZone.php diff --git a/src/Exception/MomentIsBeforeOrEqualTo.php b/src/Exception/MomentIsBeforeOrEqualTo.php new file mode 100644 index 0000000..2461975 --- /dev/null +++ b/src/Exception/MomentIsBeforeOrEqualTo.php @@ -0,0 +1,18 @@ +isBeforeOrEqualTo($moment)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsBeforeOrEqualTo(); + } + } + + /** + * @param ?callable(): \Throwable $otherwiseThrow + * + * @throws \Throwable + * @throws Exception\MomentIsBeforeOrEqualTo + */ + public function mustNotBeBeforeOrEqualToInTimeZone( + Time | Weekday | Date | Month | Year $moment, + \DateTimeZone $timeZone, + ?callable $otherwiseThrow = null, + ): void { + if ($this->isBeforeOrEqualToInTimeZone($moment, $timeZone)) { + throw $otherwiseThrow !== null + ? $otherwiseThrow() + : new Exception\MomentIsBeforeOrEqualTo(); + } + } } diff --git a/tests/Moment/MustNotBeBeforeOrEqualToInTimeZoneTest.php b/tests/Moment/MustNotBeBeforeOrEqualToInTimeZoneTest.php new file mode 100644 index 0000000..61fefd5 --- /dev/null +++ b/tests/Moment/MustNotBeBeforeOrEqualToInTimeZoneTest.php @@ -0,0 +1,85 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeBeforeOrEqualToInTimeZone + */ + public function must_not_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->mustNotBeBeforeOrEqualToInTimeZone( + $comparator, + $timeZone, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromStringInTimeZone('2022-10-08 16:00:00', new \DateTimeZone('Europe/Berlin')), + Time::fromString('15:00:00'), + new \DateTimeZone('Europe/Berlin'), + null, + ], + 'default exception' => [ + MomentIsBeforeOrEqualTo::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' => [ + CustomMomentIsBeforeOrEqualToInTimeZone::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(), + ], + ]; + } +} diff --git a/tests/Moment/MustNotBeBeforeOrEqualToTest.php b/tests/Moment/MustNotBeBeforeOrEqualToTest.php new file mode 100644 index 0000000..97681ce --- /dev/null +++ b/tests/Moment/MustNotBeBeforeOrEqualToTest.php @@ -0,0 +1,74 @@ + $expectedResult + * + * @dataProvider dataProvider + * + * @covers ::mustNotBeBeforeOrEqualTo + */ + public function must_not_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->mustNotBeBeforeOrEqualTo( + $comparator, + $otherwiseThrow, + ); + } + + /** + * @return array + */ + public static function dataProvider(): array + { + return [ + 'without exception' => [ + null, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 15:00:00'), + null, + ], + 'default exception' => [ + MomentIsBeforeOrEqualTo::class, + Moment::fromString('2022-10-08 15:00:00'), + Moment::fromString('2022-10-08 16:00:00'), + null, + ], + 'custom exception' => [ + CustomMomentIsBeforeOrEqualTo::class, + Moment::fromString('2022-10-08 16:00:00'), + Moment::fromString('2022-10-08 17:00:00'), + static fn () => new CustomMomentIsBeforeOrEqualTo(), + ], + ]; + } +} diff --git a/tests/Test/Exception/CustomMomentIsBeforeOrEqualTo.php b/tests/Test/Exception/CustomMomentIsBeforeOrEqualTo.php new file mode 100644 index 0000000..788b4a0 --- /dev/null +++ b/tests/Test/Exception/CustomMomentIsBeforeOrEqualTo.php @@ -0,0 +1,9 @@ + Date: Sun, 23 Jun 2024 08:37:01 +0200 Subject: [PATCH 14/14] Adapt changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1734c8..6f9a7af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 0.11.0 - Added `Clock` implementation with `SystemClock` and `FrozenClock`. +- Added guard methods for comparison methods to `Moment` like `mustBeEqualTo` with option to throw custom exception. ## 0.10.0