1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\DateTime; |
6
|
|
|
|
7
|
|
|
use Arp\DateTime\DateIntervalFactory; |
8
|
|
|
use Arp\DateTime\DateIntervalFactoryInterface; |
9
|
|
|
use Arp\DateTime\Exception\DateIntervalFactoryException; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Alex Patterson <[email protected]> |
14
|
|
|
* @package ArpTest\DateTime |
15
|
|
|
*/ |
16
|
|
|
final class DateIntervalFactoryTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Ensure that the DateIntervalFactory implements the DateIntervalFactoryInterface. |
20
|
|
|
* |
21
|
|
|
* @covers \Arp\DateTime\DateIntervalFactory |
22
|
|
|
*/ |
23
|
|
|
public function testImplementsDateIntervalFactoryInterface(): void |
24
|
|
|
{ |
25
|
|
|
$factory = new DateIntervalFactory(); |
26
|
|
|
|
27
|
|
|
$this->assertInstanceOf(DateIntervalFactoryInterface::class, $factory); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Ensure that the DateInterval is created in accordance with the provided $spec. |
32
|
|
|
* |
33
|
|
|
* @param string $spec The \DateInterval specification. |
34
|
|
|
* |
35
|
|
|
* @dataProvider getCreateDateIntervalData |
36
|
|
|
* |
37
|
|
|
* @covers \Arp\DateTime\DateIntervalFactory::createDateInterval |
38
|
|
|
*/ |
39
|
|
|
public function testCreateDateInterval(string $spec): void |
40
|
|
|
{ |
41
|
|
|
$factory = new DateIntervalFactory(); |
42
|
|
|
|
43
|
|
|
$dateInterval = $factory->createDateInterval($spec); |
44
|
|
|
|
45
|
|
|
$test = new \DateInterval($spec); |
46
|
|
|
|
47
|
|
|
$this->assertSame($test->y, $dateInterval->y); |
48
|
|
|
$this->assertSame($test->m, $dateInterval->m); |
49
|
|
|
$this->assertSame($test->d, $dateInterval->d); |
50
|
|
|
$this->assertSame($test->h, $dateInterval->h); |
51
|
|
|
$this->assertSame($test->i, $dateInterval->i); |
52
|
|
|
$this->assertSame($test->f, $dateInterval->f); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @see https://www.php.net/manual/en/class.dateinterval.php |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getCreateDateIntervalData(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
['P100D'], |
64
|
|
|
['P4Y1DT9H11M3S'], |
65
|
|
|
['P2Y4DT6H8M'], |
66
|
|
|
['P7Y8M'], |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Ensure that createDateInterval() will throw a DateIntervalFactoryException if the provided $spec is invalid. |
72
|
|
|
* |
73
|
|
|
* @param string $spec |
74
|
|
|
* |
75
|
|
|
* @dataProvider getCreateDateIntervalWillThrowDateIntervalFactoryExceptionData |
76
|
|
|
* |
77
|
|
|
* @covers \Arp\DateTime\DateIntervalFactory::createDateInterval |
78
|
|
|
*/ |
79
|
|
|
public function testCreateDateIntervalWillThrowDateIntervalFactoryException(string $spec): void |
80
|
|
|
{ |
81
|
|
|
$factory = new DateIntervalFactory(); |
82
|
|
|
|
83
|
|
|
$exceptionMessage = sprintf('DateInterval::__construct(): Unknown or bad format (%s)', $spec); |
84
|
|
|
|
85
|
|
|
$this->expectException(DateIntervalFactoryException::class); |
86
|
|
|
$this->expectExceptionMessage( |
87
|
|
|
sprintf( |
88
|
|
|
'Failed to create a valid \DateInterval instance using \'%s\': %s', |
89
|
|
|
$spec, |
90
|
|
|
$exceptionMessage |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$factory->createDateInterval($spec); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public function getCreateDateIntervalWillThrowDateIntervalFactoryExceptionData(): array |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
|
|
['test'], |
104
|
|
|
['invalid'], |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|