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

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5
namespace Doctrine\DBAL\Tests\Types;
6

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

15 16
use function base64_encode;
use function fopen;
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);
Sergei Morozov's avatar
Sergei Morozov committed
29
        $this->type     = new JsonType();
30 31
    }

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
    {
63 64 65 66 67
        $value = ['foo' => 'bar', 'bar' => 'foo'];

        $databaseValue = '{"foo":"bar","bar":"foo"}';

        $phpValue = $this->type->convertToPHPValue($databaseValue, $this->platform);
68

69
        self::assertEquals($value, $phpValue);
70 71 72
    }

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

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

87
    public function testJsonResourceConvertsToPHPValue(): void
88
    {
89 90 91 92 93
        $value = ['foo' => 'bar', 'bar' => 'foo'];

        $json = '{"foo":"bar","bar":"foo"}';

        $databaseValue = fopen('data://text/plain;base64,' . base64_encode($json), 'r');
94 95
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);

96
        self::assertSame($value, $phpValue);
97 98
    }

99
    public function testRequiresSQLCommentHint(): void
100
    {
101
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
102 103
    }
}