Completed
Pull Request — 2.7 (#3217)
by Matthias
64:32
created

BlobTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 98
dl 0
loc 166
rs 10
c 0
b 0
f 0

8 Methods

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