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

VarDateTimeTest::testConversionWithMicroseconds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
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 Doctrine\DBAL\Types\ConversionException;
7
use Doctrine\DBAL\Types\Type;
8
use Doctrine\DBAL\Types\VarDateTimeType;
9
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
10
use Doctrine\Tests\DbalTestCase;
11
12
class VarDateTimeTest extends DbalTestCase
13
{
14
    /** @var MockPlatform */
15
    private $platform;
16
17
    /** @var Type */
18
    private $type;
19
20
    protected function setUp() : void
21
    {
22
        $this->platform = new MockPlatform();
23
        if (! Type::hasType('vardatetime')) {
24
            Type::addType('vardatetime', VarDateTimeType::class);
25
        }
26
        $this->type = Type::getType('vardatetime');
27
    }
28
29
    public function testDateTimeConvertsToDatabaseValue()
30
    {
31
        $date = new DateTime('1985-09-01 10:10:10');
32
33
        $expected = $date->format($this->platform->getDateTimeTzFormatString());
34
        $actual   = $this->type->convertToDatabaseValue($date, $this->platform);
35
36
        self::assertEquals($expected, $actual);
37
    }
38
39
    public function testDateTimeConvertsToPHPValue()
40
    {
41
        // Birthday of jwage and also birthday of Doctrine. Send him a present ;)
42
        $date = $this->type->convertToPHPValue('1985-09-01 00:00:00', $this->platform);
43
        self::assertInstanceOf('DateTime', $date);
44
        self::assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
45
        self::assertEquals('000000', $date->format('u'));
46
    }
47
48
    public function testInvalidDateTimeFormatConversion()
49
    {
50
        $this->expectException(ConversionException::class);
51
        $this->type->convertToPHPValue('abcdefg', $this->platform);
52
    }
53
54
    public function testConversionWithMicroseconds()
55
    {
56
        $date = $this->type->convertToPHPValue('1985-09-01 00:00:00.123456', $this->platform);
57
        self::assertInstanceOf('DateTime', $date);
58
        self::assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
59
        self::assertEquals('123456', $date->format('u'));
60
    }
61
62
    public function testNullConversion()
63
    {
64
        self::assertNull($this->type->convertToPHPValue(null, $this->platform));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->convertToPH...(null, $this->platform) targeting Doctrine\DBAL\Types\Type::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...
65
    }
66
67
    public function testNormalizesDateTimeToPHPValue()
68
    {
69
        $date = new DateTime('now');
70
        self::assertSame($date, $this->type->normalizeToPHPValue($date, $this->platform));
71
    }
72
}
73