|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\DBAL\Types; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
8
|
|
|
use Doctrine\DBAL\Platforms\MySQLPlatform; |
|
9
|
|
|
use Ecodev\Felix\DBAL\Types\PhpEnumType; |
|
10
|
|
|
use EcodevTests\Felix\Service\OtherTestEnum; |
|
11
|
|
|
use EcodevTests\Felix\Service\TestEnum; |
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use ValueError; |
|
15
|
|
|
|
|
16
|
|
|
class PhpEnumTypeTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
private PhpEnumType $type; |
|
19
|
|
|
|
|
20
|
|
|
private AbstractPlatform $platform; |
|
21
|
|
|
|
|
22
|
|
|
protected function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->type = new class() extends PhpEnumType { |
|
25
|
|
|
protected function getEnumType(): string |
|
26
|
|
|
{ |
|
27
|
|
|
return TestEnum::class; |
|
28
|
|
|
} |
|
29
|
|
|
}; |
|
30
|
|
|
|
|
31
|
|
|
$this->platform = new MySQLPlatform(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testEnum(): void |
|
35
|
|
|
{ |
|
36
|
|
|
self::assertSame("ENUM('value1', 'value2')", $this->type->getSqlDeclaration(['foo'], $this->platform)); |
|
37
|
|
|
self::assertTrue($this->type->requiresSQLCommentHint($this->platform)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @dataProvider providerConvertToPHPValue |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testConvertToPHPValue(?string $input, ?TestEnum $expected): void |
|
44
|
|
|
{ |
|
45
|
|
|
self::assertSame($expected, $this->type->convertToPHPValue($input, $this->platform)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function providerConvertToPHPValue(): iterable |
|
49
|
|
|
{ |
|
50
|
|
|
yield ['value1', TestEnum::key1]; |
|
51
|
|
|
yield [null, null]; |
|
52
|
|
|
yield ['', null]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @dataProvider providerConvertToDatabaseValue |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testConvertToDatabaseValue(mixed $input, ?string $expected): void |
|
59
|
|
|
{ |
|
60
|
|
|
self::assertSame($expected, $this->type->convertToDatabaseValue($input, $this->platform)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function providerConvertToDatabaseValue(): iterable |
|
64
|
|
|
{ |
|
65
|
|
|
yield [null, null]; |
|
66
|
|
|
yield [TestEnum::key1, 'value1']; |
|
67
|
|
|
yield ['value1', 'value1']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @dataProvider providerInvalidConvertToDatabaseValue |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testInvalidConvertToDatabaseValue(mixed $input): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
76
|
|
|
|
|
77
|
|
|
$this->type->convertToDatabaseValue($input, $this->platform); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function providerInvalidConvertToDatabaseValue(): iterable |
|
81
|
|
|
{ |
|
82
|
|
|
yield ['foo']; |
|
83
|
|
|
yield ['key1']; |
|
84
|
|
|
yield [OtherTestEnum::key1]; |
|
85
|
|
|
yield [0]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testConvertToPHPValueThrowsWithInvalidValue(): void |
|
89
|
|
|
{ |
|
90
|
|
|
$this->expectException(ValueError::class); |
|
91
|
|
|
|
|
92
|
|
|
$this->type->convertToPHPValue('foo', $this->platform); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testConvertToPHPValueThrowsWithZero(): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
98
|
|
|
|
|
99
|
|
|
$this->type->convertToPHPValue(0, $this->platform); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|