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\Type;
11
use Doctrine\DBAL\Types\VarDateTimeImmutableType;
12
use PHPUnit\Framework\TestCase;
13
use Prophecy\Prophecy\ObjectProphecy;
14
15
class VarDateTimeImmutableTypeTest extends TestCase
16
{
17
    /** @var AbstractPlatform|ObjectProphecy */
18
    private $platform;
19
20
    /** @var VarDateTimeImmutableType */
21
    private $type;
22
23
    protected function setUp() : void
24
    {
25
        if (! Type::hasType('vardatetime_immutable')) {
26
            Type::addType('vardatetime_immutable', VarDateTimeImmutableType::class);
27
        }
28
29
        $this->type     = Type::getType('vardatetime_immutable');
30
        $this->platform = $this->prophesize(AbstractPlatform::class);
31
    }
32
33
    public function testReturnsName()
34
    {
35
        self::assertSame('datetime_immutable', $this->type->getName());
36
    }
37
38
    public function testReturnsBindingType()
39
    {
40
        self::assertSame(ParameterType::STRING, $this->type->getBindingType());
41
    }
42
43
    public function testConvertsDateTimeImmutableInstanceToDatabaseValue()
44
    {
45
        $date = $this->prophesize(DateTimeImmutable::class);
46
47
        $this->platform->getDateTimeFormatString()->willReturn('Y-m-d H:i:s')->shouldBeCalled();
48
        $date->format('Y-m-d H:i:s')->willReturn('2016-01-01 15:58:59')->shouldBeCalled();
49
50
        self::assertSame(
51
            '2016-01-01 15:58:59',
52
            $this->type->convertToDatabaseValue($date->reveal(), $this->platform->reveal())
53
        );
54
    }
55
56
    public function testConvertsNullToDatabaseValue()
57
    {
58
        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\VarD...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...
59
    }
60
61
    public function testDoesNotSupportMutableDateTimeToDatabaseValueConversion()
62
    {
63
        $this->expectException(ConversionException::class);
64
65
        $this->type->convertToDatabaseValue(new DateTime(), $this->platform->reveal());
66
    }
67
68
    public function testNormalizesInstanceToPHPValue()
69
    {
70
        $date = new DateTimeImmutable();
71
72
        self::assertSame($date, $this->type->normalizeToPHPValue($date, $this->platform->reveal()));
73
    }
74
75
    public function testConvertsNullToPHPValue()
76
    {
77
        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\VarD...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...
78
    }
79
80
    public function testConvertsDateishStringToPHPValue()
81
    {
82
        $this->platform->getDateTimeFormatString()->shouldNotBeCalled();
83
84
        $date = $this->type->convertToPHPValue('2016-01-01 15:58:59.123456 UTC', $this->platform->reveal());
85
86
        self::assertInstanceOf(DateTimeImmutable::class, $date);
87
        self::assertSame('2016-01-01 15:58:59.123456 UTC', $date->format('Y-m-d H:i:s.u T'));
88
    }
89
90
    public function testThrowsExceptionDuringConversionToPHPValueWithInvalidDateishString()
91
    {
92
        $this->expectException(ConversionException::class);
93
94
        $this->type->convertToPHPValue('invalid date-ish string', $this->platform->reveal());
95
    }
96
97
    public function testRequiresSQLCommentHint()
98
    {
99
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform->reveal()));
100
    }
101
}
102