|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Types\Type; |
|
6
|
|
|
use PDO; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @group DBAL-6 |
|
10
|
|
|
*/ |
|
11
|
|
|
class BlobTest extends \Doctrine\Tests\DbalFunctionalTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
protected function setUp() |
|
14
|
|
|
{ |
|
15
|
|
|
parent::setUp(); |
|
16
|
|
|
|
|
17
|
|
|
if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlsrv\Driver) { |
|
18
|
|
|
$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'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
try { |
|
22
|
|
|
/* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */ |
|
23
|
|
|
$table = new \Doctrine\DBAL\Schema\Table("blob_table"); |
|
24
|
|
|
$table->addColumn('id', 'integer'); |
|
25
|
|
|
$table->addColumn('clobfield', 'text'); |
|
26
|
|
|
$table->addColumn('blobfield', 'blob'); |
|
27
|
|
|
$table->addColumn('binaryfield', 'binary', array('length' => 50)); |
|
28
|
|
|
$table->setPrimaryKey(array('id')); |
|
29
|
|
|
|
|
30
|
|
|
$sm = $this->_conn->getSchemaManager(); |
|
31
|
|
|
$sm->createTable($table); |
|
32
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
$this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('blob_table')); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
public function testInsert() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$ret = $this->_conn->insert( |
|
40
|
|
|
'blob_table', |
|
41
|
|
|
array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'), |
|
42
|
|
|
array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB) |
|
43
|
|
|
); |
|
44
|
|
|
self::assertEquals(1, $ret); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
View Code Duplication |
public function testSelect() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$this->_conn->insert( |
|
50
|
|
|
'blob_table', |
|
51
|
|
|
array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'), |
|
52
|
|
|
array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertBlobContains('test'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function testUpdate() |
|
59
|
|
|
{ |
|
60
|
|
|
$this->_conn->insert( |
|
61
|
|
|
'blob_table', |
|
62
|
|
|
array('id' => 1, 'clobfield' => 'test', 'blobfield' => 'test', 'binaryfield' => 'test'), |
|
63
|
|
|
array(\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_LOB, \PDO::PARAM_LOB) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->_conn->update( |
|
67
|
|
|
'blob_table', |
|
68
|
|
|
array('blobfield' => 'test2', 'binaryfield' => 'test2'), |
|
69
|
|
|
array('id' => 1), |
|
70
|
|
|
array(\PDO::PARAM_LOB, \PDO::PARAM_LOB, \PDO::PARAM_INT) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertBlobContains('test2'); |
|
74
|
|
|
$this->assertBinaryContains('test2'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
private function assertBinaryContains($text) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$rows = $this->_conn->fetchAll('SELECT * FROM blob_table'); |
|
80
|
|
|
|
|
81
|
|
|
self::assertEquals(1, count($rows)); |
|
82
|
|
|
$row = array_change_key_case($rows[0], CASE_LOWER); |
|
83
|
|
|
|
|
84
|
|
|
$blobValue = Type::getType('binary')->convertToPHPValue($row['binaryfield'], $this->_conn->getDatabasePlatform()); |
|
85
|
|
|
|
|
86
|
|
|
self::assertInternalType('resource', $blobValue); |
|
87
|
|
|
self::assertEquals($text, stream_get_contents($blobValue)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
View Code Duplication |
private function assertBlobContains($text) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$rows = $this->_conn->fetchAll('SELECT * FROM blob_table'); |
|
93
|
|
|
|
|
94
|
|
|
self::assertEquals(1, count($rows)); |
|
95
|
|
|
$row = array_change_key_case($rows[0], CASE_LOWER); |
|
96
|
|
|
|
|
97
|
|
|
$blobValue = Type::getType('blob')->convertToPHPValue($row['blobfield'], $this->_conn->getDatabasePlatform()); |
|
98
|
|
|
|
|
99
|
|
|
self::assertInternalType('resource', $blobValue); |
|
100
|
|
|
self::assertEquals($text, stream_get_contents($blobValue)); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|