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)); |
|
|
|
|
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
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
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.