testConvertToDatabaseValueThrowsWithInvalidValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
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\ChronosTime;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Platforms\MySQLPlatform;
10
use Doctrine\DBAL\Types\Exception\InvalidFormat;
11
use Doctrine\DBAL\Types\Exception\InvalidType;
12
use Ecodev\Felix\DBAL\Types\TimeType;
13
use PHPUnit\Framework\TestCase;
14
15
class TimeTypeTest extends TestCase
16
{
17
    private TimeType $type;
18
19
    private AbstractPlatform $platform;
20
21
    protected function setUp(): void
22
    {
23
        $this->type = new TimeType();
24
        $this->platform = new MySQLPlatform();
25
    }
26
27
    public function testConvertToDatabaseValue(): void
28
    {
29
        self::assertSame('TIME', $this->type->getSqlDeclaration(['foo' => 'bar'], $this->platform));
30
31
        $actual = $this->type->convertToDatabaseValue(new ChronosTime('09:33'), $this->platform);
32
        self::assertSame('09:33:00', $actual, 'support Chronos');
33
34
        self::assertNull($this->type->convertToDatabaseValue(null, $this->platform), 'support null values');
35
    }
36
37
    public function testConvertToPHPValue(): void
38
    {
39
        $actualPhp = $this->type->convertToPHPValue('18:59:23', $this->platform);
40
        self::assertInstanceOf(ChronosTime::class, $actualPhp);
41
        self::assertSame('18:59:23', $actualPhp->__toString(), 'support string');
42
43
        $actualPhp = $this->type->convertToPHPValue(new ChronosTime('18:59:23'), $this->platform);
44
        self::assertInstanceOf(ChronosTime::class, $actualPhp);
45
        self::assertSame('18:59:23', $actualPhp->__toString(), 'support ChronosTime');
46
47
        self::assertNull($this->type->convertToPHPValue(null, $this->platform), 'support null values');
48
    }
49
50
    public function testConvertToPHPValueThrowsWithInvalidValue(): void
51
    {
52
        $this->expectException(InvalidFormat::class);
53
54
        $this->type->convertToPHPValue(123, $this->platform);
55
    }
56
57
    public function testConvertToDatabaseValueThrowsWithInvalidValue(): void
58
    {
59
        $this->expectException(InvalidType::class);
60
61
        $this->type->convertToDatabaseValue(123, $this->platform);
62
    }
63
}
64