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

DateTimeTzImmutableTypeTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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