Passed
Pull Request — master (#2579)
by Sergei
16:10
created

testNegativeDateIntervalConvertsToPHPValue()   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
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
    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
    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
    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
    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));
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...
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
    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