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

DateTimeImmutableFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 25
c 3
b 0
f 0
dl 0
loc 99
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCreatFromFormatWillReturnDateTimeImmutableData() 0 11 1
A testCreatFromFormatWillReturnDateTimeImmutable() 0 5 1
A testCreatDateTimeWillReturnDateTimeImmutable() 0 7 1
A testImplementsDateTimeFactoryInterface() 0 5 1
A testCreateDateTimeWillThrowDateTimeFactoryExceptionIfDateTimeClassNameIsNotDateTimeImmutable() 0 11 1
A getCreatDateTimeWillReturnDateTimeImmutableData() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ArpTest\DateTime;
6
7
use Arp\DateTime\DateTimeFactoryInterface;
8
use Arp\DateTime\DateTimeImmutableFactory;
9
use Arp\DateTime\Exception\DateTimeFactoryException;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @covers  \Arp\DateTime\DateTimeImmutableFactory
14
 *
15
 * @author  Alex Patterson <[email protected]>
16
 * @package ArpTest\DateTime
17
 */
18
final class DateTimeImmutableFactoryTest extends TestCase
19
{
20
    /**
21
     * Assert the class implements DateTimeFactoryInterface
22
     */
23
    public function testImplementsDateTimeFactoryInterface(): void
24
    {
25
        $factory = new DateTimeImmutableFactory();
26
27
        $this->assertInstanceOf(DateTimeFactoryInterface::class, $factory);
28
    }
29
30
    /**
31
     * Assert that an exception will be thrown if the provided $dateTimeClassName option is a class that does not
32
     * implement \DateTimeInterface.
33
     */
34
    public function testCreateDateTimeWillThrowDateTimeFactoryExceptionIfDateTimeClassNameIsNotDateTimeImmutable(): void
35
    {
36
        $this->expectException(DateTimeFactoryException::class);
37
        $this->expectExceptionMessage(
38
            sprintf(
39
                'The \'dateTimeClassName\' parameter must be a class name that implements \'%s\'',
40
                \DateTimeImmutable::class
41
            )
42
        );
43
44
        new DateTimeImmutableFactory(\DateTime::class);
45
    }
46
47
    /**
48
     * Assert that calls to createDateTime() will return a DateTimeImmutable instance,
49
     * matching the provided $spec and $timeZone
50
     *
51
     * @param string|null               $spec
52
     * @param \DateTimeZone|string|null $timeZone
53
     *
54
     * @dataProvider getCreatDateTimeWillReturnDateTimeImmutableData
55
     *
56
     * @throws DateTimeFactoryException
57
     * @throws \Exception
58
     */
59
    public function testCreatDateTimeWillReturnDateTimeImmutable(?string $spec, $timeZone = null): void
60
    {
61
        $factory = new DateTimeImmutableFactory();
62
63
        $immutableDateTime = $factory->createDateTime($spec, $timeZone);
64
65
        $this->assertInstanceOf(\DateTimeImmutable::class, $immutableDateTime);
66
    }
67
68
    /**
69
     * @return array<mixed>
70
     */
71
    public function getCreatDateTimeWillReturnDateTimeImmutableData(): array
72
    {
73
        return [
74
            [
75
                null,
76
            ],
77
            [
78
                'now',
79
                'UTC',
80
            ],
81
            [
82
                '2021-05-01',
83
            ],
84
        ];
85
    }
86
87
    /**
88
     * @param string                    $format
89
     * @param string                    $spec
90
     * @param string|\DateTimeZone|null $timeZone
91
     *
92
     * @dataProvider getCreatFromFormatWillReturnDateTimeImmutableData
93
     *
94
     * @throws DateTimeFactoryException
95
     */
96
    public function testCreatFromFormatWillReturnDateTimeImmutable(string $format, string $spec, $timeZone = null): void
97
    {
98
        $factory = new DateTimeImmutableFactory();
99
100
        $this->assertInstanceOf(\DateTimeImmutable::class, $factory->createFromFormat($format, $spec, $timeZone));
101
    }
102
103
    /**
104
     * @return array<mixed>
105
     */
106
    public function getCreatFromFormatWillReturnDateTimeImmutableData(): array
107
    {
108
        return [
109
            [
110
                'Y/m/d H:i:s',
111
                '2021/05/03 14:36:47',
112
            ],
113
            [
114
                'Y-d-m H:i:s',
115
                '1999-01-12 11:06:01',
116
                'UTC',
117
            ],
118
        ];
119
    }
120
}
121