Completed
Push — utc-datetime-storing ( 42f778 )
by Kamil
29:36
created

UTCDateTimeTypeTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 100
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A it_converts_datetime_object_to_UTC_based_database_value() 0 18 1
A it_does_nothing_while_converting_null_to_UTC_based_database_value() 0 4 1
A it_converts_UTC_based_database_value_to_datetime_object() 0 12 1
A it_does_nothing_while_converting_null_to_datetime_object() 0 4 1
A it_does_nothing_while_converting_datetime_object_to_datetime_object() 0 8 1
A it_throws_an_exception_while_converting_invalid_date_to_datetime_object() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\Bundle\ResourceBundle\Tests\Doctrine\DBAL\Type;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Types\Type;
9
use PHPUnit\Framework\Assert;
10
use PHPUnit\Framework\TestCase;
11
use Sylius\Bundle\ResourceBundle\Doctrine\DBAL\Type\UTCDateTimeType;
12
13
final class UTCDateTimeTypeTest extends TestCase
14
{
15
    /**
16
     * @var UTCDateTimeType
17
     */
18
    private $type;
19
20
    /**
21
     * @var AbstractPlatform
22
     */
23
    private $abstractPlatform;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function setUp()
29
    {
30
        Type::overrideType(Type::DATETIME, UTCDateTimeType::class);
31
32
        $this->type = Type::getType(Type::DATETIME);
33
34
        $this->abstractPlatform = $this->createMock(AbstractPlatform::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...bstractPlatform::class) of type object<PHPUnit_Framework_MockObject_MockObject> is incompatible with the declared type object<Doctrine\DBAL\Platforms\AbstractPlatform> of property $abstractPlatform.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
        $this->abstractPlatform->method('getDateTimeFormatString')->willReturn('Y-m-d H:i:s');
36
    }
37
38
    /**
39
     * @test
40
     */
41
    public function it_converts_datetime_object_to_UTC_based_database_value()
0 ignored issues
show
Coding Style introduced by
function it_converts_dat..._based_database_value() does not seem to conform to the naming convention (^(?:(?:[a-z]|__)[a-zA-Z0-9]*|[a-z][a-z0-9_]*)$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
42
    {
43
        Assert::assertSame(
44
            '2016-12-31 19:00:00',
45
            $this->type->convertToDatabaseValue(
46
                \DateTime::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('GMT+0600')),
47
                $this->abstractPlatform
48
            )
49
        );
50
51
        Assert::assertSame(
52
            '2016-12-31 19:00:00',
53
            $this->type->convertToDatabaseValue(
54
                \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('GMT+0600')),
55
                $this->abstractPlatform
56
            )
57
        );
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function it_does_nothing_while_converting_null_to_UTC_based_database_value()
0 ignored issues
show
Coding Style introduced by
function it_does_nothing..._based_database_value() does not seem to conform to the naming convention (^(?:(?:[a-z]|__)[a-zA-Z0-9]*|[a-z][a-z0-9_]*)$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
64
    {
65
        Assert::assertNull($this->type->convertToDatabaseValue(null, $this->abstractPlatform));
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function it_converts_UTC_based_database_value_to_datetime_object()
0 ignored issues
show
Coding Style introduced by
function it_converts_UTC...ue_to_datetime_object() does not seem to conform to the naming convention (^(?:(?:[a-z]|__)[a-zA-Z0-9]*|[a-z][a-z0-9_]*)$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
72
    {
73
        Assert::assertEquals(
74
            \DateTime::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('UTC')),
75
            $this->type->convertToPHPValue('2017-01-01 01:00:00', $this->abstractPlatform)
76
        );
77
78
        Assert::assertEquals(
79
            \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('UTC')),
80
            $this->type->convertToPHPValue('2017-01-01 01:00:00', $this->abstractPlatform)
81
        );
82
    }
83
84
    /**
85
     * @test
86
     */
87
    public function it_does_nothing_while_converting_null_to_datetime_object()
88
    {
89
        Assert::assertNull($this->type->convertToPHPValue(null, $this->abstractPlatform));
90
    }
91
92
    /**
93
     * @test
94
     */
95
    public function it_does_nothing_while_converting_datetime_object_to_datetime_object()
96
    {
97
        $date = \DateTime::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('GMT+0600'));
98
        Assert::assertSame($date, $this->type->convertToPHPValue($date, $this->abstractPlatform));
99
100
        $date = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', '2017-01-01 01:00:00', new \DateTimeZone('GMT+0600'));
101
        Assert::assertSame($date, $this->type->convertToPHPValue($date, $this->abstractPlatform));
102
    }
103
104
    /**
105
     * @test
106
     * @expectedException \Doctrine\DBAL\Types\ConversionException
107
     */
108
    public function it_throws_an_exception_while_converting_invalid_date_to_datetime_object()
109
    {
110
        $this->type->convertToPHPValue('2017-02-30T01:00:00', $this->abstractPlatform);
111
    }
112
}
113