JsonTest.php 2.77 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
    protected $type;

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

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

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

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

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

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

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

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

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

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

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

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

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

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