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
|
|
|
|
38
|
|
|
// Should always return string |
39
|
|
|
self::assertSame(TestEnum::key1, $this->type->convertToPHPValue('value1', $this->platform)); |
40
|
|
|
|
41
|
|
|
// Should support null values or empty string |
42
|
|
|
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); |
43
|
|
|
self::assertNull($this->type->convertToPHPValue('', $this->platform)); |
|
|
|
|
44
|
|
|
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); |
45
|
|
|
|
46
|
|
|
self::assertTrue($this->type->requiresSQLCommentHint($this->platform)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testConvertToPHPValueThrowsWithInvalidValue(): void |
50
|
|
|
{ |
51
|
|
|
$this->expectException(ValueError::class); |
52
|
|
|
|
53
|
|
|
$this->type->convertToPHPValue('foo', $this->platform); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testConvertToDatabaseValueThrowsWithInvalidValue(): void |
57
|
|
|
{ |
58
|
|
|
$this->expectException(InvalidArgumentException::class); |
59
|
|
|
|
60
|
|
|
$this->type->convertToDatabaseValue('foo', $this->platform); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testConvertToDatabaseValueThrowsWithInvalidEnum(): void |
64
|
|
|
{ |
65
|
|
|
$this->expectException(InvalidArgumentException::class); |
66
|
|
|
|
67
|
|
|
$this->type->convertToDatabaseValue(OtherTestEnum::key1, $this->platform); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testConvertToPHPValueThrowsWithZero(): void |
71
|
|
|
{ |
72
|
|
|
$this->expectException(InvalidArgumentException::class); |
73
|
|
|
|
74
|
|
|
$this->type->convertToPHPValue(0, $this->platform); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testConvertToDatabaseValueThrowsWithZero(): void |
78
|
|
|
{ |
79
|
|
|
$this->expectException(InvalidArgumentException::class); |
80
|
|
|
|
81
|
|
|
$this->type->convertToDatabaseValue(0, $this->platform); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.