1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Types; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
6
|
|
|
use Doctrine\DBAL\Types\DateIntervalType; |
7
|
|
|
use Doctrine\DBAL\Types\Type; |
8
|
|
|
use Doctrine\Tests\DBAL\Mocks\MockPlatform; |
9
|
|
|
|
10
|
|
|
final class DateIntervalTest extends \Doctrine\Tests\DbalTestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var MockPlatform |
14
|
|
|
*/ |
15
|
|
|
private $platform; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Doctrine\DBAL\Types\DateIntervalType |
19
|
|
|
*/ |
20
|
|
|
private $type; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritDoc} |
24
|
|
|
*/ |
25
|
|
|
protected function setUp() : void |
26
|
|
|
{ |
27
|
|
|
$this->platform = new MockPlatform(); |
28
|
|
|
$this->type = Type::getType('dateinterval'); |
29
|
|
|
|
30
|
|
|
self::assertInstanceOf(DateIntervalType::class, $this->type); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function testDateIntervalConvertsToDatabaseValue() : void |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$interval = new \DateInterval('P2Y1DT1H2M3S'); |
36
|
|
|
|
37
|
|
|
$expected = '+P02Y00M01DT01H02M03S'; |
38
|
|
|
$actual = $this->type->convertToDatabaseValue($interval, $this->platform); |
39
|
|
|
|
40
|
|
|
self::assertEquals($expected, $actual); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function testDateIntervalConvertsToPHPValue() : void |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$interval = $this->type->convertToPHPValue('+P02Y00M01DT01H02M03S', $this->platform); |
46
|
|
|
|
47
|
|
|
self::assertInstanceOf(\DateInterval::class, $interval); |
48
|
|
|
self::assertEquals('+P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
View Code Duplication |
public function testNegativeDateIntervalConvertsToDatabaseValue() : void |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$interval = new \DateInterval('P2Y1DT1H2M3S'); |
54
|
|
|
$interval->invert = 1; |
55
|
|
|
|
56
|
|
|
$actual = $this->type->convertToDatabaseValue($interval, $this->platform); |
57
|
|
|
|
58
|
|
|
self::assertEquals('-P02Y00M01DT01H02M03S', $actual); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function testNegativeDateIntervalConvertsToPHPValue() : void |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$interval = $this->type->convertToPHPValue('-P02Y00M01DT01H02M03S', $this->platform); |
64
|
|
|
|
65
|
|
|
self::assertInstanceOf(\DateInterval::class, $interval); |
66
|
|
|
self::assertEquals('-P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testInvalidDateIntervalFormatConversion() : void |
70
|
|
|
{ |
71
|
|
|
$this->expectException(ConversionException::class); |
72
|
|
|
|
73
|
|
|
$this->type->convertToPHPValue('abcdefg', $this->platform); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testDateIntervalNullConversion() : void |
77
|
|
|
{ |
78
|
|
|
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @group DBAL-1288 |
83
|
|
|
*/ |
84
|
|
|
public function testRequiresSQLCommentHint() : void |
85
|
|
|
{ |
86
|
|
|
self::assertTrue($this->type->requiresSQLCommentHint($this->platform)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @dataProvider invalidPHPValuesProvider |
91
|
|
|
*/ |
92
|
|
|
public function testInvalidTypeConversionToDatabaseValue($value) : void |
93
|
|
|
{ |
94
|
|
|
$this->expectException(ConversionException::class); |
95
|
|
|
|
96
|
|
|
$this->type->convertToDatabaseValue($value, $this->platform); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed[][] |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function invalidPHPValuesProvider() : array |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
[0], |
106
|
|
|
[''], |
107
|
|
|
['foo'], |
108
|
|
|
['10:11:12'], |
109
|
|
|
['2015-01-31'], |
110
|
|
|
['2015-01-31 10:11:12'], |
111
|
|
|
[new \stdClass()], |
112
|
|
|
[$this], |
113
|
|
|
[27], |
114
|
|
|
[-1], |
115
|
|
|
[1.2], |
116
|
|
|
[[]], |
117
|
|
|
[['an array']], |
118
|
|
|
[new \DateTime()], |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.