Completed
Pull Request — master (#17)
by
unknown
02:18
created

DateTimeCreatorTest::testGetNowDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Smartbox\CoreBundle\Tests\Utils\Helper;
4
5
use Smartbox\CoreBundle\Utils\Helper\DateTimeCreator;
6
use Symfony\Bridge\PhpUnit\ClockMock;
7
8
/**
9
 * @coversDefaultClass \Smartbox\CoreBundle\Utils\Helper\DateTimeCreator
10
 */
11
class DateTimeCreatorTest extends \PHPUnit\Framework\TestCase
12
{
13
    /**
14
     * @covers ::getNowDateTime
15
     * @dataProvider microtimeProvider
16
     */
17
    public function testGetNowDateTimePHP70($microtime, $expectedDateTime)
18
    {
19
        if (PHP_VERSION_ID >= 70100) {
20
            $this->markTestSkipped('Skipped as using a different method to create a datetime');
21
        }
22
        $clockReflection = new \ReflectionClass(ClockMock::class);
23
24
        $reflectionProperty = $clockReflection->getProperty('now');
25
        $reflectionProperty->setAccessible(true);
26
        $reflectionProperty->setValue($microtime);
27
28
        ClockMock::register(DateTimeCreator::class);
29
30
        $datetime = DateTimeCreator::getNowDateTime();
31
32
        $this->assertEquals($expectedDateTime, $datetime);
33
    }
34
35
    /**
36
     * @covers ::getNowDateTime
37
     */
38
    public function testGetNowDateTimePHP71()
39
    {
40
        if (PHP_VERSION_ID < 70100) {
41
            $this->markTestSkipped('Skipped as using a different method to create date time');
42
        }
43
44
        $datetime = DateTimeCreator::getNowDateTime();
45
46
        $this->assertInstanceOf(\DateTime::class, $datetime);
47
    }
48
49
    public function microtimeProvider()
50
    {
51
        return [
52
            'with-decimal' => [
53
                1544538334.9465,
54
                new \DateTime('2018-12-11 14:25:34.946500'),
55
            ],
56
            'without-decimal' => [
57
                1544538334,
58
                new \DateTime('2018-12-11 14:25:34.000000'),
59
            ],
60
            'with-decimal-zero' => [
61
                1544538334.0,
62
                new \DateTime('2018-12-11 14:25:34.000000'),
63
            ],
64
        ];
65
    }
66
}
67