JsonArrayTest.php 2.43 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Types;

5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
Sergei Morozov's avatar
Sergei Morozov committed
7
use Doctrine\DBAL\Types\JsonArrayType;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\DBAL\Types\Types;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12 13 14
use function base64_encode;
use function fopen;
use function json_encode;
15

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

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

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

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

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

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

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

52
    public function testJsonNullConvertsToPHPValue() : void
53
    {
Sergei Morozov's avatar
Sergei Morozov committed
54
        self::assertSame([], $this->type->convertToPHPValue(null, $this->platform));
55 56
    }

57
    public function testJsonEmptyStringConvertsToPHPValue() : void
58
    {
Sergei Morozov's avatar
Sergei Morozov committed
59
        self::assertSame([], $this->type->convertToPHPValue('', $this->platform));
60 61
    }

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

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

71
    public function testJsonResourceConvertsToPHPValue() : void
72
    {
Sergei Morozov's avatar
Sergei Morozov committed
73
        $value         = ['foo' => 'bar', 'bar' => 'foo'];
74 75 76
        $databaseValue = fopen('data://text/plain;base64,' . base64_encode(json_encode($value)), 'r');
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);

77
        self::assertSame($value, $phpValue);
78 79
    }

80
    public function testRequiresSQLCommentHint() : void
81
    {
82
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
83 84
    }
}