Passed
Pull Request — 2.7 (#3217)
by Matthias
10:56
created

testInsertCanHandleStreamLongerThanChunkSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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