Passed
Pull Request — master (#2579)
by Sergei
13:33
created

testNegativeDateIntervalConvertsToDatabaseValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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 testInvalidDateIntervalFormatConversion() : void
71
    {
72
        $this->expectException(ConversionException::class);
73
74
        $this->type->convertToPHPValue('abcdefg', $this->platform);
75
    }
76
77
    public function testDateIntervalNullConversion() : void
78
    {
79
        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...
80
    }
81
82
    /**
83
     * @group DBAL-1288
84
     */
85
    public function testRequiresSQLCommentHint() : void
86
    {
87
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
88
    }
89
90
    /**
91
     * @dataProvider invalidPHPValuesProvider
92
     */
93
    public function testInvalidTypeConversionToDatabaseValue($value) : void
94
    {
95
        $this->expectException(ConversionException::class);
96
97
        $this->type->convertToDatabaseValue($value, $this->platform);
98
    }
99
100
    /**
101
     * @return mixed[][]
102
     */
103
    public function invalidPHPValuesProvider() : array
104
    {
105
        return [
106
            [0],
107
            [''],
108
            ['foo'],
109
            ['10:11:12'],
110
            ['2015-01-31'],
111
            ['2015-01-31 10:11:12'],
112
            [new \stdClass()],
113
            [$this],
114
            [27],
115
            [-1],
116
            [1.2],
117
            [[]],
118
            [['an array']],
119
            [new \DateTime()],
120
        ];
121
    }
122
}
123