Completed
Pull Request — master (#3291)
by Tom
12:57
created

testArrayConvertsToPHPFailsWithArrayParameterValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Types;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Types\ConversionException;
7
use Doctrine\DBAL\Types\Type;
8
use Doctrine\Tests\DBAL\Mocks\MockPlatform;
9
use Doctrine\Tests\DbalTestCase;
10
use function serialize;
11
12
class ArrayTest extends DbalTestCase
13
{
14
    /** @var AbstractPlatform */
15
    private $platform;
16
17
    /** @var Type */
18
    private $type;
19
20
    protected function setUp()
21
    {
22
        $this->platform = new MockPlatform();
23
        $this->type     = Type::getType('array');
24
    }
25
26
    public function testArrayConvertsToDatabaseValue()
27
    {
28
        self::assertInternalType(
29
            'string',
30
            $this->type->convertToDatabaseValue([], $this->platform)
31
        );
32
    }
33
34
    public function testArrayConvertsToPHPValue()
35
    {
36
        self::assertInternalType(
37
            'array',
38
            $this->type->convertToPHPValue(serialize([]), $this->platform)
39
        );
40
    }
41
42
    public function testArrayConvertsToPHPFailsWithArrayParameterValue()
43
    {
44
        $this->expectException(ConversionException::class);
45
46
        self::assertInternalType(
47
            'array',
48
            $this->type->convertToPHPValue([], $this->platform)
49
        );
50
    }
51
52
    public function testArrayNormalizesToPHPValue()
53
    {
54
        self::assertInternalType(
55
            'array',
56
            $this->type->normalizeToPHPValue([], $this->platform)
57
        );
58
59
        self::assertInternalType(
60
            'null',
61
            $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\Type::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...
62
        );
63
64
        self::assertInternalType(
65
            'array',
66
            $this->type->convertToPHPValue(serialize([]), $this->platform)
67
        );
68
    }
69
70
    public function testArrayPassesThroughArrayForConvertToPHPValue()
71
    {
72
        self::assertInternalType(
73
            'array',
74
            $this->type->convertToPHPValue(serialize([]), $this->platform)
75
        );
76
    }
77
78
    public function testConversionFailure()
79
    {
80
        $this->expectException(ConversionException::class);
81
        $this->expectExceptionMessage("Could not convert database value to 'array' as an error was triggered by the unserialization: 'unserialize(): Error at offset 0 of 7 bytes'");
82
        $this->type->convertToPHPValue('abcdefg', $this->platform);
83
    }
84
85
    public function testNullConversion()
86
    {
87
        self::assertNull($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\Type::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...
88
    }
89
90
    /**
91
     * @group DBAL-73
92
     */
93
    public function testFalseConversion()
94
    {
95
        self::assertFalse($this->type->convertToPHPValue(serialize(false), $this->platform));
96
    }
97
}
98