1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Types; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use Doctrine\DBAL\ParameterType; |
8
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
9
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
10
|
|
|
use Doctrine\DBAL\Types\DateTimeImmutableType; |
11
|
|
|
use Doctrine\DBAL\Types\Type; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
14
|
|
|
use function get_class; |
15
|
|
|
|
16
|
|
|
class DateTimeImmutableTypeTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** @var AbstractPlatform|ObjectProphecy */ |
19
|
|
|
private $platform; |
20
|
|
|
|
21
|
|
|
/** @var DateTimeImmutableType */ |
22
|
|
|
private $type; |
23
|
|
|
|
24
|
|
|
protected function setUp() : void |
25
|
|
|
{ |
26
|
|
|
$this->type = Type::getType('datetime_immutable'); |
27
|
|
|
$this->platform = $this->prophesize(AbstractPlatform::class); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testFactoryCreatesCorrectType() |
31
|
|
|
{ |
32
|
|
|
self::assertSame(DateTimeImmutableType::class, get_class($this->type)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testReturnsName() |
36
|
|
|
{ |
37
|
|
|
self::assertSame('datetime_immutable', $this->type->getName()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testReturnsBindingType() |
41
|
|
|
{ |
42
|
|
|
self::assertSame(ParameterType::STRING, $this->type->getBindingType()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testConvertsDateTimeImmutableInstanceToDatabaseValue() |
46
|
|
|
{ |
47
|
|
|
$date = $this->prophesize(DateTimeImmutable::class); |
48
|
|
|
|
49
|
|
|
$this->platform->getDateTimeFormatString()->willReturn('Y-m-d H:i:s')->shouldBeCalled(); |
50
|
|
|
$date->format('Y-m-d H:i:s')->willReturn('2016-01-01 15:58:59')->shouldBeCalled(); |
51
|
|
|
|
52
|
|
|
self::assertSame( |
53
|
|
|
'2016-01-01 15:58:59', |
54
|
|
|
$this->type->convertToDatabaseValue($date->reveal(), $this->platform->reveal()) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testConvertsNullToDatabaseValue() |
59
|
|
|
{ |
60
|
|
|
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform->reveal())); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testDoesNotSupportMutableDateTimeToDatabaseValueConversion() |
64
|
|
|
{ |
65
|
|
|
$this->expectException(ConversionException::class); |
66
|
|
|
|
67
|
|
|
$this->type->convertToDatabaseValue(new DateTime(), $this->platform->reveal()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testNormalizeInstanceToPHPValue() |
71
|
|
|
{ |
72
|
|
|
$date = new DateTimeImmutable(); |
73
|
|
|
|
74
|
|
|
self::assertSame($date, $this->type->normalizeToPHPValue($date, $this->platform->reveal())); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testConvertsNullToPHPValue() |
78
|
|
|
{ |
79
|
|
|
self::assertNull($this->type->convertToPHPValue(null, $this->platform->reveal())); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testConvertsDateTimeStringToPHPValue() |
83
|
|
|
{ |
84
|
|
|
$this->platform->getDateTimeFormatString()->willReturn('Y-m-d H:i:s')->shouldBeCalled(); |
85
|
|
|
|
86
|
|
|
$date = $this->type->convertToPHPValue('2016-01-01 15:58:59', $this->platform->reveal()); |
87
|
|
|
|
88
|
|
|
self::assertInstanceOf(DateTimeImmutable::class, $date); |
89
|
|
|
self::assertSame('2016-01-01 15:58:59', $date->format('Y-m-d H:i:s')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @group DBAL-415 |
94
|
|
|
*/ |
95
|
|
|
public function testConvertsDateTimeStringWithMicrosecondsToPHPValue() |
96
|
|
|
{ |
97
|
|
|
$this->platform->getDateTimeFormatString()->willReturn('Y-m-d H:i:s'); |
98
|
|
|
|
99
|
|
|
$date = $this->type->convertToPHPValue('2016-01-01 15:58:59.123456', $this->platform->reveal()); |
100
|
|
|
|
101
|
|
|
self::assertSame('2016-01-01 15:58:59', $date->format('Y-m-d H:i:s')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testThrowsExceptionDuringConversionToPHPValueWithInvalidDateTimeString() |
105
|
|
|
{ |
106
|
|
|
$this->expectException(ConversionException::class); |
107
|
|
|
|
108
|
|
|
$this->type->convertToPHPValue('invalid datetime string', $this->platform->reveal()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testRequiresSQLCommentHint() |
112
|
|
|
{ |
113
|
|
|
self::assertTrue($this->type->requiresSQLCommentHint($this->platform->reveal())); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.