-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mustBeAfterOrEqualTo and mustBeAfterOrEqualToInTimeZone
- Loading branch information
Christian Kolb
committed
Jun 14, 2024
1 parent
fded4dd
commit ccbc7af
Showing
6 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Exception; | ||
|
||
/** | ||
* @psalm-immutable | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
final class MomentIsNotAfterOrEqualTo extends \InvalidArgumentException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('The moment is not after or equal to but must be.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Moment; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Date; | ||
use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotAfterOrEqualTo; | ||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
use DigitalCraftsman\DateTimePrecision\Month; | ||
use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotAfterOrEqualToInTimeZone; | ||
use DigitalCraftsman\DateTimePrecision\Time; | ||
use DigitalCraftsman\DateTimePrecision\Weekday; | ||
use DigitalCraftsman\DateTimePrecision\Year; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Moment */ | ||
final class MustBeAfterOrEqualToInTimeZoneTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @param ?class-string<\Throwable> $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<string, array{ | ||
* 0: ?string, | ||
* 1: Moment, | ||
* 2: Time | Weekday | Date | Month | Year, | ||
* 3: \DateTimeZone, | ||
* 4: ?callable(): \Throwable | ||
* }> | ||
*/ | ||
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(), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Moment; | ||
|
||
use DigitalCraftsman\DateTimePrecision\Exception\MomentIsNotAfterOrEqualTo; | ||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsNotAfterOrEqualTo; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Moment */ | ||
final class MustBeAfterOrEqualToTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @param ?class-string<\Throwable> $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<string, array{ | ||
* 0: ?string, | ||
* 1: Moment, | ||
* 2: Moment, | ||
* 3: ?callable(): \Throwable | ||
* }> | ||
*/ | ||
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(), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Test\Exception; | ||
|
||
final class CustomMomentIsNotAfterOrEqualTo extends \InvalidArgumentException | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/Test/Exception/CustomMomentIsNotAfterOrEqualToInTimeZone.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DigitalCraftsman\DateTimePrecision\Test\Exception; | ||
|
||
final class CustomMomentIsNotAfterOrEqualToInTimeZone extends \InvalidArgumentException | ||
{ | ||
} |