JsonTest.php 2.76 KB
Newer Older
1 2
<?php

3
namespace Doctrine\DBAL\Tests\Types;
4

5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
Sergei Morozov's avatar
Sergei Morozov committed
7
use Doctrine\DBAL\Types\ConversionException;
Sergei Morozov's avatar
Sergei Morozov committed
8
use Doctrine\DBAL\Types\JsonType;
9
use Doctrine\DBAL\Types\Type;
10
use Doctrine\DBAL\Types\Types;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use PHPUnit\Framework\TestCase;
13

14 15 16
use function base64_encode;
use function fopen;
use function json_encode;
17

18
class JsonTest extends TestCase
19
{
20
    /** @var AbstractPlatform|MockObject */
21 22
    protected $platform;

Sergei Morozov's avatar
Sergei Morozov committed
23
    /** @var JsonType */
24 25
    protected $type;

26
    protected function setUp(): void
27
    {
28
        $this->platform = $this->createMock(AbstractPlatform::class);
29 30 31
        $this->type     = Type::getType('json');
    }

32
    public function testReturnsBindingType(): void
33
    {
34
        self::assertSame(ParameterType::STRING, $this->type->getBindingType());
35 36
    }

37
    public function testReturnsName(): void
38
    {
39
        self::assertSame(Types::JSON, $this->type->getName());
40 41
    }

42
    public function testReturnsSQLDeclaration(): void
43
    {
44
        $this->platform->expects(self::once())
45 46 47 48
            ->method('getJsonTypeDeclarationSQL')
            ->willReturn('TEST_JSON');

        self::assertSame('TEST_JSON', $this->type->getSQLDeclaration([], $this->platform));
49 50
    }

51
    public function testJsonNullConvertsToPHPValue(): void
52
    {
53
        self::assertNull($this->type->convertToPHPValue(null, $this->platform));
54 55
    }

56
    public function testJsonEmptyStringConvertsToPHPValue(): void
57
    {
58
        self::assertNull($this->type->convertToPHPValue('', $this->platform));
59 60
    }

61
    public function testJsonStringConvertsToPHPValue(): void
62
    {
Sergei Morozov's avatar
Sergei Morozov committed
63
        $value         = ['foo' => 'bar', 'bar' => 'foo'];
64 65 66
        $databaseValue = json_encode($value);
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);

67
        self::assertEquals($value, $phpValue);
68 69 70
    }

    /** @dataProvider providerFailure */
71
    public function testConversionFailure(string $data): void
72
    {
Sergei Morozov's avatar
Sergei Morozov committed
73
        $this->expectException(ConversionException::class);
74 75 76
        $this->type->convertToPHPValue($data, $this->platform);
    }

77 78 79
    /**
     * @return mixed[][]
     */
80
    public static function providerFailure(): iterable
81
    {
Sergei Morozov's avatar
Sergei Morozov committed
82
        return [['a'], ['{']];
83 84
    }

85
    public function testJsonResourceConvertsToPHPValue(): void
86
    {
Sergei Morozov's avatar
Sergei Morozov committed
87
        $value         = ['foo' => 'bar', 'bar' => 'foo'];
88 89 90
        $databaseValue = fopen('data://text/plain;base64,' . base64_encode(json_encode($value)), 'r');
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);

91
        self::assertSame($value, $phpValue);
92 93
    }

94
    public function testRequiresSQLCommentHint(): void
95
    {
96
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
97 98
    }
}