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
|
|
|
* @dataProvider microtimeProvider |
15
|
|
|
* |
16
|
|
|
* @param $microtime |
17
|
|
|
* @param $expectedDateTime |
18
|
|
|
* @throws \ReflectionException |
19
|
|
|
*/ |
20
|
|
|
public function testGetNowDateTimePHP70($microtime, $expectedDateTime) |
21
|
|
|
{ |
22
|
|
|
if (PHP_VERSION_ID >= 70100) { |
23
|
|
|
$this->markTestSkipped('Skipped as using a different method to create a datetime'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$clockReflection = new \ReflectionClass(ClockMock::class); |
27
|
|
|
|
28
|
|
|
$reflectionProperty = $clockReflection->getProperty('now'); |
29
|
|
|
$reflectionProperty->setAccessible(true); |
30
|
|
|
$reflectionProperty->setValue($microtime); |
31
|
|
|
|
32
|
|
|
ClockMock::register(DateTimeCreator::class); |
33
|
|
|
|
34
|
|
|
$datetime = DateTimeCreator::getNowDateTime(); |
35
|
|
|
|
36
|
|
|
$this->assertEquals($expectedDateTime, $datetime); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGetNowDateTimePHP71() |
40
|
|
|
{ |
41
|
|
|
if (PHP_VERSION_ID < 70100) { |
42
|
|
|
$this->markTestSkipped('Skipped as using a different method to create date time'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$datetime = DateTimeCreator::getNowDateTime(); |
46
|
|
|
|
47
|
|
|
$this->assertInstanceOf(\DateTime::class, $datetime); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function microtimeProvider() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
'with-decimal' => [ |
54
|
|
|
1544538334.9465, |
55
|
|
|
new \DateTime('2018-12-11 14:25:34.946500'), |
56
|
|
|
], |
57
|
|
|
'without-decimal' => [ |
58
|
|
|
1544538334, |
59
|
|
|
new \DateTime('2018-12-11 14:25:34.000000'), |
60
|
|
|
], |
61
|
|
|
'with-decimal-zero' => [ |
62
|
|
|
1544538334.0, |
63
|
|
|
new \DateTime('2018-12-11 14:25:34.000000'), |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|