Failed Conditions
Push — master ( d4a96d...f2d1f3 )
by Sergei
07:54 queued 07:50
created

testDateIntervalFormatWithoutSignConvertsToPHPValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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
use Doctrine\Tests\DbalTestCase;
10
11
final class DateIntervalTest extends DbalTestCase
12
{
13
    /**
14
     * @var MockPlatform
15
     */
16
    private $platform;
17
18
    /**
19
     * @var \Doctrine\DBAL\Types\DateIntervalType
20
     */
21
    private $type;
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    protected function setUp() : void
27
    {
28
        $this->platform = new MockPlatform();
29
        $this->type     = Type::getType('dateinterval');
30
31
        self::assertInstanceOf(DateIntervalType::class, $this->type);
32
    }
33
34
    public function testDateIntervalConvertsToDatabaseValue() : void
35
    {
36
        $interval = new \DateInterval('P2Y1DT1H2M3S');
37
38
        $expected = '+P02Y00M01DT01H02M03S';
39
        $actual = $this->type->convertToDatabaseValue($interval, $this->platform);
40
41
        self::assertEquals($expected, $actual);
42
    }
43
44
    public function testDateIntervalConvertsToPHPValue() : void
45
    {
46
        $interval = $this->type->convertToPHPValue('+P02Y00M01DT01H02M03S', $this->platform);
47
48
        self::assertInstanceOf(\DateInterval::class, $interval);
49
        self::assertEquals('+P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
50
    }
51
52
    public function testNegativeDateIntervalConvertsToDatabaseValue() : void
53
    {
54
        $interval         = new \DateInterval('P2Y1DT1H2M3S');
55
        $interval->invert = 1;
56
57
        $actual = $this->type->convertToDatabaseValue($interval, $this->platform);
58
59
        self::assertEquals('-P02Y00M01DT01H02M03S', $actual);
60
    }
61
62
    public function testNegativeDateIntervalConvertsToPHPValue() : void
63
    {
64
        $interval = $this->type->convertToPHPValue('-P02Y00M01DT01H02M03S', $this->platform);
65
66
        self::assertInstanceOf(\DateInterval::class, $interval);
67
        self::assertEquals('-P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
68
    }
69
70
    public function testDateIntervalFormatWithoutSignConvertsToPHPValue() : void
71
    {
72
        $interval = $this->type->convertToPHPValue('P02Y00M01DT01H02M03S', $this->platform);
73
74
        self::assertInstanceOf(\DateInterval::class, $interval);
75
        self::assertEquals('+P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
76
    }
77
78
    public function testInvalidDateIntervalFormatConversion() : void
79
    {
80
        $this->expectException(ConversionException::class);
81
82
        $this->type->convertToPHPValue('abcdefg', $this->platform);
83
    }
84
85
    public function testDateIntervalNullConversion() : void
86
    {
87
        self::assertNull($this->type->convertToPHPValue(null, $this->platform));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->convertToPH...(null, $this->platform) targeting Doctrine\DBAL\Types\Date...pe::convertToPHPValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
88
    }
89
90
    public function testDateIntervalEmptyStringConversion() : void
91
    {
92
        $this->expectException(ConversionException::class);
93
94
        $this->type->convertToPHPValue('', $this->platform);
95
    }
96
97
    /**
98
     * @group DBAL-1288
99
     */
100
    public function testRequiresSQLCommentHint() : void
101
    {
102
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
103
    }
104
105
    /**
106
     * @dataProvider invalidPHPValuesProvider
107
     */
108
    public function testInvalidTypeConversionToDatabaseValue($value) : void
109
    {
110
        $this->expectException(ConversionException::class);
111
112
        $this->type->convertToDatabaseValue($value, $this->platform);
113
    }
114
115
    /**
116
     * @return mixed[][]
117
     */
118
    public function invalidPHPValuesProvider() : array
119
    {
120
        return [
121
            [0],
122
            [''],
123
            ['foo'],
124
            ['10:11:12'],
125
            ['2015-01-31'],
126
            ['2015-01-31 10:11:12'],
127
            [new \stdClass()],
128
            [$this],
129
            [27],
130
            [-1],
131
            [1.2],
132
            [[]],
133
            [['an array']],
134
            [new \DateTime()],
135
        ];
136
    }
137
}
138