1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
<?php
namespace Doctrine\DBAL\Tests\Functional;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\Type;
use function fopen;
use function str_repeat;
use function stream_get_contents;
class BlobTest extends FunctionalTestCase
{
protected function setUp(): void
{
if (TestUtil::isDriverOneOf('pdo_oci')) {
// inserting BLOBs as streams on Oracle requires Oracle-specific SQL syntax which is currently not supported
// see http://php.net/manual/en/pdo.lobs.php#example-1035
self::markTestSkipped("DBAL doesn't support storing LOBs represented as streams using PDO_OCI");
}
$table = new Table('blob_table');
$table->addColumn('id', 'integer');
$table->addColumn('clobcolumn', 'text', ['notnull' => false]);
$table->addColumn('blobcolumn', 'blob', ['notnull' => false]);
$table->setPrimaryKey(['id']);
$this->dropAndCreateTable($table);
}
public function testInsert(): void
{
$ret = $this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'test',
'blobcolumn' => 'test',
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
self::assertEquals(1, $ret);
}
public function testInsertNull(): void
{
$ret = $this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => null,
'blobcolumn' => null,
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
self::assertEquals(1, $ret);
[$clobValue, $blobValue] = $this->fetchRow();
self::assertNull($clobValue);
self::assertNull($blobValue);
}
public function testInsertProcessesStream(): void
{
// https://github.com/doctrine/dbal/issues/3290
if (TestUtil::isDriverOneOf('oci8')) {
self::markTestIncomplete('The oci8 driver does not support stream resources as parameters');
}
$longBlob = str_repeat('x', 4 * 8192); // send 4 chunks
$this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'ignored',
'blobcolumn' => fopen('data://text/plain,' . $longBlob, 'r'),
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
$this->assertBlobContains($longBlob);
}
public function testSelect(): void
{
$this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'test',
'blobcolumn' => 'test',
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
$this->assertBlobContains('test');
}
public function testUpdate(): void
{
$this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'test',
'blobcolumn' => 'test',
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
$this->connection->update('blob_table', ['blobcolumn' => 'test2'], ['id' => 1], [
ParameterType::LARGE_OBJECT,
ParameterType::INTEGER,
]);
$this->assertBlobContains('test2');
}
public function testUpdateProcessesStream(): void
{
// https://github.com/doctrine/dbal/issues/3290
if (TestUtil::isDriverOneOf('oci8')) {
self::markTestIncomplete('The oci8 driver does not support stream resources as parameters');
}
$this->connection->insert('blob_table', [
'id' => 1,
'clobcolumn' => 'ignored',
'blobcolumn' => 'test',
], [
ParameterType::INTEGER,
ParameterType::STRING,
ParameterType::LARGE_OBJECT,
]);
$this->connection->update('blob_table', [
'id' => 1,
'blobcolumn' => fopen('data://text/plain,test2', 'r'),
], ['id' => 1], [
ParameterType::INTEGER,
ParameterType::LARGE_OBJECT,
]);
$this->assertBlobContains('test2');
}
public function testBindParamProcessesStream(): void
{
if (TestUtil::isDriverOneOf('oci8')) {
self::markTestIncomplete('The oci8 driver does not support stream resources as parameters');
}
$stmt = $this->connection->prepare(
"INSERT INTO blob_table(id, clobcolumn, blobcolumn) VALUES (1, 'ignored', ?)",
);
$stmt->bindParam(1, $stream, ParameterType::LARGE_OBJECT);
// Bind param does late binding (bind by reference), so create the stream only now:
$stream = fopen('data://text/plain,test', 'r');
$stmt->execute();
$this->assertBlobContains('test');
}
private function assertBlobContains(string $text): void
{
[, $blobValue] = $this->fetchRow();
$blobValue = Type::getType('blob')->convertToPHPValue($blobValue, $this->connection->getDatabasePlatform());
self::assertIsResource($blobValue);
self::assertEquals($text, stream_get_contents($blobValue));
}
/** @return list<mixed> */
private function fetchRow(): array
{
$rows = $this->connection->fetchAllNumeric('SELECT clobcolumn, blobcolumn FROM blob_table');
self::assertCount(1, $rows);
return $rows[0];
}
}
|