TimeImmutableTypeTest.php 3.59 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\TimeImmutableType;
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 TimeImmutableTypeTest extends TestCase
17
{
18
    /** @var AbstractPlatform|MockObject */
19 20
    private $platform;

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

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

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

35
    public function testReturnsName() : void
36
    {
37
        self::assertSame('time_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('getTimeFormatString')
            ->willReturn('H:i:s');
52
        $date->expects(self::once())
53 54 55
            ->method('format')
            ->with('H:i:s')
            ->willReturn('15:58:59');
56

57
        self::assertSame(
58
            '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 testConvertsTimeStringToPHPValue() : void
88
    {
89
        $this->platform->expects(self::once())
90 91
            ->method('getTimeFormatString')
            ->willReturn('H:i:s');
92

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

Sergei Morozov's avatar
Sergei Morozov committed
95
        self::assertInstanceOf(DateTimeImmutable::class, $date);
96
        self::assertSame('15:58:59', $date->format('H:i:s'));
97 98
    }

99
    public function testResetDateFractionsWhenConvertingToPHPValue() : void
100
    {
101
        $this->platform->expects(self::any())
102 103
            ->method('getTimeFormatString')
            ->willReturn('H:i:s');
104

105
        $date = $this->type->convertToPHPValue('15:58:59', $this->platform);
106

107
        self::assertSame('1970-01-01 15:58:59', $date->format('Y-m-d H:i:s'));
108 109
    }

110
    public function testThrowsExceptionDuringConversionToPHPValueWithInvalidTimeString() : void
111 112 113
    {
        $this->expectException(ConversionException::class);

114
        $this->type->convertToPHPValue('invalid time string', $this->platform);
115 116
    }

117
    public function testRequiresSQLCommentHint() : void
118
    {
119
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
120 121
    }
}