LocalizedTypeTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 3
b 0
f 0
dl 0
loc 52
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testConvertToPHPValueWillThrowIfNotJsonArray() 0 4 1
A testConvertToDatabaseValueWillThrowIfString() 0 4 1
A testConvertToDatabaseValueWillThrowIfNull() 0 4 1
A setUp() 0 4 1
A testConvertToDatabaseValue() 0 4 1
A testConvertToPHPValue() 0 5 1
A testMustAlwaysStoreUnescaped() 0 9 1
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
    private LocalizedType $type;
15
16
    private AbstractPlatform $platform;
17
18
    protected function setUp(): void
19
    {
20
        $this->type = new LocalizedType();
21
        $this->platform = new MySQLPlatform();
22
    }
23
24
    public function testConvertToPHPValue(): void
25
    {
26
        self::assertSame([], $this->type->convertToPHPValue(null, $this->platform));
27
        self::assertSame([], $this->type->convertToPHPValue('', $this->platform));
28
        self::assertSame(['fr' => 'foo'], $this->type->convertToPHPValue('{"fr":"foo"}', $this->platform));
29
    }
30
31
    public function testConvertToDatabaseValue(): void
32
    {
33
        self::assertSame('{"fr":"foo"}', $this->type->convertToDatabaseValue(['fr' => 'foo'], $this->platform));
34
        self::assertSame('{}', $this->type->convertToDatabaseValue([], $this->platform), 'empty should still be valid JSON');
35
    }
36
37
    public function testConvertToDatabaseValueWillThrowIfNull(): void
38
    {
39
        $this->expectExceptionMessage('Could not convert PHP type "null" to "json". An error was triggered by the serialization: value must be a PHP array');
40
        $this->type->convertToDatabaseValue(null, $this->platform);
41
    }
42
43
    public function testConvertToDatabaseValueWillThrowIfString(): void
44
    {
45
        $this->expectExceptionMessage('Could not convert PHP type "string" to "json". An error was triggered by the serialization: value must be a PHP array');
46
        $this->type->convertToDatabaseValue('', $this->platform);
47
    }
48
49
    public function testConvertToPHPValueWillThrowIfNotJsonArray(): void
50
    {
51
        $this->expectExceptionMessage('Could not convert database value to "json" as an error was triggered by the unserialization: value in DB is not a JSON encoded associative array');
52
        $this->type->convertToPHPValue('"foo"', $this->platform);
53
    }
54
55
    public function testMustAlwaysStoreUnescaped(): void
56
    {
57
        $original = ['fr' => 'aéa/a💕a'];
58
59
        $actualDB = $this->type->convertToDatabaseValue($original, $this->platform);
60
        self::assertSame('{"fr":"aéa/a💕a"}', $actualDB, 'unicode and slashes should not be escaped');
61
62
        $actualPHP = $this->type->convertToPHPValue($actualDB, $this->platform);
63
        self::assertSame($original, $actualPHP, 'can be re-converted back to the exact same original');
64
    }
65
}
66