Completed
Pull Request — 2.7 (#3217)
by Matthias
63:38
created

BlobTest::assertClobContains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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