DateIntervalTest.php 3.85 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Tests\Types;
4

Sergei Morozov's avatar
Sergei Morozov committed
5 6
use DateInterval;
use DateTime;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8 9
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateIntervalType;
10
use Doctrine\DBAL\Types\Type;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use PHPUnit\Framework\TestCase;
Sergei Morozov's avatar
Sergei Morozov committed
13
use stdClass;
14

15
final class DateIntervalTest extends TestCase
16
{
17
    /** @var AbstractPlatform|MockObject */
18 19
    private $platform;

Sergei Morozov's avatar
Sergei Morozov committed
20
    /** @var DateIntervalType */
21
    private $type;
22

23
    protected function setUp(): void
24
    {
25
        $this->platform = $this->createMock(AbstractPlatform::class);
26
        $this->type     = Type::getType('dateinterval');
27

28
        self::assertInstanceOf(DateIntervalType::class, $this->type);
29 30
    }

31
    public function testDateIntervalConvertsToDatabaseValue(): void
32
    {
Sergei Morozov's avatar
Sergei Morozov committed
33
        $interval = new DateInterval('P2Y1DT1H2M3S');
34

35
        $expected = '+P02Y00M01DT01H02M03S';
Sergei Morozov's avatar
Sergei Morozov committed
36
        $actual   = $this->type->convertToDatabaseValue($interval, $this->platform);
37

38
        self::assertEquals($expected, $actual);
39 40
    }

41
    public function testDateIntervalConvertsToPHPValue(): void
42
    {
43 44
        $interval = $this->type->convertToPHPValue('+P02Y00M01DT01H02M03S', $this->platform);

Sergei Morozov's avatar
Sergei Morozov committed
45
        self::assertInstanceOf(DateInterval::class, $interval);
46
        self::assertEquals('+P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
47 48
    }

49
    public function testNegativeDateIntervalConvertsToDatabaseValue(): void
50
    {
Sergei Morozov's avatar
Sergei Morozov committed
51
        $interval         = new DateInterval('P2Y1DT1H2M3S');
52 53 54 55
        $interval->invert = 1;

        $actual = $this->type->convertToDatabaseValue($interval, $this->platform);

56
        self::assertEquals('-P02Y00M01DT01H02M03S', $actual);
57 58
    }

59
    public function testNegativeDateIntervalConvertsToPHPValue(): void
60 61 62
    {
        $interval = $this->type->convertToPHPValue('-P02Y00M01DT01H02M03S', $this->platform);

Sergei Morozov's avatar
Sergei Morozov committed
63
        self::assertInstanceOf(DateInterval::class, $interval);
64
        self::assertEquals('-P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
65 66
    }

67
    public function testDateIntervalFormatWithoutSignConvertsToPHPValue(): void
68 69 70
    {
        $interval = $this->type->convertToPHPValue('P02Y00M01DT01H02M03S', $this->platform);

Sergei Morozov's avatar
Sergei Morozov committed
71
        self::assertInstanceOf(DateInterval::class, $interval);
72 73 74
        self::assertEquals('+P02Y00M01DT01H02M03S', $interval->format(DateIntervalType::FORMAT));
    }

75
    public function testInvalidDateIntervalFormatConversion(): void
76
    {
77 78
        $this->expectException(ConversionException::class);

79
        $this->type->convertToPHPValue('abcdefg', $this->platform);
80
    }
81

82
    public function testDateIntervalNullConversion(): void
83
    {
84
        self::assertNull($this->type->convertToPHPValue(null, $this->platform));
85
    }
86

87
    public function testDateIntervalEmptyStringConversion(): void
88 89 90 91 92 93
    {
        $this->expectException(ConversionException::class);

        $this->type->convertToPHPValue('', $this->platform);
    }

94 95 96
    /**
     * @group DBAL-1288
     */
97
    public function testRequiresSQLCommentHint(): void
98
    {
99
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
100 101 102
    }

    /**
103 104
     * @param mixed $value
     *
105 106
     * @dataProvider invalidPHPValuesProvider
     */
107
    public function testInvalidTypeConversionToDatabaseValue($value): void
108
    {
109
        $this->expectException(ConversionException::class);
110 111 112 113 114 115 116

        $this->type->convertToDatabaseValue($value, $this->platform);
    }

    /**
     * @return mixed[][]
     */
117
    public static function invalidPHPValuesProvider(): iterable
118 119 120 121 122 123 124 125
    {
        return [
            [0],
            [''],
            ['foo'],
            ['10:11:12'],
            ['2015-01-31'],
            ['2015-01-31 10:11:12'],
Sergei Morozov's avatar
Sergei Morozov committed
126
            [new stdClass()],
127 128 129 130 131
            [27],
            [-1],
            [1.2],
            [[]],
            [['an array']],
Sergei Morozov's avatar
Sergei Morozov committed
132
            [new DateTime()],
133
        ];
134
    }
135
}