1 | <?php |
||
2 | |||
3 | namespace Doctrine\Tests\DBAL\Types; |
||
4 | |||
5 | use Doctrine\Tests\DBAL\Mocks\MockPlatform; |
||
6 | |||
7 | abstract class BaseDateTypeTestCase extends \PHPUnit\Framework\TestCase |
||
8 | { |
||
9 | /** |
||
10 | * @var MockPlatform |
||
11 | */ |
||
12 | protected $platform; |
||
13 | |||
14 | /** |
||
15 | * @var \Doctrine\DBAL\Types\Type |
||
16 | */ |
||
17 | protected $type; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $currentTimezone; |
||
23 | |||
24 | /** |
||
25 | * {@inheritDoc} |
||
26 | */ |
||
27 | protected function setUp() |
||
28 | { |
||
29 | $this->platform = new MockPlatform(); |
||
30 | $this->currentTimezone = date_default_timezone_get(); |
||
31 | |||
32 | self::assertInstanceOf('Doctrine\DBAL\Types\Type', $this->type); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * {@inheritDoc} |
||
37 | */ |
||
38 | protected function tearDown() |
||
39 | { |
||
40 | date_default_timezone_set($this->currentTimezone); |
||
41 | } |
||
42 | |||
43 | public function testDateConvertsToDatabaseValue() |
||
44 | { |
||
45 | self::assertInternalType('string', $this->type->convertToDatabaseValue(new \DateTime(), $this->platform)); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @dataProvider invalidPHPValuesProvider |
||
50 | * |
||
51 | * @param mixed $value |
||
52 | */ |
||
53 | public function testInvalidTypeConversionToDatabaseValue($value) |
||
54 | { |
||
55 | $this->expectException('Doctrine\DBAL\Types\ConversionException'); |
||
56 | |||
57 | $this->type->convertToDatabaseValue($value, $this->platform); |
||
58 | } |
||
59 | |||
60 | public function testNullConversion() |
||
61 | { |
||
62 | self::assertNull($this->type->convertToPHPValue(null, $this->platform)); |
||
63 | } |
||
64 | |||
65 | public function testConvertDateTimeToPHPValue() |
||
66 | { |
||
67 | $date = new \DateTime('now'); |
||
68 | |||
69 | self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform)); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @group #2794 |
||
74 | * |
||
75 | * Note that while \@see \DateTimeImmutable is supposed to be handled |
||
76 | * by @see \Doctrine\DBAL\Types\DateTimeImmutableType, previous DBAL versions handled it just fine. |
||
77 | * This test is just in place to prevent further regressions, even if the type is being misused |
||
78 | */ |
||
79 | public function testConvertDateTimeImmutableToPHPValue() |
||
80 | { |
||
81 | $date = new \DateTimeImmutable('now'); |
||
82 | |||
83 | self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform)); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @group #2794 |
||
88 | * |
||
89 | * Note that while \@see \DateTimeImmutable is supposed to be handled |
||
90 | * by @see \Doctrine\DBAL\Types\DateTimeImmutableType, previous DBAL versions handled it just fine. |
||
91 | * This test is just in place to prevent further regressions, even if the type is being misused |
||
92 | */ |
||
93 | public function testDateTimeImmutableConvertsToDatabaseValue() |
||
94 | { |
||
95 | self::assertInternalType( |
||
96 | 'string', |
||
97 | $this->type->convertToDatabaseValue(new \DateTimeImmutable(), $this->platform) |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return mixed[][] |
||
103 | */ |
||
104 | View Code Duplication | public function invalidPHPValuesProvider() |
|
0 ignored issues
–
show
|
|||
105 | { |
||
106 | return [ |
||
107 | [0], |
||
108 | [''], |
||
109 | ['foo'], |
||
110 | ['10:11:12'], |
||
111 | ['2015-01-31'], |
||
112 | ['2015-01-31 10:11:12'], |
||
113 | [new \stdClass()], |
||
114 | [$this], |
||
115 | [27], |
||
116 | [-1], |
||
117 | [1.2], |
||
118 | [[]], |
||
119 | [['an array']], |
||
120 | ]; |
||
121 | } |
||
122 | } |
||
123 |
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.