Completed
Push — develop ( 0c4aa7...b62acb )
by Sergei
55s queued 14s
created

BinaryTest::testBinaryResourceConvertsToPHPValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Types;
6
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Types\BinaryType;
10
use Doctrine\DBAL\Types\ConversionException;
11
use Doctrine\DBAL\Types\Type;
12
use Doctrine\DBAL\Types\Types;
13
use Doctrine\Tests\DbalTestCase;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use function array_map;
16
use function base64_encode;
17
use function fopen;
18
use function implode;
19
use function range;
20
21
class BinaryTest extends DbalTestCase
22
{
23
    /** @var AbstractPlatform|MockObject */
24
    protected $platform;
25
26
    /** @var BinaryType */
27
    protected $type;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function setUp() : void
33
    {
34
        $this->platform = $this->createMock(AbstractPlatform::class);
35
        $this->type     = Type::getType('binary');
36
    }
37
38
    public function testReturnsBindingType() : void
39
    {
40
        self::assertSame(ParameterType::BINARY, $this->type->getBindingType());
41
    }
42
43
    public function testReturnsName() : void
44
    {
45
        self::assertSame(Types::BINARY, $this->type->getName());
46
    }
47
48
    /**
49
     * @param mixed[][] $definition
50
     *
51
     * @dataProvider definitionProvider()
52
     */
53
    public function testReturnsSQLDeclaration(array $definition, string $expectedDeclaration) : void
54
    {
55
        $platform = $this->getMockForAbstractClass(AbstractPlatform::class);
56
        self::assertSame($expectedDeclaration, $this->type->getSQLDeclaration($definition, $platform));
57
    }
58
59
    /**
60
     * @return mixed[][]
61
     */
62
    public static function definitionProvider() : iterable
63
    {
64
        return [
65
            'fixed-unspecified-length' => [
66
                ['fixed' => true],
67
                'BINARY',
68
            ],
69
            'fixed-specified-length' => [
70
                [
71
                    'fixed' => true,
72
                    'length' => 20,
73
                ],
74
                'BINARY(20)',
75
            ],
76
            'variable-length' => [
77
                ['length' => 20],
78
                'VARBINARY(20)',
79
            ],
80
        ];
81
    }
82
83
    public function testBinaryNullConvertsToPHPValue() : void
84
    {
85
        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\Bina...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...
86
    }
87
88
    public function testBinaryStringConvertsToPHPValue() : void
89
    {
90
        $databaseValue = $this->getBinaryString();
91
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);
92
93
        self::assertSame($databaseValue, $phpValue);
94
    }
95
96
    public function testBinaryResourceConvertsToPHPValue() : void
97
    {
98
        $databaseValue = fopen('data://text/plain;base64,' . base64_encode('binary string'), 'r');
99
        $phpValue      = $this->type->convertToPHPValue($databaseValue, $this->platform);
100
101
        self::assertSame('binary string', $phpValue);
102
    }
103
104
    /**
105
     * Creates a binary string containing all possible byte values.
106
     */
107
    private function getBinaryString() : string
108
    {
109
        return implode(array_map('chr', range(0, 255)));
110
    }
111
112
    /**
113
     * @param mixed $value
114
     *
115
     * @dataProvider getInvalidDatabaseValues
116
     */
117
    public function testThrowsConversionExceptionOnInvalidDatabaseValue($value) : void
118
    {
119
        $this->expectException(ConversionException::class);
120
121
        $this->type->convertToPHPValue($value, $this->platform);
122
    }
123
124
    /**
125
     * @return mixed[][]
126
     */
127
    public static function getInvalidDatabaseValues() : iterable
128
    {
129
        return [
130
            [false],
131
            [true],
132
            [0],
133
            [1],
134
            [-1],
135
            [0.0],
136
            [1.1],
137
            [-1.1],
138
        ];
139
    }
140
}
141