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

BlobTest::testInsertProcessesStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.9
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::STRING,
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'   => 'ignored',
65
            'blobfield'   => fopen('data://text/plain,test', 'r'),
66
            'binaryfield' => fopen('data://text/plain,test', 'r'),
67
        ], [
68
            ParameterType::INTEGER,
69
            ParameterType::STRING,
70
            ParameterType::LARGE_OBJECT,
71
            ParameterType::LARGE_OBJECT,
72
        ]);
73
74
        $this->assertBlobContains('test');
75
        $this->assertBinaryContains('test');
76
    }
77
78
    public function testInsertCanHandleStreamLongerThanChunkSize()
79
    {
80
        $longBlob = str_repeat('x', 40000);
81
82
        $this->_conn->insert('blob_table', [
83
            'id'          => 1,
84
            'clobfield'   => 'ignored',
85
            'blobfield'   => fopen('data://text/plain,' . $longBlob, 'r'),
86
            'binaryfield' => 'ignored',
87
        ], [
88
            ParameterType::INTEGER,
89
            ParameterType::STRING,
90
            ParameterType::LARGE_OBJECT,
91
            ParameterType::LARGE_OBJECT,
92
        ]);
93
94
        $this->assertBlobContains($longBlob);
95
    }
96
97
    public function testSelect()
98
    {
99
        $this->_conn->insert('blob_table', [
100
            'id'          => 1,
101
            'clobfield'   => 'test',
102
            'blobfield'   => 'test',
103
            'binaryfield' => 'test',
104
        ], [
105
            ParameterType::INTEGER,
106
            ParameterType::STRING,
107
            ParameterType::LARGE_OBJECT,
108
            ParameterType::LARGE_OBJECT,
109
        ]);
110
111
        $this->assertBlobContains('test');
112
    }
113
114
    public function testUpdate()
115
    {
116
        $this->_conn->insert('blob_table', [
117
            'id' => 1,
118
            'clobfield' => 'test',
119
            'blobfield' => 'test',
120
            'binaryfield' => 'test',
121
        ], [
122
            ParameterType::INTEGER,
123
            ParameterType::STRING,
124
            ParameterType::LARGE_OBJECT,
125
            ParameterType::LARGE_OBJECT,
126
        ]);
127
128
        $this->_conn->update('blob_table', [
129
            'blobfield' => 'test2',
130
            'binaryfield' => 'test2',
131
        ], ['id' => 1], [
132
            ParameterType::LARGE_OBJECT,
133
            ParameterType::LARGE_OBJECT,
134
            ParameterType::INTEGER,
135
        ]);
136
137
        $this->assertBlobContains('test2');
138
        $this->assertBinaryContains('test2');
139
    }
140
141
    public function testUpdateProcessesStream()
142
    {
143
        $this->_conn->insert('blob_table', [
144
            'id'          => 1,
145
            'clobfield'   => 'ignored',
146
            'blobfield'   => 'test',
147
            'binaryfield' => 'test',
148
        ], [
149
            ParameterType::INTEGER,
150
            ParameterType::STRING,
151
            ParameterType::LARGE_OBJECT,
152
            ParameterType::LARGE_OBJECT,
153
        ]);
154
155
        $this->_conn->update('blob_table', [
156
            'id'          => 1,
157
            'blobfield'   => fopen('data://text/plain,test2', 'r'),
158
            'binaryfield' => fopen('data://text/plain,test2', 'r'),
159
        ], ['id' => 1], [
160
            ParameterType::INTEGER,
161
            ParameterType::LARGE_OBJECT,
162
            ParameterType::LARGE_OBJECT,
163
        ]);
164
165
        $this->assertBlobContains('test2');
166
        $this->assertBinaryContains('test2');
167
    }
168
169
    private function assertBinaryContains($text)
170
    {
171
        $rows = $this->_conn->fetchAll('SELECT * FROM blob_table');
172
173
        self::assertCount(1, $rows);
174
        $row = array_change_key_case($rows[0], CASE_LOWER);
175
176
        $blobValue = Type::getType('binary')->convertToPHPValue($row['binaryfield'], $this->_conn->getDatabasePlatform());
177
178
        self::assertInternalType('resource', $blobValue);
179
        self::assertEquals($text, stream_get_contents($blobValue));
180
    }
181
182
    private function assertBlobContains($text)
183
    {
184
        $rows = $this->_conn->fetchAll('SELECT * FROM blob_table');
185
186
        self::assertCount(1, $rows);
187
        $row = array_change_key_case($rows[0], CASE_LOWER);
188
189
        $blobValue = Type::getType('blob')->convertToPHPValue($row['blobfield'], $this->_conn->getDatabasePlatform());
190
191
        self::assertInternalType('resource', $blobValue);
192
        self::assertEquals($text, stream_get_contents($blobValue));
193
    }
194
}
195