|
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\LocalizedType; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
class LocalizedTypeTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var LocalizedType |
|
16
|
|
|
*/ |
|
17
|
|
|
private $type; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var AbstractPlatform |
|
21
|
|
|
*/ |
|
22
|
|
|
private $platform; |
|
23
|
|
|
|
|
24
|
|
|
protected function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$this->type = new LocalizedType(); |
|
27
|
|
|
$this->platform = new MySqlPlatform(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testConvertToPHPValue(): void |
|
31
|
|
|
{ |
|
32
|
|
|
self::assertSame([], $this->type->convertToPHPValue(null, $this->platform)); |
|
33
|
|
|
self::assertSame([], $this->type->convertToPHPValue('', $this->platform)); |
|
34
|
|
|
self::assertSame(['fr' => 'foo'], $this->type->convertToPHPValue('{"fr":"foo"}', $this->platform)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testConvertToDatabaseValue(): void |
|
38
|
|
|
{ |
|
39
|
|
|
self::assertSame('{"fr":"foo"}', $this->type->convertToDatabaseValue(['fr' => 'foo'], $this->platform)); |
|
40
|
|
|
self::assertSame('', $this->type->convertToDatabaseValue([], $this->platform), 'micro-optimization of an empty array into an empty string to save two bytes'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testConvertToDatabaseValueWillThrowIfNull(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$this->expectExceptionMessage("Could not convert PHP type 'NULL' to 'json', as an 'value must be a PHP array' error was triggered by the serialization"); |
|
46
|
|
|
$this->type->convertToDatabaseValue(null, $this->platform); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testConvertToDatabaseValueWillThrowIfString(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->expectExceptionMessage("Could not convert PHP type 'string' to 'json', as an 'value must be a PHP array' error was triggered by the serialization"); |
|
52
|
|
|
$this->type->convertToDatabaseValue('', $this->platform); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|