1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Types; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Types\Type; |
6
|
|
|
use Doctrine\Tests\DBAL\Mocks\MockPlatform; |
7
|
|
|
|
8
|
|
|
class DateIntervalTest extends \Doctrine\Tests\DbalTestCase |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var MockPlatform |
12
|
|
|
*/ |
13
|
|
|
private $platform; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Doctrine\DBAL\Types\DateIntervalType |
17
|
|
|
*/ |
18
|
|
|
private $type; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritDoc} |
22
|
|
|
*/ |
23
|
|
|
protected function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->platform = new MockPlatform(); |
26
|
|
|
$this->type = Type::getType('dateinterval'); |
27
|
|
|
|
28
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Types\DateIntervalType', $this->type); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function testDateIntervalConvertsToDatabaseValue() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$interval = new \DateInterval('P2Y1DT1H2M3S'); |
34
|
|
|
|
35
|
|
|
$expected = '+P0002-00-01T01:02:03'; |
36
|
|
|
$actual = $this->type->convertToDatabaseValue($interval, $this->platform); |
37
|
|
|
|
38
|
|
|
$this->assertEquals($expected, $actual); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testDateIntervalConvertsToPHPValue() |
42
|
|
|
{ |
43
|
|
|
$interval = $this->type->convertToPHPValue('+P0002-00-01T01:02:03', $this->platform); |
44
|
|
|
$this->assertInstanceOf('DateInterval', $interval); |
45
|
|
|
$this->assertEquals('+P2Y0M1DT1H2M3S', $interval->format('%RP%yY%mM%dDT%hH%iM%sS')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testOldDateIntervalFormatConvertsToPHPValue() |
49
|
|
|
{ |
50
|
|
|
$interval = $this->type->convertToPHPValue('P0002-00-01T01:02:03', $this->platform); |
51
|
|
|
$this->assertInstanceOf('DateInterval', $interval); |
52
|
|
|
$this->assertEquals('+P2Y0M1DT1H2M3S', $interval->format('%RP%yY%mM%dDT%hH%iM%sS')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function testNegativeDateIntervalConvertsToDatabaseValue() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$interval = new \DateInterval('P2Y1DT1H2M3S'); |
58
|
|
|
$interval->invert = 1; |
59
|
|
|
|
60
|
|
|
$expected = '-P0002-00-01T01:02:03'; |
61
|
|
|
$actual = $this->type->convertToDatabaseValue($interval, $this->platform); |
62
|
|
|
|
63
|
|
|
$this->assertEquals($expected, $actual); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testNegativeDateIntervalConvertsToPHPValue() |
67
|
|
|
{ |
68
|
|
|
$interval = $this->type->convertToPHPValue('-P0002-00-01T01:02:03', $this->platform); |
69
|
|
|
$this->assertInstanceOf('DateInterval', $interval); |
70
|
|
|
$this->assertEquals('-P2Y0M1DT1H2M3S', $interval->format('%RP%yY%mM%dDT%hH%iM%sS')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testInvalidDateIntervalFormatConversion() |
74
|
|
|
{ |
75
|
|
|
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); |
|
|
|
|
76
|
|
|
$this->type->convertToPHPValue('abcdefg', $this->platform); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testDateIntervalNullConversion() |
80
|
|
|
{ |
81
|
|
|
$this->assertNull($this->type->convertToPHPValue(null, $this->platform)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @group DBAL-1288 |
86
|
|
|
*/ |
87
|
|
|
public function testRequiresSQLCommentHint() |
88
|
|
|
{ |
89
|
|
|
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @dataProvider invalidPHPValuesProvider |
94
|
|
|
* |
95
|
|
|
* @param mixed $value |
96
|
|
|
*/ |
97
|
|
|
public function testInvalidTypeConversionToDatabaseValue($value) |
98
|
|
|
{ |
99
|
|
|
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); |
|
|
|
|
100
|
|
|
|
101
|
|
|
$this->type->convertToDatabaseValue($value, $this->platform); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return mixed[][] |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
public function invalidPHPValuesProvider() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
|
|
[0], |
111
|
|
|
[''], |
112
|
|
|
['foo'], |
113
|
|
|
['10:11:12'], |
114
|
|
|
['2015-01-31'], |
115
|
|
|
['2015-01-31 10:11:12'], |
116
|
|
|
[new \stdClass()], |
117
|
|
|
[$this], |
118
|
|
|
[27], |
119
|
|
|
[-1], |
120
|
|
|
[1.2], |
121
|
|
|
[[]], |
122
|
|
|
[['an array']], |
123
|
|
|
[new \DateTime()], |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|