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

TimeImmutableTypeTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 29
dl 0
loc 95
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testReturnsName() 0 3 1
A testReturnsBindingType() 0 3 1
A testConvertsNullToPHPValue() 0 3 1
A testRequiresSQLCommentHint() 0 3 1
A setUp() 0 4 1
A testFactoryCreatesCorrectType() 0 3 1
A testConvertsNullToDatabaseValue() 0 3 1
A testThrowsExceptionDuringConversionToPHPValueWithInvalidTimeString() 0 5 1
A testDoesNotSupportMutableDateTimeToDatabaseValueConversion() 0 5 1
A testConvertsTimeStringToPHPValue() 0 8 1
A testResetDateFractionsWhenConvertingToPHPValue() 0 7 1
A testConvertsDateTimeImmutableInstanceToDatabaseValue() 0 10 1
A testNormalizesInstanceToPHPValue() 0 5 1
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\TimeImmutableType;
11
use Doctrine\DBAL\Types\Type;
12
use PHPUnit\Framework\TestCase;
13
use Prophecy\Prophecy\ObjectProphecy;
14
use function get_class;
15
16
class TimeImmutableTypeTest extends TestCase
17
{
18
    /** @var AbstractPlatform|ObjectProphecy */
19
    private $platform;
20
21
    /** @var TimeImmutableType */
22
    private $type;
23
24
    protected function setUp() : void
25
    {
26
        $this->type     = Type::getType('time_immutable');
27
        $this->platform = $this->prophesize(AbstractPlatform::class);
28
    }
29
30
    public function testFactoryCreatesCorrectType()
31
    {
32
        self::assertSame(TimeImmutableType::class, get_class($this->type));
33
    }
34
35
    public function testReturnsName()
36
    {
37
        self::assertSame('time_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->getTimeFormatString()->willReturn('H:i:s')->shouldBeCalled();
50
        $date->format('H:i:s')->willReturn('15:58:59')->shouldBeCalled();
51
52
        self::assertSame(
53
            '15:58:59',
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\Time...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\Time...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 testConvertsTimeStringToPHPValue()
83
    {
84
        $this->platform->getTimeFormatString()->willReturn('H:i:s')->shouldBeCalled();
85
86
        $date = $this->type->convertToPHPValue('15:58:59', $this->platform->reveal());
87
88
        self::assertInstanceOf(DateTimeImmutable::class, $date);
89
        self::assertSame('15:58:59', $date->format('H:i:s'));
90
    }
91
92
    public function testResetDateFractionsWhenConvertingToPHPValue()
93
    {
94
        $this->platform->getTimeFormatString()->willReturn('H:i:s');
95
96
        $date = $this->type->convertToPHPValue('15:58:59', $this->platform->reveal());
97
98
        self::assertSame('1970-01-01 15:58:59', $date->format('Y-m-d H:i:s'));
99
    }
100
101
    public function testThrowsExceptionDuringConversionToPHPValueWithInvalidTimeString()
102
    {
103
        $this->expectException(ConversionException::class);
104
105
        $this->type->convertToPHPValue('invalid time string', $this->platform->reveal());
106
    }
107
108
    public function testRequiresSQLCommentHint()
109
    {
110
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform->reveal()));
111
    }
112
}
113