1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\DateTime; |
6
|
|
|
|
7
|
|
|
use Arp\DateTime\DateFactory; |
8
|
|
|
use Arp\DateTime\DateFactoryInterface; |
9
|
|
|
use Arp\DateTime\DateIntervalFactoryInterface; |
10
|
|
|
use Arp\DateTime\DateTimeFactoryInterface; |
11
|
|
|
use Arp\DateTime\DateTimeZoneFactoryInterface; |
12
|
|
|
use Arp\DateTime\Exception\DateIntervalFactoryException; |
13
|
|
|
use Arp\DateTime\Exception\DateTimeFactoryException; |
14
|
|
|
use Arp\DateTime\Exception\DateTimeZoneFactoryException; |
15
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @covers \Arp\DateTime\DateFactory |
20
|
|
|
*/ |
21
|
|
|
final class DateFactoryTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var DateTimeFactoryInterface&MockObject |
25
|
|
|
*/ |
26
|
|
|
private DateTimeFactoryInterface $dateTimeFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DateTimeZoneFactoryInterface&MockObject |
30
|
|
|
*/ |
31
|
|
|
private DateTimeZoneFactoryInterface $dateTimeZoneFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DateIntervalFactoryInterface&MockObject |
35
|
|
|
*/ |
36
|
|
|
private DateIntervalFactoryInterface $dateIntervalFactory; |
37
|
|
|
|
38
|
|
|
public function setUp(): void |
39
|
|
|
{ |
40
|
|
|
$this->dateTimeFactory = $this->createMock(DateTimeFactoryInterface::class); |
41
|
|
|
$this->dateTimeZoneFactory = $this->createMock(DateTimeZoneFactoryInterface::class); |
42
|
|
|
$this->dateIntervalFactory = $this->createMock(DateIntervalFactoryInterface::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Ensure that the factory implements DateFactoryInterface |
47
|
|
|
* |
48
|
|
|
* @throws DateTimeFactoryException |
49
|
|
|
*/ |
50
|
|
|
public function testImplementsDateFactoryInterface(): void |
51
|
|
|
{ |
52
|
|
|
$factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory); |
53
|
|
|
|
54
|
|
|
$this->assertInstanceOf(DateFactoryInterface::class, $factory); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Assert the calls to createDateTime() will proxy to the internal DateTimeFactory |
59
|
|
|
* |
60
|
|
|
* @throws DateTimeFactoryException |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
|
|
public function testCreateDateTimeWillProxyToDateTimeFactory(): void |
64
|
|
|
{ |
65
|
|
|
$factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory); |
66
|
|
|
|
67
|
|
|
$spec = '2021-05-01 23:29:33'; |
68
|
|
|
$timeZone = new \DateTimeZone('UTC'); |
69
|
|
|
|
70
|
|
|
$dateTime = new \DateTime($spec, $timeZone); |
71
|
|
|
|
72
|
|
|
$this->dateTimeFactory->expects($this->once()) |
|
|
|
|
73
|
|
|
->method('createDateTime') |
74
|
|
|
->with($spec, $timeZone) |
75
|
|
|
->willReturn($dateTime); |
76
|
|
|
|
77
|
|
|
$this->assertSame($dateTime, $factory->createDateTime($spec, $timeZone)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Assert the calls to createFromFormat() will proxy to the internal DateTimeFactory |
82
|
|
|
* |
83
|
|
|
* @throws DateTimeFactoryException |
84
|
|
|
*/ |
85
|
|
|
public function testCreateFromFormatWillProxyToDateTimeFactory(): void |
86
|
|
|
{ |
87
|
|
|
$factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory); |
88
|
|
|
|
89
|
|
|
$spec = '2021-05-01 23:29:33'; |
90
|
|
|
$format = 'Y-m-d H:i:s'; |
91
|
|
|
$timeZone = new \DateTimeZone('UTC'); |
92
|
|
|
|
93
|
|
|
$dateTime = \DateTime::createFromFormat($format, $spec, $timeZone); |
94
|
|
|
|
95
|
|
|
$this->dateTimeFactory->expects($this->once()) |
96
|
|
|
->method('createFromFormat') |
97
|
|
|
->with($format, $spec, $timeZone) |
98
|
|
|
->willReturn($dateTime); |
99
|
|
|
|
100
|
|
|
$this->assertSame($dateTime, $factory->createFromFormat($format, $spec, $timeZone)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Assert the calls to createDateTimeZone() will proxy to the internal DateTimeZoneFactory |
105
|
|
|
* |
106
|
|
|
* @throws DateTimeZoneFactoryException |
107
|
|
|
* @throws DateTimeFactoryException |
108
|
|
|
* @throws \Exception |
109
|
|
|
*/ |
110
|
|
|
public function testCreateDateTimeZoneWillProxyToDateTimeZoneFactory(): void |
111
|
|
|
{ |
112
|
|
|
$factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory); |
113
|
|
|
|
114
|
|
|
$spec = 'UTC'; |
115
|
|
|
$timeZone = new \DateTimeZone($spec); |
116
|
|
|
|
117
|
|
|
$this->dateTimeZoneFactory->expects($this->once()) |
|
|
|
|
118
|
|
|
->method('createDateTimeZone') |
119
|
|
|
->with($spec) |
120
|
|
|
->willReturn($timeZone); |
121
|
|
|
|
122
|
|
|
$this->assertSame($timeZone, $factory->createDateTimeZone($spec)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Assert the calls to createDateInterval() will proxy to the internal DateTimeIntervalFactory |
127
|
|
|
* |
128
|
|
|
* @throws DateIntervalFactoryException |
129
|
|
|
* @throws DateTimeFactoryException |
130
|
|
|
*/ |
131
|
|
|
public function testCreateDateIntervalWillProxyToDateTimeIntervalFactory(): void |
132
|
|
|
{ |
133
|
|
|
$factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory); |
134
|
|
|
|
135
|
|
|
$spec = 'P1D'; |
136
|
|
|
$dateInterval = new \DateInterval('P1D'); |
137
|
|
|
|
138
|
|
|
$this->dateIntervalFactory->expects($this->once()) |
|
|
|
|
139
|
|
|
->method('createDateInterval') |
140
|
|
|
->with($spec) |
141
|
|
|
->willReturn($dateInterval); |
142
|
|
|
|
143
|
|
|
$this->assertSame($dateInterval, $factory->createDateInterval($spec)); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.