Failed Conditions
Push — master ( e75200...fab761 )
by Adrien
02:43
created

testConvertToDatabaseValueThrowsWithInvalidValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EcodevTests\Felix\DBAL\Types;
6
7
use Cake\Chronos\Chronos;
1 ignored issue
show
Bug introduced by
The type Cake\Chronos\Chronos was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use DateTimeImmutable;
9
use Doctrine\DBAL\Platforms\AbstractPlatform;
10
use Doctrine\DBAL\Platforms\MySQLPlatform;
11
use Doctrine\DBAL\Types\ConversionException;
12
use Ecodev\Felix\DBAL\Types\ChronosType;
13
use PHPUnit\Framework\TestCase;
14
15
class ChronosTypeTest extends TestCase
16
{
17
    private ChronosType $type;
18
19
    private AbstractPlatform $platform;
20
21
    protected function setUp(): void
22
    {
23
        $this->type = new ChronosType();
24
        $this->platform = new MySQLPlatform();
25
    }
26
27
    public function testConvertToDatabaseValue(): void
28
    {
29
        self::assertSame('DATETIME', $this->type->getSqlDeclaration(['foo'], $this->platform));
30
        self::assertFalse($this->type->requiresSQLCommentHint($this->platform));
31
32
        $actual = $this->type->convertToDatabaseValue(new Chronos('2016-01-01 15:58:59'), $this->platform);
33
        self::assertSame('2016-01-01 15:58:59', $actual, 'support Chronos');
34
35
        $actual = $this->type->convertToDatabaseValue(new DateTimeImmutable('2016-01-01 15:58:59'), $this->platform);
36
        self::assertSame('2016-01-01 15:58:59', $actual, 'support DateTimeImmutable');
37
38
        self::assertNull($this->type->convertToDatabaseValue(null, $this->platform), 'support null values');
39
    }
40
41
    public function testConvertToPHPValue(): void
42
    {
43
        $actualPhp = $this->type->convertToPHPValue('2016-01-01 15:58:59', $this->platform);
44
        self::assertInstanceOf(Chronos::class, $actualPhp);
45
        self::assertSame('2016-01-01 15:58:59', $actualPhp->__toString(), 'support string');
46
47
        $actualPhp = $this->type->convertToPHPValue(new Chronos('2016-01-01 15:58:59'), $this->platform);
48
        self::assertInstanceOf(Chronos::class, $actualPhp);
49
        self::assertSame('2016-01-01 15:58:59', $actualPhp->__toString(), 'support Chronos');
50
51
        $actualPhp = $this->type->convertToPHPValue(new DateTimeImmutable('2016-01-01 15:58:59'), $this->platform);
52
        self::assertInstanceOf(Chronos::class, $actualPhp);
53
        self::assertSame('2016-01-01 15:58:59', $actualPhp->__toString(), 'support DateTimeImmutable');
54
55
        self::assertNull($this->type->convertToPHPValue(null, $this->platform), 'support null values');
56
    }
57
58
    public function testConvertToPHPValueThrowsWithInvalidValue(): void
59
    {
60
        $this->expectException(ConversionException::class);
61
62
        $this->type->convertToPHPValue(123, $this->platform);
63
    }
64
65
    public function testConvertToDatabaseValueThrowsWithInvalidValue(): void
66
    {
67
        $this->expectException(ConversionException::class);
68
69
        $this->type->convertToDatabaseValue(123, $this->platform);
70
    }
71
}
72