DateTimeImmutableTypeTest.php 3.92 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Tests\Types;
4

Sergei Morozov's avatar
Sergei Morozov committed
5 6
use DateTime;
use DateTimeImmutable;
7
use Doctrine\DBAL\ParameterType;
8 9 10 11
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeImmutableType;
use Doctrine\DBAL\Types\Type;
12
use PHPUnit\Framework\MockObject\MockObject;
Sergei Morozov's avatar
Sergei Morozov committed
13
use PHPUnit\Framework\TestCase;
14
use function get_class;
15

Sergei Morozov's avatar
Sergei Morozov committed
16
class DateTimeImmutableTypeTest extends TestCase
17
{
18
    /** @var AbstractPlatform|MockObject */
19 20
    private $platform;

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

24
    protected function setUp() : void
25
    {
Sergei Morozov's avatar
Sergei Morozov committed
26
        $this->type     = Type::getType('datetime_immutable');
27
        $this->platform = $this->getMockBuilder(AbstractPlatform::class)->getMock();
28 29
    }

30
    public function testFactoryCreatesCorrectType() : void
31
    {
32
        self::assertSame(DateTimeImmutableType::class, get_class($this->type));
33 34
    }

35
    public function testReturnsName() : void
36
    {
37
        self::assertSame('datetime_immutable', $this->type->getName());
38 39
    }

40
    public function testReturnsBindingType() : void
41
    {
42
        self::assertSame(ParameterType::STRING, $this->type->getBindingType());
43 44
    }

45
    public function testConvertsDateTimeImmutableInstanceToDatabaseValue() : void
46
    {
47
        $date = $this->getMockBuilder(DateTimeImmutable::class)->getMock();
48

49
        $this->platform->expects(self::once())
50 51
            ->method('getDateTimeFormatString')
            ->willReturn('Y-m-d H:i:s');
52
        $date->expects(self::once())
53 54 55
            ->method('format')
            ->with('Y-m-d H:i:s')
            ->willReturn('2016-01-01 15:58:59');
56

57
        self::assertSame(
58
            '2016-01-01 15:58:59',
59
            $this->type->convertToDatabaseValue($date, $this->platform)
60 61 62
        );
    }

63
    public function testConvertsNullToDatabaseValue() : void
64
    {
65
        self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
66 67
    }

68
    public function testDoesNotSupportMutableDateTimeToDatabaseValueConversion() : void
69 70 71
    {
        $this->expectException(ConversionException::class);

72
        $this->type->convertToDatabaseValue(new DateTime(), $this->platform);
73 74
    }

75
    public function testConvertsDateTimeImmutableInstanceToPHPValue() : void
76
    {
Sergei Morozov's avatar
Sergei Morozov committed
77
        $date = new DateTimeImmutable();
78

79
        self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform));
80 81
    }

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

87
    public function testConvertsDateTimeStringToPHPValue() : void
88
    {
89
        $this->platform->expects(self::once())
90 91
            ->method('getDateTimeFormatString')
            ->willReturn('Y-m-d H:i:s');
92

93
        $date = $this->type->convertToPHPValue('2016-01-01 15:58:59', $this->platform);
94

Sergei Morozov's avatar
Sergei Morozov committed
95
        self::assertInstanceOf(DateTimeImmutable::class, $date);
96
        self::assertSame('2016-01-01 15:58:59', $date->format('Y-m-d H:i:s'));
97 98 99 100 101
    }

    /**
     * @group DBAL-415
     */
102
    public function testConvertsDateTimeStringWithMicrosecondsToPHPValue() : void
103
    {
104
        $this->platform->expects(self::any())
105 106
            ->method('getDateTimeFormatString')
            ->willReturn('Y-m-d H:i:s');
107

108
        $date = $this->type->convertToPHPValue('2016-01-01 15:58:59.123456', $this->platform);
109

110
        self::assertSame('2016-01-01 15:58:59', $date->format('Y-m-d H:i:s'));
111 112
    }

113
    public function testThrowsExceptionDuringConversionToPHPValueWithInvalidDateTimeString() : void
114
    {
115
        $this->platform->expects(self::atLeastOnce())
116 117 118
            ->method('getDateTimeFormatString')
            ->willReturn('Y-m-d H:i:s');

119 120
        $this->expectException(ConversionException::class);

121
        $this->type->convertToPHPValue('invalid datetime string', $this->platform);
122 123
    }

124
    public function testRequiresSQLCommentHint() : void
125
    {
126
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
127 128
    }
}