|
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\SetType; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
|
|
13
|
|
|
final class SetTypeTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
private SetType $type; |
|
16
|
|
|
|
|
17
|
|
|
private AbstractPlatform $platform; |
|
18
|
|
|
|
|
19
|
|
|
protected function setUp(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$this->type = new ExampleSet(); |
|
22
|
|
|
$this->platform = new MySQLPlatform(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testSet(): void |
|
26
|
|
|
{ |
|
27
|
|
|
self::assertSame("SET('value1', 'value2')", $this->type->getSqlDeclaration(['foo' => 'bar'], $this->platform)); |
|
28
|
|
|
|
|
29
|
|
|
// Should always return string |
|
30
|
|
|
self::assertSame(['value1', 'value2'], $this->type->convertToPHPValue('value1,value2', $this->platform)); |
|
31
|
|
|
|
|
32
|
|
|
// Should support null values or empty string |
|
33
|
|
|
self::assertNull($this->type->convertToPHPValue(null, $this->platform)); |
|
34
|
|
|
self::assertSame([], $this->type->convertToPHPValue('', $this->platform)); |
|
35
|
|
|
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform)); |
|
36
|
|
|
self::assertSame('', $this->type->convertToDatabaseValue([], $this->platform)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testConvertToPHPValueThrowsWithInvalidValue(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
42
|
|
|
$this->expectExceptionMessage("Invalid 'foo' value fetched from database for set ExampleSet"); |
|
43
|
|
|
|
|
44
|
|
|
$this->type->convertToPHPValue('foo', $this->platform); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testConvertToDatabaseValueThrowsWithInvalidValue(): void |
|
48
|
|
|
{ |
|
49
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
50
|
|
|
$this->expectExceptionMessage("Invalid 'foo' value to be stored in database for set ExampleSet"); |
|
51
|
|
|
|
|
52
|
|
|
$this->type->convertToDatabaseValue(['foo'], $this->platform); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testConvertToDatabaseValueThrowsWithInvalidType(): void |
|
56
|
|
|
{ |
|
57
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
58
|
|
|
$this->expectExceptionMessage("Invalid '' value to be stored in database for set ExampleSet"); |
|
59
|
|
|
|
|
60
|
|
|
$this->type->convertToDatabaseValue('foo', $this->platform); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testConvertToPHPValueThrowsWithZero(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
66
|
|
|
$this->expectExceptionMessage("Invalid '0' value fetched from database for set ExampleSet"); |
|
67
|
|
|
|
|
68
|
|
|
$this->type->convertToPHPValue(0, $this->platform); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testConvertToDatabaseValueThrowsWithZero(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
74
|
|
|
$this->expectExceptionMessage("Invalid '' value to be stored in database for set ExampleSet"); |
|
75
|
|
|
|
|
76
|
|
|
$this->type->convertToDatabaseValue(0, $this->platform); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|