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

ArrayTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 22
dl 0
loc 73
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testArrayConvertsToPHPValue() 0 3 1
A testArrayConvertsToDatabaseValue() 0 3 1
A testNullConversion() 0 3 1
A testArrayPassesThroughArrayForConvertToPHPValue() 0 4 1
A testFalseConversion() 0 3 1
A testArrayConvertsToPHPFailsWithArrayParameterValue() 0 6 1
A testConversionFailure() 0 5 1
A testArrayNormalizesToPHPValue() 0 12 1
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() : void
21
    {
22
        $this->platform = new MockPlatform();
23
        $this->type     = Type::getType('array');
24
    }
25
26
    public function testArrayConvertsToDatabaseValue()
27
    {
28
        self::assertIsString($this->type->convertToDatabaseValue([], $this->platform));
29
    }
30
31
    public function testArrayConvertsToPHPValue()
32
    {
33
        self::assertIsArray($this->type->convertToPHPValue(serialize([]), $this->platform));
34
    }
35
36
    public function testArrayConvertsToPHPFailsWithArrayParameterValue()
37
    {
38
        $this->expectException(ConversionException::class);
39
40
        self::assertIsArray(
41
            $this->type->convertToPHPValue([], $this->platform)
42
        );
43
    }
44
45
    public function testArrayNormalizesToPHPValue()
46
    {
47
        self::assertIsArray(
48
            $this->type->normalizeToPHPValue([], $this->platform)
49
        );
50
51
        self::assertNull(
52
            $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...
53
        );
54
55
        self::assertIsArray(
56
            $this->type->convertToPHPValue(serialize([]), $this->platform)
57
        );
58
    }
59
60
    public function testArrayPassesThroughArrayForConvertToPHPValue()
61
    {
62
        self::assertIsArray(
63
            $this->type->convertToPHPValue(serialize([]), $this->platform)
64
        );
65
    }
66
67
    public function testConversionFailure()
68
    {
69
        $this->expectException(ConversionException::class);
70
        $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'");
71
        $this->type->convertToPHPValue('abcdefg', $this->platform);
72
    }
73
74
    public function testNullConversion()
75
    {
76
        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...
77
    }
78
79
    /**
80
     * @group DBAL-73
81
     */
82
    public function testFalseConversion()
83
    {
84
        self::assertFalse($this->type->convertToPHPValue(serialize(false), $this->platform));
85
    }
86
}
87