1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ShlinkioTest\Shlink\Common\Doctrine\Type; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
8
|
|
|
use DateTime; |
9
|
|
|
use DateTimeImmutable; |
10
|
|
|
use DateTimeInterface; |
11
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
12
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
13
|
|
|
use Doctrine\DBAL\Types\Type; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Shlinkio\Shlink\Common\Doctrine\Type\ChronosDateTimeType; |
16
|
|
|
use stdClass; |
17
|
|
|
|
18
|
|
|
class ChronosDateTimeTypeTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** @var ChronosDateTimeType */ |
21
|
|
|
private $type; |
22
|
|
|
|
23
|
|
|
protected function setUp(): void |
24
|
|
|
{ |
25
|
|
|
if (! Type::hasType(ChronosDateTimeType::CHRONOS_DATETIME)) { |
26
|
|
|
Type::addType(ChronosDateTimeType::CHRONOS_DATETIME, ChronosDateTimeType::class); |
27
|
|
|
} else { |
28
|
|
|
Type::overrideType(ChronosDateTimeType::CHRONOS_DATETIME, ChronosDateTimeType::class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->type = Type::getType(ChronosDateTimeType::CHRONOS_DATETIME); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** @test */ |
35
|
|
|
public function nameIsReturned(): void |
36
|
|
|
{ |
37
|
|
|
$this->assertEquals(ChronosDateTimeType::CHRONOS_DATETIME, $this->type->getName()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @test |
42
|
|
|
* @dataProvider provideValues |
43
|
|
|
*/ |
44
|
|
|
public function valueIsConverted(?string $value, ?string $expected): void |
45
|
|
|
{ |
46
|
|
|
$platform = $this->prophesize(AbstractPlatform::class); |
47
|
|
|
$platform->getDateTimeFormatString()->willReturn('Y-m-d H:i:s'); |
48
|
|
|
|
49
|
|
|
$result = $this->type->convertToPHPValue($value, $platform->reveal()); |
50
|
|
|
|
51
|
|
|
if ($expected === null) { |
52
|
|
|
$this->assertNull($result); |
53
|
|
|
} else { |
54
|
|
|
$this->assertInstanceOf($expected, $result); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function provideValues(): iterable |
59
|
|
|
{ |
60
|
|
|
yield 'null date' => [null, null]; |
61
|
|
|
yield 'human friendly date' => ['now', Chronos::class]; |
62
|
|
|
yield 'numeric date' => ['2017-01-01', Chronos::class]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
* @dataProvider providePhpValues |
68
|
|
|
*/ |
69
|
|
|
public function valueIsConvertedToDatabaseFormat(?DateTimeInterface $value, ?string $expected): void |
70
|
|
|
{ |
71
|
|
|
$platform = $this->prophesize(AbstractPlatform::class); |
72
|
|
|
$platform->getDateTimeFormatString()->willReturn('Y-m-d'); |
73
|
|
|
|
74
|
|
|
$this->assertEquals($expected, $this->type->convertToDatabaseValue($value, $platform->reveal())); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function providePhpValues(): iterable |
78
|
|
|
{ |
79
|
|
|
yield 'null date' => [null, null]; |
80
|
|
|
yield 'DateTimeImmutable date' => [new DateTimeImmutable('2017-01-01'), '2017-01-01']; |
81
|
|
|
yield 'Chronos date' => [Chronos::parse('2017-02-01'), '2017-02-01']; |
82
|
|
|
yield 'DateTime date' => [new DateTime('2017-03-01'), '2017-03-01']; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** @test */ |
86
|
|
|
public function exceptionIsThrownIfInvalidValueIsParsedToDatabase(): void |
87
|
|
|
{ |
88
|
|
|
$this->expectException(ConversionException::class); |
89
|
|
|
$this->type->convertToDatabaseValue(new stdClass(), $this->prophesize(AbstractPlatform::class)->reveal()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|