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