Passed
Pull Request — master (#3291)
by Tom
63:46
created

testConvertsDateTimeImmutableInstanceToPHPValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Types;
4
5
use DateTime;
6
use DateTimeImmutable;
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Types\ConversionException;
10
use Doctrine\DBAL\Types\DateImmutableType;
11
use Doctrine\DBAL\Types\Type;
12
use PHPUnit\Framework\Error\Warning;
13
use PHPUnit\Framework\TestCase;
14
use Prophecy\Prophecy\ObjectProphecy;
15
use function get_class;
16
17
class DateImmutableTypeTest extends TestCase
18
{
19
    /** @var AbstractPlatform|ObjectProphecy */
20
    private $platform;
21
22
    /** @var DateImmutableType */
23
    private $type;
24
25
    protected function setUp() : void
26
    {
27
        $this->type     = Type::getType('date_immutable');
28
        $this->platform = $this->prophesize(AbstractPlatform::class);
29
    }
30
31
    public function testFactoryCreatesCorrectType()
32
    {
33
        self::assertSame(DateImmutableType::class, get_class($this->type));
34
    }
35
36
    public function testReturnsName()
37
    {
38
        self::assertSame('date_immutable', $this->type->getName());
39
    }
40
41
    public function testReturnsBindingType()
42
    {
43
        self::assertSame(ParameterType::STRING, $this->type->getBindingType());
44
    }
45
46
    public function testConvertsDateTimeImmutableInstanceToDatabaseValue()
47
    {
48
        $date = $this->prophesize(DateTimeImmutable::class);
49
50
        $this->platform->getDateFormatString()->willReturn('Y-m-d')->shouldBeCalled();
51
        $date->format('Y-m-d')->willReturn('2016-01-01')->shouldBeCalled();
52
53
        self::assertSame(
54
            '2016-01-01',
55
            $this->type->convertToDatabaseValue($date->reveal(), $this->platform->reveal())
56
        );
57
    }
58
59
    public function testConvertsNullToDatabaseValue()
60
    {
61
        self::assertNull($this->type->convertToDatabaseValue(null, $this->platform->reveal()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->convertToDa...is->platform->reveal()) targeting Doctrine\DBAL\Types\Date...onvertToDatabaseValue() 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...
62
    }
63
64
    public function testDoesNotSupportMutableDateTimeToDatabaseValueConversion()
65
    {
66
        $this->expectException(ConversionException::class);
67
68
        $this->type->convertToDatabaseValue(new DateTime(), $this->platform->reveal());
69
    }
70
71
    public function testNormalizesInstanceToPHPValue()
72
    {
73
        $date = new DateTimeImmutable();
74
75
        self::assertSame($date, $this->type->normalizeToPHPValue($date, $this->platform->reveal()));
76
    }
77
78
    public function testConvertsNullToPHPValue()
79
    {
80
        self::assertNull($this->type->convertToPHPValue(null, $this->platform->reveal()));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->convertToPH...is->platform->reveal()) 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...
81
    }
82
83
    public function testConvertsDateStringToPHPValue()
84
    {
85
        $this->platform->getDateFormatString()->willReturn('Y-m-d')->shouldBeCalled();
86
87
        $date = $this->type->convertToPHPValue('2016-01-01', $this->platform->reveal());
88
89
        self::assertInstanceOf(DateTimeImmutable::class, $date);
90
        self::assertSame('2016-01-01', $date->format('Y-m-d'));
91
    }
92
93
    public function testResetTimeFractionsWhenConvertingToPHPValue()
94
    {
95
        $this->platform->getDateFormatString()->willReturn('Y-m-d');
96
97
        $date = $this->type->convertToPHPValue('2016-01-01', $this->platform->reveal());
98
99
        self::assertSame('2016-01-01 00:00:00.000000', $date->format('Y-m-d H:i:s.u'));
100
    }
101
102
    public function testThrowsExceptionDuringConversionToPHPValueWithInvalidDateString()
103
    {
104
        $this->expectException(ConversionException::class);
105
106
        $this->type->convertToPHPValue('invalid date string', $this->platform->reveal());
107
    }
108
109
    public function testArrayConvertsToPHPFailsWithDateTimeImmutableParameterValue()
110
    {
111
        $this->expectException(Warning::class);
112
113
        $date = new DateTimeImmutable();
114
115
        self::assertInstanceOf(
116
            DateTime::class,
117
            $this->type->convertToPHPValue($date, $this->platform->reveal())
118
        );
119
    }
120
121
    public function testRequiresSQLCommentHint()
122
    {
123
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform->reveal()));
124
    }
125
}
126