Failed Conditions
Pull Request — master (#3217)
by Matthias
64:03
created

BlobTest::testInsertProcessesStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
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::STRING,
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
            // https://github.com/doctrine/dbal/issues/3290 for Oracle
59
            $this->markTestIncomplete('Platform does not support stream resources as parameters');
60
        }
61
62
        $longBlob = str_repeat('x', 4 * 8192); // send 4 chunks
63
        $this->_conn->insert('blob_table', [
64
            'id'        => 1,
65
            'clobfield' => 'ignored',
66
            'blobfield' => fopen('data://text/plain,' . $longBlob, 'r'),
67
        ], [
68
            ParameterType::INTEGER,
69
            ParameterType::STRING,
70
            ParameterType::LARGE_OBJECT,
71
        ]);
72
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::STRING,
85
            ParameterType::LARGE_OBJECT,
86
        ]);
87
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::STRING,
100
            ParameterType::LARGE_OBJECT,
101
        ]);
102
103
        $this->_conn->update('blob_table', [
104
            'blobfield' => 'test2',
105
        ], ['id' => 1], [
106
            ParameterType::LARGE_OBJECT,
107
            ParameterType::INTEGER,
108
        ]);
109
110
        $this->assertBlobContains('test2');
111
    }
112
113
    public function testUpdateProcessesStream()
114
    {
115
        if (in_array($this->_conn->getDatabasePlatform()->getName(), ['oracle', 'db2'], true)) {
116
            // https://github.com/doctrine/dbal/issues/3288 for DB2
117
            // https://github.com/doctrine/dbal/issues/3290 for Oracle
118
            $this->markTestIncomplete('Platform does not support stream resources as parameters');
119
        }
120
121
        $this->_conn->insert('blob_table', [
122
            'id'          => 1,
123
            'clobfield'   => 'ignored',
124
            'blobfield'   => 'test',
125
        ], [
126
            ParameterType::INTEGER,
127
            ParameterType::STRING,
128
            ParameterType::LARGE_OBJECT,
129
        ]);
130
131
        $this->_conn->update('blob_table', [
132
            'id'          => 1,
133
            'blobfield'   => fopen('data://text/plain,test2', 'r'),
134
        ], ['id' => 1], [
135
            ParameterType::INTEGER,
136
            ParameterType::LARGE_OBJECT,
137
        ]);
138
139
        $this->assertBlobContains('test2');
140
    }
141
142
    public function testBindParamProcessesStream()
143
    {
144
        if (in_array($this->_conn->getDatabasePlatform()->getName(), ['oracle', 'db2'], true)) {
145
            // https://github.com/doctrine/dbal/issues/3288 for DB2
146
            // https://github.com/doctrine/dbal/issues/3290 for Oracle
147
            $this->markTestIncomplete('Platform does not support stream resources as parameters');
148
        }
149
150
        $stmt = $this->_conn->prepare("INSERT INTO blob_table(id, clobfield, blobfield) VALUES (1, 'ignored', ?)");
151
152
        $stream = null;
153
        $stmt->bindParam(1, $stream, ParameterType::LARGE_OBJECT);
154
155
        // Bind param does late binding (bind by reference), so create the stream only now:
156
        $stream = fopen('data://text/plain,test', 'r');
1 ignored issue
show
Unused Code introduced by
The assignment to $stream is dead and can be removed.
Loading history...
157
158
        $stmt->execute();
159
160
        $this->assertBlobContains('test');
161
    }
162
163
    private function assertBlobContains($text)
164
    {
165
        $rows = $this->_conn->query('SELECT blobfield FROM blob_table')->fetchAll(FetchMode::COLUMN);
166
167
        self::assertCount(1, $rows);
168
169
        $blobValue = Type::getType('blob')->convertToPHPValue($rows[0], $this->_conn->getDatabasePlatform());
170
171
        self::assertInternalType('resource', $blobValue);
172
        self::assertEquals($text, stream_get_contents($blobValue));
173
    }
174
}
175