|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DigitalCraftsman\DateTimePrecision\Day; |
| 6 | + |
| 7 | +use DigitalCraftsman\DateTimePrecision\Day; |
| 8 | +use DigitalCraftsman\DateTimePrecision\Exception\InvalidDay; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * @coversDefaultClass \DigitalCraftsman\DateTimePrecision\Day |
| 13 | + */ |
| 14 | +final class ConstructionTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @test |
| 18 | + * |
| 19 | + * @dataProvider dataProvider |
| 20 | + * |
| 21 | + * @covers ::fromDateTime |
| 22 | + */ |
| 23 | + public function from_date_time_works( |
| 24 | + Day $expectedResult, |
| 25 | + \DateTimeImmutable $dateTime, |
| 26 | + ): void { |
| 27 | + // -- Act & Assert |
| 28 | + self::assertEquals($expectedResult, Day::fromDateTime($dateTime)); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @return array<string, array{ |
| 33 | + * 0: Day, |
| 34 | + * 1: \DateTimeImmutable, |
| 35 | + * }> |
| 36 | + */ |
| 37 | + public static function dataProvider(): array |
| 38 | + { |
| 39 | + return [ |
| 40 | + 'first day of month' => [ |
| 41 | + new Day(1), |
| 42 | + new \DateTimeImmutable('2022-10-01 22:15:00'), |
| 43 | + ], |
| 44 | + 'middle of month' => [ |
| 45 | + new Day(15), |
| 46 | + new \DateTimeImmutable('2022-10-15 10:00:00'), |
| 47 | + ], |
| 48 | + 'last day of month' => [ |
| 49 | + new Day(31), |
| 50 | + new \DateTimeImmutable('2022-10-31 23:59:59'), |
| 51 | + ], |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @test |
| 57 | + * |
| 58 | + * @covers ::__construct |
| 59 | + * |
| 60 | + * @doesNotPerformAssertions |
| 61 | + */ |
| 62 | + public function construction_works(): void |
| 63 | + { |
| 64 | + // -- Act & Assert |
| 65 | + new Day(1); |
| 66 | + new Day(15); |
| 67 | + new Day(31); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @test |
| 72 | + * |
| 73 | + * @covers ::__construct |
| 74 | + */ |
| 75 | + public function construction_fails_with_day_too_low(): void |
| 76 | + { |
| 77 | + // -- Assert |
| 78 | + $this->expectException(InvalidDay::class); |
| 79 | + |
| 80 | + // -- Act |
| 81 | + new Day(0); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @test |
| 86 | + * |
| 87 | + * @covers ::__construct |
| 88 | + */ |
| 89 | + public function construction_fails_with_day_too_high(): void |
| 90 | + { |
| 91 | + // -- Assert |
| 92 | + $this->expectException(InvalidDay::class); |
| 93 | + |
| 94 | + // -- Act |
| 95 | + new Day(32); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @test |
| 100 | + * |
| 101 | + * @covers ::__construct |
| 102 | + */ |
| 103 | + public function construction_fails_with_negative_day(): void |
| 104 | + { |
| 105 | + // -- Assert |
| 106 | + $this->expectException(InvalidDay::class); |
| 107 | + |
| 108 | + // -- Act |
| 109 | + new Day(-1); |
| 110 | + } |
| 111 | +} |
0 commit comments