Passed
Pull Request — 2.7 (#3217)
by Matthias
17:44
created

BlobTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 105
dl 0
loc 177
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testInsert() 0 15 1
A setUp() 0 23 3
A testInsertProcessesStream() 0 16 1
A assertBlobContains() 0 11 1
A testInsertCanHandleStreamLongerThanChunkSize() 0 17 1
A testSelect() 0 15 1
A assertBinaryContains() 0 11 1
A testUpdateProcessesStream() 0 26 1
A testUpdate() 0 25 1
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
use function fopen;
11
12
/**
13
 * @group DBAL-6
14
 */
15
class BlobTest extends \Doctrine\Tests\DbalFunctionalTestCase
16
{
17
    protected function setUp()
18
    {
19
        parent::setUp();
20
21
        if ($this->_conn->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlsrv\Driver) {
22
            $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');
23
        }
24
25
        try {
26
            /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
27
            $table = new \Doctrine\DBAL\Schema\Table("blob_table");
28
            $table->addColumn('id', 'integer');
29
            $table->addColumn('clobfield', 'text');
30
            $table->addColumn('blobfield', 'blob');
31
            $table->addColumn('binaryfield', 'binary', array('length' => 50));
32
            $table->setPrimaryKey(array('id'));
33
34
            $sm = $this->_conn->getSchemaManager();
35
            $sm->createTable($table);
36
        } catch(\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
37
38
        }
39
        $this->_conn->exec($this->_conn->getDatabasePlatform()->getTruncateTableSQL('blob_table'));
40
    }
41
42
    public function testInsert()
43
    {
44
        $ret = $this->_conn->insert('blob_table', [
45
            'id'          => 1,
46
            'clobfield'   => 'test',
47
            'blobfield'   => 'test',
48
            'binaryfield' => 'test',
49
        ], [
50
            ParameterType::INTEGER,
51
            ParameterType::STRING,
52
            ParameterType::LARGE_OBJECT,
53
            ParameterType::LARGE_OBJECT,
54
        ]);
55
56
        self::assertEquals(1, $ret);
57
    }
58
59
    public function testInsertProcessesStream()
60
    {
61
        $this->_conn->insert('blob_table', [
62
            'id'          => 1,
63
            'clobfield'   => 'ignored',
64
            'blobfield'   => fopen('data://text/plain,test', 'r'),
65
            'binaryfield' => fopen('data://text/plain,test', 'r'),
66
        ], [
67
            ParameterType::INTEGER,
68
            ParameterType::STRING,
69
            ParameterType::LARGE_OBJECT,
70
            ParameterType::LARGE_OBJECT,
71
        ]);
72
73
        $this->assertBlobContains('test');
74
        $this->assertBinaryContains('test');
75
    }
76
77
    public function testInsertCanHandleStreamLongerThanChunkSize()
78
    {
79
        $longBlob = str_repeat('x', 40000);
80
81
        $this->_conn->insert('blob_table', [
82
            'id'          => 1,
83
            'clobfield'   => 'ignored',
84
            'blobfield'   => fopen('data://text/plain,' . $longBlob, 'r'),
85
            'binaryfield' => 'ignored',
86
        ], [
87
            ParameterType::INTEGER,
88
            ParameterType::STRING,
89
            ParameterType::LARGE_OBJECT,
90
            ParameterType::LARGE_OBJECT,
91
        ]);
92
93
        $this->assertBlobContains($longBlob);
94
    }
95
96
    public function testSelect()
97
    {
98
        $this->_conn->insert('blob_table', [
99
            'id'          => 1,
100
            'clobfield'   => 'test',
101
            'blobfield'   => 'test',
102
            'binaryfield' => 'test',
103
        ], [
104
            ParameterType::INTEGER,
105
            ParameterType::STRING,
106
            ParameterType::LARGE_OBJECT,
107
            ParameterType::LARGE_OBJECT,
108
        ]);
109
110
        $this->assertBlobContains('test');
111
    }
112
113
    public function testUpdate()
114
    {
115
        $this->_conn->insert('blob_table', [
116
            'id' => 1,
117
            'clobfield' => 'test',
118
            'blobfield' => 'test',
119
            'binaryfield' => 'test',
120
        ], [
121
            ParameterType::INTEGER,
122
            ParameterType::STRING,
123
            ParameterType::LARGE_OBJECT,
124
            ParameterType::LARGE_OBJECT,
125
        ]);
126
127
        $this->_conn->update('blob_table', [
128
            'blobfield' => 'test2',
129
            'binaryfield' => 'test2',
130
        ], ['id' => 1], [
131
            ParameterType::LARGE_OBJECT,
132
            ParameterType::LARGE_OBJECT,
133
            ParameterType::INTEGER,
134
        ]);
135
136
        $this->assertBlobContains('test2');
137
        $this->assertBinaryContains('test2');
138
    }
139
140
    public function testUpdateProcessesStream()
141
    {
142
        $this->_conn->insert('blob_table', [
143
            'id'          => 1,
144
            'clobfield'   => 'ignored',
145
            'blobfield'   => 'test',
146
            'binaryfield' => 'test',
147
        ], [
148
            ParameterType::INTEGER,
149
            ParameterType::STRING,
150
            ParameterType::LARGE_OBJECT,
151
            ParameterType::LARGE_OBJECT,
152
        ]);
153
154
        $this->_conn->update('blob_table', [
155
            'id'          => 1,
156
            'blobfield'   => fopen('data://text/plain,test2', 'r'),
157
            'binaryfield' => fopen('data://text/plain,test2', 'r'),
158
        ], ['id' => 1], [
159
            ParameterType::INTEGER,
160
            ParameterType::LARGE_OBJECT,
161
            ParameterType::LARGE_OBJECT,
162
        ]);
163
164
        $this->assertBlobContains('test2');
165
        $this->assertBinaryContains('test2');
166
    }
167
168
    private function assertBinaryContains($text)
169
    {
170
        $rows = $this->_conn->fetchAll('SELECT * FROM blob_table');
171
172
        self::assertCount(1, $rows);
173
        $row = array_change_key_case($rows[0], CASE_LOWER);
174
175
        $blobValue = Type::getType('binary')->convertToPHPValue($row['binaryfield'], $this->_conn->getDatabasePlatform());
176
177
        self::assertInternalType('resource', $blobValue);
178
        self::assertEquals($text, stream_get_contents($blobValue));
179
    }
180
181
    private function assertBlobContains($text)
182
    {
183
        $rows = $this->_conn->fetchAll('SELECT * FROM blob_table');
184
185
        self::assertCount(1, $rows);
186
        $row = array_change_key_case($rows[0], CASE_LOWER);
187
188
        $blobValue = Type::getType('blob')->convertToPHPValue($row['blobfield'], $this->_conn->getDatabasePlatform());
189
190
        self::assertInternalType('resource', $blobValue);
191
        self::assertEquals($text, stream_get_contents($blobValue));
192
    }
193
}
194