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

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5
namespace Doctrine\DBAL\Tests\Types;
6

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

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

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

25
    protected function setUp() : void
26
    {
Sergei Morozov's avatar
Sergei Morozov committed
27 28
        $this->platform = $this->createMock(AbstractPlatform::class);
        $this->type     = new DateTimeImmutableType();
29 30
    }

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

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

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

46
    public function testConvertsDateTimeImmutableInstanceToDatabaseValue() : void
47
    {
Sergei Morozov's avatar
Sergei Morozov committed
48
        $date = $this->createMock(DateTimeImmutable::class);
49

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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