Passed
Pull Request — master (#3291)
by Tom
63:46
created

HPValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Types;
4
5
use Doctrine\DBAL\ParameterType;
6
use Doctrine\DBAL\Types\JsonArrayType;
7
use Doctrine\DBAL\Types\Type;
8
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
9
use Doctrine\Tests\DbalTestCase;
10
use function base64_encode;
11
use function fopen;
12
use function json_encode;
13
14
class JsonArrayTest extends DbalTestCase
15
{
16
    /** @var MockPlatform */
17
    protected $platform;
18
19
    /** @var JsonArrayType */
20
    protected $type;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function setUp() : void
26
    {
27
        $this->platform = new MockPlatform();
28
        $this->type     = Type::getType('json_array');
29
    }
30
31
    public function testReturnsBindingType()
32
    {
33
        self::assertSame(ParameterType::STRING, $this->type->getBindingType());
34
    }
35
36
    public function testReturnsName()
37
    {
38
        self::assertSame(Type::JSON_ARRAY, $this->type->getName());
39
    }
40
41
    public function testReturnsSQLDeclaration()
42
    {
43
        self::assertSame('DUMMYJSON', $this->type->getSQLDeclaration([], $this->platform));
44
    }
45
46
    public function testJsonNullConvertsToPHPValue()
47
    {
48
        self::assertSame(null, $this->type->convertToPHPValue(null, $this->platform));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->convertToPH...(null, $this->platform) targeting Doctrine\DBAL\Types\Json...pe::convertToPHPValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
49
    }
50
51
    public function testJsonNormalizesToPHPValue()
52
    {
53
        self::assertSame([], $this->type->normalizeToPHPValue('', $this->platform));
54
        self::assertSame(null, $this->type->normalizeToPHPValue(null, $this->platform));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->type->normalizeTo...(null, $this->platform) targeting Doctrine\DBAL\Types\Json...::normalizeToPHPValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
55
    }
56
57
    public function testJsonStringConvertsToPHPValue()
58
    {
59
        $value         = ['foo' => 'bar', 'bar' => 'foo'];
60
        $databaseValue = json_encode($value);
61
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);
62
63
        self::assertEquals($value, $phpValue);
64
    }
65
66
    public function testJsonResourceConvertsToPHPValue()
67
    {
68
        $value         = ['foo' => 'bar', 'bar' => 'foo'];
69
        $databaseValue = fopen('data://text/plain;base64,' . base64_encode(json_encode($value)), 'r');
70
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);
71
72
        self::assertSame($value, $phpValue);
73
    }
74
75
    public function testRequiresSQLCommentHint()
76
    {
77
        self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
78
    }
79
}
80