|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AssoConnect\PHPDate\Tests; |
| 6 | + |
| 7 | +use AssoConnect\PHPDate\AbsoluteDate; |
| 8 | +use AssoConnect\PHPDate\TimeTraveler; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class TimeTravelerTest extends TestCase |
| 12 | +{ |
| 13 | + private TimeTraveler $timeTraveler; |
| 14 | + |
| 15 | + public function setUp(): void |
| 16 | + { |
| 17 | + $this->timeTraveler = new TimeTraveler(); |
| 18 | + } |
| 19 | + |
| 20 | + /** @dataProvider provideMonths */ |
| 21 | + public function testAddMonth(string $from, string $expected): void |
| 22 | + { |
| 23 | + self::assertSame($expected, $this->timeTraveler->addMonth(new AbsoluteDate($from))->__toString()); |
| 24 | + } |
| 25 | + |
| 26 | + /** @return array{string, string}[] */ |
| 27 | + public function provideMonths(): iterable |
| 28 | + { |
| 29 | + yield ['2020-01-01', '2020-02-01']; |
| 30 | + yield ['2020-01-28', '2020-02-28']; |
| 31 | + yield ['2020-01-29', '2020-02-29']; |
| 32 | + yield ['2020-01-30', '2020-02-29']; |
| 33 | + yield ['2020-01-31', '2020-02-29']; |
| 34 | + yield ['2020-02-29', '2020-03-29']; |
| 35 | + } |
| 36 | + |
| 37 | + /** @dataProvider provideMonthsWithReference */ |
| 38 | + public function testAddMonthWithReference(string $reference, string $from, string $expected): void |
| 39 | + { |
| 40 | + self::assertSame($expected, $this->timeTraveler->addMonthWithReference( |
| 41 | + new AbsoluteDate($reference), |
| 42 | + new AbsoluteDate($from) |
| 43 | + )->__toString()); |
| 44 | + } |
| 45 | + |
| 46 | + /** @return array{string, string, string}[] */ |
| 47 | + public function provideMonthsWithReference(): iterable |
| 48 | + { |
| 49 | + yield ['2020-01-01', '2020-01-01', '2020-02-01']; |
| 50 | + yield ['2020-01-01', '2020-02-01', '2020-03-01']; |
| 51 | + |
| 52 | + yield ['2020-01-25', '2020-02-25', '2020-03-25']; |
| 53 | + |
| 54 | + // Test the result day matches the reference day |
| 55 | + yield ['2020-01-30', '2020-02-29', '2020-03-30']; |
| 56 | + yield ['2020-01-31', '2020-02-29', '2020-03-31']; |
| 57 | + yield ['2020-01-31', '2020-03-31', '2020-04-30']; |
| 58 | + } |
| 59 | + |
| 60 | + /** @dataProvider provideMonthsWithReferenceOverYear */ |
| 61 | + public function testAddMonthWithReferenceWorksYearOverYear(string $reference, string $expected): void |
| 62 | + { |
| 63 | + $reference = new AbsoluteDate($reference); |
| 64 | + |
| 65 | + for ($i = 0; $i < 12; $i++) { |
| 66 | + $actual = $this->timeTraveler->addMonthWithReference($reference, $actual ?? $reference); |
| 67 | + } |
| 68 | + self::assertTrue(isset($actual)); |
| 69 | + self::assertSame($expected, $actual->__toString()); |
| 70 | + } |
| 71 | + |
| 72 | + /** @return array{string, string}[] */ |
| 73 | + public function provideMonthsWithReferenceOverYear(): iterable |
| 74 | + { |
| 75 | + yield ['2020-01-01', '2021-01-01']; |
| 76 | + yield ['2020-01-15', '2021-01-15']; |
| 77 | + yield ['2020-01-28', '2021-01-28']; |
| 78 | + yield ['2020-01-29', '2021-01-29']; |
| 79 | + yield ['2020-01-30', '2021-01-30']; |
| 80 | + yield ['2020-01-31', '2021-01-31']; |
| 81 | + } |
| 82 | + |
| 83 | + /** @dataProvider provideYears */ |
| 84 | + public function testAddYear(string $from, string $expected): void |
| 85 | + { |
| 86 | + self::assertSame($expected, $this->timeTraveler->addYear(new AbsoluteDate($from))->__toString()); |
| 87 | + } |
| 88 | + |
| 89 | + /** @return array{string, string}[] */ |
| 90 | + public function provideYears(): iterable |
| 91 | + { |
| 92 | + yield ['2020-01-01', '2021-01-01']; |
| 93 | + yield ['2020-01-31', '2021-01-31']; |
| 94 | + yield ['2020-02-29', '2021-02-28']; |
| 95 | + } |
| 96 | +} |
0 commit comments