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 = '+P02Y00M01DT01H02M03S'; |
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('+P02Y00M01DT01H02M03S', $this->platform); |
44
|
|
|
$this->assertInstanceOf('DateInterval', $interval); |
45
|
|
|
$this->assertEquals('+P02Y00M01DT01H02M03S', $interval->format('%RP%YY%MM%DDT%HH%IM%SS')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testNegativeDateIntervalConvertsToDatabaseValue() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$interval = new \DateInterval('P2Y1DT1H2M3S'); |
51
|
|
|
$interval->invert = 1; |
52
|
|
|
|
53
|
|
|
$expected = '-P02Y00M01DT01H02M03S'; |
54
|
|
|
$actual = $this->type->convertToDatabaseValue($interval, $this->platform); |
55
|
|
|
|
56
|
|
|
$this->assertEquals($expected, $actual); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testNegativeDateIntervalConvertsToPHPValue() |
60
|
|
|
{ |
61
|
|
|
$interval = $this->type->convertToPHPValue('-P02Y00M01DT01H02M03S', $this->platform); |
62
|
|
|
$this->assertInstanceOf('DateInterval', $interval); |
63
|
|
|
$this->assertEquals('-P02Y00M01DT01H02M03S', $interval->format('%RP%YY%MM%DDT%HH%IM%SS')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testInvalidDateIntervalFormatConversion() |
67
|
|
|
{ |
68
|
|
|
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); |
|
|
|
|
69
|
|
|
$this->type->convertToPHPValue('abcdefg', $this->platform); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testDateIntervalNullConversion() |
73
|
|
|
{ |
74
|
|
|
$this->assertNull($this->type->convertToPHPValue(null, $this->platform)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @group DBAL-1288 |
79
|
|
|
*/ |
80
|
|
|
public function testRequiresSQLCommentHint() |
81
|
|
|
{ |
82
|
|
|
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @dataProvider invalidPHPValuesProvider |
87
|
|
|
* |
88
|
|
|
* @param mixed $value |
89
|
|
|
*/ |
90
|
|
|
public function testInvalidTypeConversionToDatabaseValue($value) |
91
|
|
|
{ |
92
|
|
|
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException'); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$this->type->convertToDatabaseValue($value, $this->platform); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return mixed[][] |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function invalidPHPValuesProvider() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
|
|
[0], |
104
|
|
|
[''], |
105
|
|
|
['foo'], |
106
|
|
|
['10:11:12'], |
107
|
|
|
['2015-01-31'], |
108
|
|
|
['2015-01-31 10:11:12'], |
109
|
|
|
[new \stdClass()], |
110
|
|
|
[$this], |
111
|
|
|
[27], |
112
|
|
|
[-1], |
113
|
|
|
[1.2], |
114
|
|
|
[[]], |
115
|
|
|
[['an array']], |
116
|
|
|
[new \DateTime()], |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|