Failed Conditions
Pull Request — master (#3217)
by Matthias
61:26
created

BlobTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 84
dl 0
loc 152
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testInsert() 0 13 1
A setUp() 0 17 2
A testInsertProcessesStream() 0 20 2
A assertBlobContains() 0 10 1
A assertClobContains() 0 10 1
A testSelect() 0 14 1
A testUpdateProcessesStream() 0 29 2
A testUpdate() 0 23 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional;
4
5
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSrvDriver;
6
use Doctrine\DBAL\FetchMode;
7
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Schema\Table;
9
use Doctrine\DBAL\Types\Type;
10
use function fopen;
11
use function str_repeat;
12
use function stream_get_contents;
13
14
/**
15
 * @group DBAL-6
16
 */
17
class BlobTest extends \Doctrine\Tests\DbalFunctionalTestCase
18
{
19
    protected function setUp()
20
    {
21
        parent::setUp();
22
23
        if ($this->_conn->getDriver() instanceof PDOSQLSrvDriver) {
24
            $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');
25
        }
26
27
        /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
28
        $table = new Table('blob_table');
29
        $table->addColumn('id', 'integer');
30
        $table->addColumn('clobfield', 'text');
31
        $table->addColumn('blobfield', 'blob');
32
        $table->setPrimaryKey(['id']);
33
34
        $sm = $this->_conn->getSchemaManager();
35
        $sm->dropAndCreateTable($table);
36
    }
37
38
    public function testInsert()
39
    {
40
        $ret = $this->_conn->insert('blob_table', [
41
            'id'          => 1,
42
            'clobfield'   => 'test',
43
            'blobfield'   => 'test',
44
        ], [
45
            ParameterType::INTEGER,
46
            ParameterType::LARGE_OBJECT,
47
            ParameterType::LARGE_OBJECT,
48
        ]);
49
50
        self::assertEquals(1, $ret);
51
    }
52
53
    public function testInsertProcessesStream()
54
    {
55
        if ($this->_conn->getDatabasePlatform()->getName() === 'db2') {
56
            // https://github.com/doctrine/dbal/issues/3288
57
            $this->markTestSkipped('IBM DB2 does not support stream resources as parameters');
58
        }
59
60
        $longBlob = str_repeat('x', 4 * 8192); // send 4 chunks
61
        $this->_conn->insert('blob_table', [
62
            'id'        => 1,
63
            'clobfield' => fopen('data://text/plain,' . $longBlob, 'r'),
64
            'blobfield' => fopen('data://text/plain,' . $longBlob, 'r'),
65
        ], [
66
            ParameterType::INTEGER,
67
            ParameterType::LARGE_OBJECT,
68
            ParameterType::LARGE_OBJECT,
69
        ]);
70
71
        $this->assertClobContains($longBlob);
72
        $this->assertBlobContains($longBlob);
73
    }
74
75
    public function testSelect()
76
    {
77
        $this->_conn->insert('blob_table', [
78
            'id'          => 1,
79
            'clobfield'   => 'test',
80
            'blobfield'   => 'test',
81
        ], [
82
            ParameterType::INTEGER,
83
            ParameterType::LARGE_OBJECT,
84
            ParameterType::LARGE_OBJECT,
85
        ]);
86
87
        $this->assertClobContains('test');
88
        $this->assertBlobContains('test');
89
    }
90
91
    public function testUpdate()
92
    {
93
        $this->_conn->insert('blob_table', [
94
            'id' => 1,
95
            'clobfield' => 'test',
96
            'blobfield' => 'test',
97
        ], [
98
            ParameterType::INTEGER,
99
            ParameterType::LARGE_OBJECT,
100
            ParameterType::LARGE_OBJECT,
101
        ]);
102
103
        $this->_conn->update('blob_table', [
104
            'clobfield' => 'test2',
105
            'blobfield' => 'test2',
106
        ], ['id' => 1], [
107
            ParameterType::LARGE_OBJECT,
108
            ParameterType::LARGE_OBJECT,
109
            ParameterType::INTEGER,
110
        ]);
111
112
        $this->assertClobContains('test2');
113
        $this->assertBlobContains('test2');
114
    }
115
116
    public function testUpdateProcessesStream()
117
    {
118
        if ($this->_conn->getDatabasePlatform()->getName() === 'db2') {
119
            // https://github.com/doctrine/dbal/issues/3288
120
            $this->markTestSkipped('IBM DB2 does not support stream resources as parameters');
121
        }
122
123
        $this->_conn->insert('blob_table', [
124
            'id'          => 1,
125
            'clobfield'   => 'test',
126
            'blobfield'   => 'test',
127
        ], [
128
            ParameterType::INTEGER,
129
            ParameterType::LARGE_OBJECT,
130
            ParameterType::LARGE_OBJECT,
131
        ]);
132
133
        $this->_conn->update('blob_table', [
134
            'id'          => 1,
135
            'clobfield'   => fopen('data://text/plain,test2', 'r'),
136
            'blobfield'   => fopen('data://text/plain,test2', 'r'),
137
        ], ['id' => 1], [
138
            ParameterType::INTEGER,
139
            ParameterType::LARGE_OBJECT,
140
            ParameterType::LARGE_OBJECT,
141
        ]);
142
143
        $this->assertClobContains('test2');
144
        $this->assertBlobContains('test2');
145
    }
146
147
    private function assertBlobContains($text)
148
    {
149
        $rows = $this->_conn->query('SELECT blobfield FROM blob_table')->fetchAll(FetchMode::COLUMN);
150
151
        self::assertCount(1, $rows);
152
153
        $blobValue = Type::getType('blob')->convertToPHPValue($rows[0], $this->_conn->getDatabasePlatform());
154
155
        self::assertInternalType('resource', $blobValue);
156
        self::assertEquals($text, stream_get_contents($blobValue));
157
    }
158
159
    private function assertClobContains($text)
160
    {
161
        $rows = $this->_conn->query('SELECT clobfield FROM blob_table')->fetchAll(FetchMode::COLUMN);
162
163
        self::assertCount(1, $rows);
164
165
        $clobValue = Type::getType('text')->convertToPHPValue($rows[0], $this->_conn->getDatabasePlatform());
166
167
        self::assertInternalType('string', $clobValue);
168
        self::assertEquals($text, $clobValue);
169
    }
170
}
171