-
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 mustNotBeBefore and mustNotBeBeforeInTimeZone
- Loading branch information
Christian Kolb
committed
Jun 17, 2024
1 parent
25a6538
commit 5c9ad4f
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 MomentIsBefore extends \InvalidArgumentException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('The moment is before but must not 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\MomentIsBefore; | ||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
use DigitalCraftsman\DateTimePrecision\Month; | ||
use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsBeforeInTimeZone; | ||
use DigitalCraftsman\DateTimePrecision\Time; | ||
use DigitalCraftsman\DateTimePrecision\Weekday; | ||
use DigitalCraftsman\DateTimePrecision\Year; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Moment */ | ||
final class MustNotBeBeforeInTimeZoneTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @param ?class-string<\Throwable> $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<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 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(), | ||
], | ||
]; | ||
} | ||
} |
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\MomentIsBefore; | ||
use DigitalCraftsman\DateTimePrecision\Moment; | ||
use DigitalCraftsman\DateTimePrecision\Test\Exception\CustomMomentIsBefore; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Moment */ | ||
final class MustNotBeBeforeTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @param ?class-string<\Throwable> $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<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 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(), | ||
], | ||
]; | ||
} | ||
} |
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 CustomMomentIsBefore extends \InvalidArgumentException | ||
{ | ||
} |
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 CustomMomentIsBeforeInTimeZone extends \InvalidArgumentException | ||
{ | ||
} |