Passed
Pull Request — master (#11)
by Alex
02:52
created

testCreateDateTimeZoneWillProxyToDateTimeZoneFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 0
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
 * @author  Alex Patterson <[email protected]>
22
 * @package ArpTest\DateTime
23
 */
24
final class DateFactoryTest extends TestCase
25
{
26
    /**
27
     * @var DateTimeFactoryInterface&MockObject
28
     */
29
    private $dateTimeFactory;
30
31
    /**
32
     * @var DateTimeZoneFactoryInterface&MockObject
33
     */
34
    private $dateTimeZoneFactory;
35
36
    /**
37
     * @var DateIntervalFactoryInterface&MockObject
38
     */
39
    private $dateIntervalFactory;
40
41
    /**
42
     * Set up the test case dependencies
43
     */
44
    public function setUp(): void
45
    {
46
        $this->dateTimeFactory = $this->createMock(DateTimeFactoryInterface::class);
47
        $this->dateTimeZoneFactory = $this->createMock(DateTimeZoneFactoryInterface::class);
48
        $this->dateIntervalFactory = $this->createMock(DateIntervalFactoryInterface::class);
49
    }
50
51
    /**
52
     * Ensure that the factory implements DateFactoryInterface
53
     *
54
     * @throws DateTimeFactoryException
55
     */
56
    public function testImplementsDateFactoryInterface(): void
57
    {
58
        $factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory);
59
60
        $this->assertInstanceOf(DateFactoryInterface::class, $factory);
61
    }
62
63
    /**
64
     * Assert the calls to createDateTime() will proxy to the internal DateTimeFactory
65
     *
66
     * @throws DateTimeFactoryException
67
     * @throws \Exception
68
     */
69
    public function testCreateDateTimeWillProxyToDateTimeFactory(): void
70
    {
71
        $factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory);
72
73
        $spec = '2021-05-01 23:29:33';
74
        $timeZone = new \DateTimeZone('UTC');
75
76
        $dateTime = new \DateTime($spec, $timeZone);
77
78
        $this->dateTimeFactory->expects($this->once())
79
            ->method('createDateTime')
80
            ->with($spec, $timeZone)
81
            ->willReturn($dateTime);
82
83
        $this->assertSame($dateTime, $factory->createDateTime($spec, $timeZone));
84
    }
85
86
    /**
87
     * Assert the calls to createFromFormat() will proxy to the internal DateTimeFactory
88
     *
89
     * @throws DateTimeFactoryException
90
     */
91
    public function testCreateFromFormatWillProxyToDateTimeFactory(): void
92
    {
93
        $factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory);
94
95
        $spec = '2021-05-01 23:29:33';
96
        $format = 'Y-m-d H:i:s';
97
        $timeZone = new \DateTimeZone('UTC');
98
99
        $dateTime = \DateTime::createFromFormat($format, $spec, $timeZone);
100
101
        $this->dateTimeFactory->expects($this->once())
102
            ->method('createFromFormat')
103
            ->with($format, $spec, $timeZone)
104
            ->willReturn($dateTime);
105
106
        $this->assertSame($dateTime, $factory->createFromFormat($format, $spec, $timeZone));
107
    }
108
109
    /**
110
     * Assert the calls to createDateTimeZone() will proxy to the internal DateTimeZoneFactory
111
     *
112
     * @throws DateTimeZoneFactoryException
113
     * @throws DateTimeFactoryException
114
     */
115
    public function testCreateDateTimeZoneWillProxyToDateTimeZoneFactory(): void
116
    {
117
        $factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory);
118
119
        $spec = 'UTC';
120
        $timeZone = new \DateTimeZone($spec);
121
122
        $this->dateTimeZoneFactory->expects($this->once())
123
            ->method('createDateTimeZone')
124
            ->with($spec)
125
            ->willReturn($timeZone);
126
127
        $this->assertSame($timeZone, $factory->createDateTimeZone($spec));
128
    }
129
130
    /**
131
     * Assert the calls to createDateInterval() will proxy to the internal DateTimeIntervalFactory
132
     *
133
     * @throws DateIntervalFactoryException
134
     * @throws DateTimeFactoryException
135
     */
136
    public function testCreateDateIntervalWillProxyToDateTimeIntervalFactory(): void
137
    {
138
        $factory = new DateFactory($this->dateTimeFactory, $this->dateTimeZoneFactory, $this->dateIntervalFactory);
139
140
        $spec = 'P1D';
141
        $dateInterval = new \DateInterval('P1D');
142
143
        $this->dateIntervalFactory->expects($this->once())
144
            ->method('createDateInterval')
145
            ->with($spec)
146
            ->willReturn($dateInterval);
147
148
        $this->assertSame($dateInterval, $factory->createDateInterval($spec));
149
    }
150
}
151