JsonTest.php 2.81 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
use function base64_encode;
use function fopen;
use function json_encode;
16

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

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

    /**
     * {@inheritdoc}
     */
28
    protected function setUp() : void
29
    {
30
        $this->platform = $this->createMock(AbstractPlatform::class);
31 32 33
        $this->type     = Type::getType('json');
    }

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

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

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

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

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

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

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

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 82
    /**
     * @return mixed[][]
     */
    public static function providerFailure() : iterable
83
    {
Sergei Morozov's avatar
Sergei Morozov committed
84
        return [['a'], ['{']];
85 86
    }

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

93
        self::assertSame($value, $phpValue);
94 95
    }

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