Passed
Push — master ( b547f3...f31bce )
by Alex
52s queued 11s
created

CurrentDateTimeProviderTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
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\CurrentDateTimeProvider;
8
use Arp\DateTime\DateTimeFactoryInterface;
9
use Arp\DateTime\DateTimeProviderInterface;
10
use Arp\DateTime\Exception\DateTimeFactoryException;
11
use Arp\DateTime\Exception\DateTimeProviderException;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
15
/**
16
 * @author  Alex Patterson <[email protected]>
17
 * @package ArpTest\DateTime
18
 */
19
class CurrentDateTimeProviderTest extends TestCase
20
{
21
    /**
22
     * @var DateTimeFactoryInterface|MockObject
23
     */
24
    protected $factory;
25
26
    /**
27
     * Set up the test case dependencies.
28
     *
29
     * @return void
30
     */
31
    public function setUp(): void
32
    {
33
        $this->factory = $this->getMockForAbstractClass(DateTimeFactoryInterface::class);
34
    }
35
36
    /**
37
     * Ensure that the provider implements DateTimeProviderInterface.
38
     *
39
     * @covers \Arp\DateTime\CurrentDateTimeProvider
40
     */
41
    public function testImplementsDateTimeProviderInterface(): void
42
    {
43
        $provider = new CurrentDateTimeProvider($this->factory);
44
45
        $this->assertInstanceOf(DateTimeProviderInterface::class, $provider);
46
    }
47
48
    /**
49
     * Ensure that a new \DateTime instance is returned when calling.
50
     *
51
     * @covers \Arp\DateTime\CurrentDateTimeProvider::getDateTime
52
     */
53
    public function testGetDateTimeWillReturnDateTimeInstance(): void
54
    {
55
        $provider = new CurrentDateTimeProvider($this->factory);
56
57
        $dateTime = new \DateTime();
58
59
        $this->factory->expects($this->once())
60
                      ->method('createDateTime')
61
                      ->willReturn($dateTime);
62
63
        $this->assertSame($dateTime, $provider->getDateTime());
64
    }
65
66
    /**
67
     * Ensure that calls to getDateTime that cannot create a new date time instance will throw
68
     * a DateTimeProviderException.
69
     *
70
     * @covers \Arp\DateTime\CurrentDateTimeProvider::getDateTime
71
     */
72
    public function testGetDateTimeWillThrowDateTimeProviderException(): void
73
    {
74
        $provider = new CurrentDateTimeProvider($this->factory);
75
76
        $exceptionMessage = 'This is a test exception message.';
77
        $exception = new DateTimeFactoryException($exceptionMessage);
78
79
        $this->factory->expects($this->once())
80
                      ->method('createDateTime')
81
                      ->willThrowException($exception);
82
83
        $this->expectException(DateTimeProviderException::class);
84
        $this->expectExceptionMessage($exceptionMessage);
85
        $this->expectExceptionCode($exception->getCode());
86
87
        $provider->getDateTime();
88
    }
89
}
90