1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EcodevTests\Felix\DBAL\Types; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
8
|
|
|
use Ecodev\Felix\DBAL\Types\EnumType; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
final class EnumTypeTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var EnumType |
15
|
|
|
*/ |
16
|
|
|
private $type; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var AbstractPlatform |
20
|
|
|
*/ |
21
|
|
|
private $platform; |
22
|
|
|
|
23
|
|
|
public function setUp(): void |
24
|
|
|
{ |
25
|
|
|
$this->type = new class() extends EnumType { |
26
|
|
|
protected function getPossibleValues(): array |
27
|
|
|
{ |
28
|
|
|
return ['value1', 'value2']; |
29
|
|
|
} |
30
|
|
|
}; |
31
|
|
|
|
32
|
|
|
$this->platform = $this->createMock(AbstractPlatform::class); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testEnum(): void |
36
|
|
|
{ |
37
|
|
|
self::assertSame("ENUM('value1', 'value2')", $this->type->getSqlDeclaration(['foo'], $this->platform)); |
38
|
|
|
|
39
|
|
|
// Should always return string |
40
|
|
|
self::assertSame('value1', $this->type->convertToPHPValue('value1', $this->platform)); |
41
|
|
|
|
42
|
|
|
// Should support null values or empty string |
43
|
|
|
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); |
|
|
|
|
44
|
|
|
self::assertNull($this->type->convertToPHPValue('', $this->platform)); |
|
|
|
|
45
|
|
|
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); |
|
|
|
|
46
|
|
|
self::assertNull($this->type->convertToDatabaseValue('', $this->platform)); |
|
|
|
|
47
|
|
|
|
48
|
|
|
self::assertTrue($this->type->requiresSQLCommentHint($this->platform)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testConvertToPHPValueThrowsWithInvalidValue(): void |
52
|
|
|
{ |
53
|
|
|
$this->expectException(\InvalidArgumentException::class); |
54
|
|
|
|
55
|
|
|
$this->type->convertToPHPValue('foo', $this->platform); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testConvertToDatabaseValueThrowsWithInvalidValue(): void |
59
|
|
|
{ |
60
|
|
|
$this->expectException(\InvalidArgumentException::class); |
61
|
|
|
|
62
|
|
|
$this->type->convertToDatabaseValue('foo', $this->platform); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testConvertToPHPValueThrowsWithZero(): void |
66
|
|
|
{ |
67
|
|
|
$this->expectException(\InvalidArgumentException::class); |
68
|
|
|
|
69
|
|
|
$this->type->convertToPHPValue(0, $this->platform); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testConvertToDatabaseValueThrowsWithZero(): void |
73
|
|
|
{ |
74
|
|
|
$this->expectException(\InvalidArgumentException::class); |
75
|
|
|
|
76
|
|
|
$this->type->convertToDatabaseValue(0, $this->platform); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..