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

DateTimeCreatorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetNowDateTimePHP70() 0 17 2
A testGetNowDateTimePHP71() 0 10 2
A microtimeProvider() 0 17 1
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