1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\ParameterType; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use const CASE_LOWER; |
8
|
|
|
use function array_change_key_case; |
9
|
|
|
use function stream_get_contents; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @group DBAL-6 |
13
|
|
|
*/ |
14
|
|
|
class BlobTest extends \Doctrine\Tests\DbalFunctionalTestCase |
15
|
|
|
{ |
16
|
|
|
protected function setUp() |
17
|
|
|
{ |
18
|
|
|
parent::setUp(); |
19
|
|
|
|
20
|
|
|
if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlsrv\Driver) { |
21
|
|
|
$this->markTestSkipped('This test does not work on pdo_sqlsrv driver due to a bug. See: http://social.msdn.microsoft.com/Forums/sqlserver/en-US/5a755bdd-41e9-45cb-9166-c9da4475bb94/how-to-set-null-for-varbinarymax-using-bindvalue-using-pdosqlsrv?forum=sqldriverforphp'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
try { |
25
|
|
|
/* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */ |
26
|
|
|
$table = new \Doctrine\DBAL\Schema\Table("blob_table"); |
27
|
|
|
$table->addColumn('id', 'integer'); |
28
|
|
|
$table->addColumn('clobfield', 'text'); |
29
|
|
|
$table->addColumn('blobfield', 'blob'); |
30
|
|
|
$table->addColumn('binaryfield', 'binary', array('length' => 50)); |
31
|
|
|
$table->setPrimaryKey(array('id')); |
32
|
|
|
|
33
|
|
|
$sm = $this->_conn->getSchemaManager(); |
34
|
|
|
$sm->createTable($table); |
35
|
|
|
} catch(\Exception $e) { |
|
|
|
|
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
$this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('blob_table')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testInsert() |
42
|
|
|
{ |
43
|
|
|
$ret = $this->_conn->insert('blob_table', [ |
44
|
|
|
'id' => 1, |
45
|
|
|
'clobfield' => 'test', |
46
|
|
|
'blobfield' => 'test', |
47
|
|
|
'binaryfield' => 'test', |
48
|
|
|
], [ |
49
|
|
|
ParameterType::INTEGER, |
50
|
|
|
ParameterType::STRING, |
51
|
|
|
ParameterType::LARGE_OBJECT, |
52
|
|
|
ParameterType::LARGE_OBJECT, |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
self::assertEquals(1, $ret); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testInsertProcessesStream() |
59
|
|
|
{ |
60
|
|
|
if (! $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver |
61
|
|
|
&& ! $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\PDOMySql\Driver) { |
62
|
|
|
$this->markTestSkipped('Passing streams into insert() is supported in Mysqli and PDOMySql only'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->_conn->insert('blob_table', [ |
66
|
|
|
'id' => 1, |
67
|
|
|
'clobfield' => 'ignored', |
68
|
|
|
'blobfield' => fopen('data://text/plain,test','r'), |
69
|
|
|
'binaryfield' => fopen('data://text/plain,test','r'), |
70
|
|
|
], [ |
71
|
|
|
ParameterType::INTEGER, |
72
|
|
|
ParameterType::STRING, |
73
|
|
|
ParameterType::LARGE_OBJECT, |
74
|
|
|
ParameterType::LARGE_OBJECT, |
75
|
|
|
]); |
76
|
|
|
|
77
|
|
|
$this->assertBlobContains('test'); |
78
|
|
|
$this->assertBinaryContains('test'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testSelect() |
82
|
|
|
{ |
83
|
|
|
$this->_conn->insert('blob_table', [ |
84
|
|
|
'id' => 1, |
85
|
|
|
'clobfield' => 'test', |
86
|
|
|
'blobfield' => 'test', |
87
|
|
|
'binaryfield' => 'test', |
88
|
|
|
], [ |
89
|
|
|
ParameterType::INTEGER, |
90
|
|
|
ParameterType::STRING, |
91
|
|
|
ParameterType::LARGE_OBJECT, |
92
|
|
|
ParameterType::LARGE_OBJECT, |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
$this->assertBlobContains('test'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testUpdate() |
99
|
|
|
{ |
100
|
|
|
$this->_conn->insert('blob_table', [ |
101
|
|
|
'id' => 1, |
102
|
|
|
'clobfield' => 'test', |
103
|
|
|
'blobfield' => 'test', |
104
|
|
|
'binaryfield' => 'test', |
105
|
|
|
], [ |
106
|
|
|
ParameterType::INTEGER, |
107
|
|
|
ParameterType::STRING, |
108
|
|
|
ParameterType::LARGE_OBJECT, |
109
|
|
|
ParameterType::LARGE_OBJECT, |
110
|
|
|
]); |
111
|
|
|
|
112
|
|
|
$this->_conn->update('blob_table', [ |
113
|
|
|
'blobfield' => 'test2', |
114
|
|
|
'binaryfield' => 'test2', |
115
|
|
|
], ['id' => 1], [ |
116
|
|
|
ParameterType::LARGE_OBJECT, |
117
|
|
|
ParameterType::LARGE_OBJECT, |
118
|
|
|
ParameterType::INTEGER, |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
$this->assertBlobContains('test2'); |
122
|
|
|
$this->assertBinaryContains('test2'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testUpdateProcessesStream() |
126
|
|
|
{ |
127
|
|
|
if (! $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\Mysqli\Driver |
128
|
|
|
&& ! $this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\PDOMySql\Driver) { |
129
|
|
|
$this->markTestSkipped('Passing streams into update() is supported in Mysqli and PDOMySql only'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$this->_conn->insert('blob_table', [ |
133
|
|
|
'id' => 1, |
134
|
|
|
'clobfield' => 'ignored', |
135
|
|
|
'blobfield' => 'test', |
136
|
|
|
'binaryfield' => 'test', |
137
|
|
|
], [ |
138
|
|
|
ParameterType::INTEGER, |
139
|
|
|
ParameterType::STRING, |
140
|
|
|
ParameterType::LARGE_OBJECT, |
141
|
|
|
ParameterType::LARGE_OBJECT, |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
$this->_conn->update('blob_table', [ |
145
|
|
|
'id' => 1, |
146
|
|
|
'blobfield' => fopen('data://text/plain,test2','r'), |
147
|
|
|
'binaryfield' => fopen('data://text/plain,test2','r'), |
148
|
|
|
], ['id' => 1], [ |
149
|
|
|
ParameterType::INTEGER, |
150
|
|
|
ParameterType::LARGE_OBJECT, |
151
|
|
|
ParameterType::LARGE_OBJECT, |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
$this->assertBlobContains('test2'); |
155
|
|
|
$this->assertBinaryContains('test2'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
private function assertBinaryContains($text) |
159
|
|
|
{ |
160
|
|
|
$rows = $this->_conn->fetchAll('SELECT * FROM blob_table'); |
161
|
|
|
|
162
|
|
|
self::assertCount(1, $rows); |
163
|
|
|
$row = array_change_key_case($rows[0], CASE_LOWER); |
164
|
|
|
|
165
|
|
|
$blobValue = Type::getType('binary')->convertToPHPValue($row['binaryfield'], $this->_conn->getDatabasePlatform()); |
166
|
|
|
|
167
|
|
|
self::assertInternalType('resource', $blobValue); |
168
|
|
|
self::assertEquals($text, stream_get_contents($blobValue)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private function assertBlobContains($text) |
172
|
|
|
{ |
173
|
|
|
$rows = $this->_conn->fetchAll('SELECT * FROM blob_table'); |
174
|
|
|
|
175
|
|
|
self::assertCount(1, $rows); |
176
|
|
|
$row = array_change_key_case($rows[0], CASE_LOWER); |
177
|
|
|
|
178
|
|
|
$blobValue = Type::getType('blob')->convertToPHPValue($row['blobfield'], $this->_conn->getDatabasePlatform()); |
179
|
|
|
|
180
|
|
|
self::assertInternalType('resource', $blobValue); |
181
|
|
|
self::assertEquals($text, stream_get_contents($blobValue)); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|