1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\Bundle\ResourceBundle\Tests\Doctrine\DBAL\Type; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
use PHPUnit\Framework\Assert; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Sylius\Bundle\ResourceBundle\Doctrine\DBAL\Type\UTCDateTimeType; |
12
|
|
|
|
13
|
|
|
final class UTCDateTimeTypeTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var UTCDateTimeType |
17
|
|
|
*/ |
18
|
|
|
private $type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AbstractPlatform |
22
|
|
|
*/ |
23
|
|
|
private $abstractPlatform; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
Type::overrideType(Type::DATETIME, UTCDateTimeType::class); |
31
|
|
|
|
32
|
|
|
$this->type = Type::getType(Type::DATETIME); |
33
|
|
|
|
34
|
|
|
$this->abstractPlatform = $this->createMock(AbstractPlatform::class); |
|
|
|
|
35
|
|
|
$this->abstractPlatform->method('getDateTimeFormatString')->willReturn('Y-m-d H:i:s'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
*/ |
41
|
|
|
public function it_converts_datetime_object_to_UTC_based_database_value() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
Assert::assertSame( |
44
|
|
|
'2016-12-31 19:00:00', |
45
|
|
|
$this->type->convertToDatabaseValue( |
46
|
|
|
\DateTime::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('GMT+0600')), |
47
|
|
|
$this->abstractPlatform |
48
|
|
|
) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
Assert::assertSame( |
52
|
|
|
'2016-12-31 19:00:00', |
53
|
|
|
$this->type->convertToDatabaseValue( |
54
|
|
|
\DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('GMT+0600')), |
55
|
|
|
$this->abstractPlatform |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
*/ |
63
|
|
|
public function it_does_nothing_while_converting_null_to_UTC_based_database_value() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
Assert::assertNull($this->type->convertToDatabaseValue(null, $this->abstractPlatform)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @test |
70
|
|
|
*/ |
71
|
|
|
public function it_converts_UTC_based_database_value_to_datetime_object() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
Assert::assertEquals( |
74
|
|
|
\DateTime::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('UTC')), |
75
|
|
|
$this->type->convertToPHPValue('2017-01-01 01:00:00', $this->abstractPlatform) |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
Assert::assertEquals( |
79
|
|
|
\DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('UTC')), |
80
|
|
|
$this->type->convertToPHPValue('2017-01-01 01:00:00', $this->abstractPlatform) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @test |
86
|
|
|
*/ |
87
|
|
|
public function it_does_nothing_while_converting_null_to_datetime_object() |
88
|
|
|
{ |
89
|
|
|
Assert::assertNull($this->type->convertToPHPValue(null, $this->abstractPlatform)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
*/ |
95
|
|
|
public function it_does_nothing_while_converting_datetime_object_to_datetime_object() |
96
|
|
|
{ |
97
|
|
|
$date = \DateTime::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('GMT+0600')); |
98
|
|
|
Assert::assertSame($date, $this->type->convertToPHPValue($date, $this->abstractPlatform)); |
99
|
|
|
|
100
|
|
|
$date = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('GMT+0600')); |
101
|
|
|
Assert::assertSame($date, $this->type->convertToPHPValue($date, $this->abstractPlatform)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @test |
106
|
|
|
* @expectedException \Doctrine\DBAL\Types\ConversionException |
107
|
|
|
*/ |
108
|
|
|
public function it_throws_an_exception_while_converting_invalid_date_to_datetime_object() |
109
|
|
|
{ |
110
|
|
|
$this->type->convertToPHPValue('2017-02-30T01:00:00', $this->abstractPlatform); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..