|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Common\Type; |
|
5
|
|
|
|
|
6
|
|
|
use Cake\Chronos\Chronos; |
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
8
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
|
9
|
|
|
use Doctrine\DBAL\Types\Type; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use Shlinkio\Shlink\Common\Type\ChronosDateTimeType; |
|
12
|
|
|
|
|
13
|
|
|
class ChronosDateTimeTypeTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ChronosDateTimeType |
|
17
|
|
|
*/ |
|
18
|
|
|
private $type; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp() |
|
21
|
|
|
{ |
|
22
|
|
|
if (! Type::hasType(ChronosDateTimeType::CHRONOS_DATETIME)) { |
|
23
|
|
|
Type::addType(ChronosDateTimeType::CHRONOS_DATETIME, ChronosDateTimeType::class); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$this->type = Type::getType(ChronosDateTimeType::CHRONOS_DATETIME); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @test |
|
31
|
|
|
*/ |
|
32
|
|
|
public function nameIsReturned() |
|
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) |
|
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(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
[null, null], |
|
59
|
|
|
['now', Chronos::class], |
|
60
|
|
|
['2017-01-01', Chronos::class], |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @test |
|
66
|
|
|
* @dataProvider providePhpValues |
|
67
|
|
|
*/ |
|
68
|
|
|
public function valueIsConvertedToDatabaseFormat(?\DateTimeInterface $value, ?string $expected) |
|
69
|
|
|
{ |
|
70
|
|
|
$platform = $this->prophesize(AbstractPlatform::class); |
|
71
|
|
|
$platform->getDateTimeFormatString()->willReturn('Y-m-d'); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertEquals($expected, $this->type->convertToDatabaseValue($value, $platform->reveal())); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function providePhpValues(): array |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
[null, null], |
|
80
|
|
|
[new \DateTimeImmutable('2017-01-01'), '2017-01-01'], |
|
81
|
|
|
[Chronos::parse('2017-02-01'), '2017-02-01'], |
|
82
|
|
|
[new \DateTime('2017-03-01'), '2017-03-01'], |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @test |
|
88
|
|
|
*/ |
|
89
|
|
|
public function exceptionIsThrownIfInvalidValueIsParsedToDatabase() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->expectException(ConversionException::class); |
|
92
|
|
|
$this->type->convertToDatabaseValue(new \stdClass(), $this->prophesize(AbstractPlatform::class)->reveal()); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|