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