|
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
|
|
|
public 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
|
|
|
/** @test */ |
|
32
|
|
|
public function nameIsReturned(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->assertEquals(ChronosDateTimeType::CHRONOS_DATETIME, $this->type->getName()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @test |
|
39
|
|
|
* @dataProvider provideValues |
|
40
|
|
|
*/ |
|
41
|
|
|
public function valueIsConverted(?string $value, ?string $expected): void |
|
42
|
|
|
{ |
|
43
|
|
|
$platform = $this->prophesize(AbstractPlatform::class); |
|
44
|
|
|
$platform->getDateTimeFormatString()->willReturn('Y-m-d H:i:s'); |
|
45
|
|
|
|
|
46
|
|
|
$result = $this->type->convertToPHPValue($value, $platform->reveal()); |
|
47
|
|
|
|
|
48
|
|
|
if ($expected === null) { |
|
49
|
|
|
$this->assertNull($result); |
|
50
|
|
|
} else { |
|
51
|
|
|
$this->assertInstanceOf($expected, $result); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function provideValues(): iterable |
|
56
|
|
|
{ |
|
57
|
|
|
yield 'null date' => [null, null]; |
|
58
|
|
|
yield 'human friendly date' => ['now', Chronos::class]; |
|
59
|
|
|
yield 'numeric date' => ['2017-01-01', Chronos::class]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @test |
|
64
|
|
|
* @dataProvider providePhpValues |
|
65
|
|
|
*/ |
|
66
|
|
|
public function valueIsConvertedToDatabaseFormat(?DateTimeInterface $value, ?string $expected): void |
|
67
|
|
|
{ |
|
68
|
|
|
$platform = $this->prophesize(AbstractPlatform::class); |
|
69
|
|
|
$platform->getDateTimeFormatString()->willReturn('Y-m-d'); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals($expected, $this->type->convertToDatabaseValue($value, $platform->reveal())); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function providePhpValues(): iterable |
|
75
|
|
|
{ |
|
76
|
|
|
yield 'null date' => [null, null]; |
|
77
|
|
|
yield 'DateTimeImmutable date' => [new DateTimeImmutable('2017-01-01'), '2017-01-01']; |
|
78
|
|
|
yield 'Chronos date' => [Chronos::parse('2017-02-01'), '2017-02-01']; |
|
79
|
|
|
yield 'DateTime date' => [new DateTime('2017-03-01'), '2017-03-01']; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** @test */ |
|
83
|
|
|
public function exceptionIsThrownIfInvalidValueIsParsedToDatabase(): void |
|
84
|
|
|
{ |
|
85
|
|
|
$this->expectException(ConversionException::class); |
|
86
|
|
|
$this->type->convertToDatabaseValue(new stdClass(), $this->prophesize(AbstractPlatform::class)->reveal()); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|