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

BlobTest::testUpdateProcessesStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
rs 9.7333
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
            $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' => 'ignored',
65
            'blobfield' => fopen('data://text/plain,' . $longBlob, 'r'),
66
        ], [
67
            ParameterType::INTEGER,
68
            ParameterType::STRING,
69
            ParameterType::LARGE_OBJECT,
70
        ]);
71
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::STRING,
84
            ParameterType::LARGE_OBJECT,
85
        ]);
86
87
        $this->assertBlobContains('test');
88
    }
89
90
    public function testUpdate()
91
    {
92
        $this->_conn->insert('blob_table', [
93
            'id' => 1,
94
            'clobfield' => 'test',
95
            'blobfield' => 'test',
96
        ], [
97
            ParameterType::INTEGER,
98
            ParameterType::STRING,
99
            ParameterType::LARGE_OBJECT,
100
        ]);
101
102
        $this->_conn->update('blob_table', [
103
            'blobfield' => 'test2',
104
        ], ['id' => 1], [
105
            ParameterType::LARGE_OBJECT,
106
            ParameterType::INTEGER,
107
        ]);
108
109
        $this->assertBlobContains('test2');
110
    }
111
112
    public function testUpdateProcessesStream()
113
    {
114
        if (in_array($this->_conn->getDatabasePlatform()->getName(), ['oracle', 'db2'], true)) {
115
            // https://github.com/doctrine/dbal/issues/3288 for DB2
116
            $this->markTestSkipped('Platform does not support stream resources as parameters');
117
        }
118
119
        $this->_conn->insert('blob_table', [
120
            'id'          => 1,
121
            'clobfield'   => 'ignored',
122
            'blobfield'   => 'test',
123
        ], [
124
            ParameterType::INTEGER,
125
            ParameterType::STRING,
126
            ParameterType::LARGE_OBJECT,
127
        ]);
128
129
        $this->_conn->update('blob_table', [
130
            'id'          => 1,
131
            'blobfield'   => fopen('data://text/plain,test2', 'r'),
132
        ], ['id' => 1], [
133
            ParameterType::INTEGER,
134
            ParameterType::LARGE_OBJECT,
135
        ]);
136
137
        $this->assertBlobContains('test2');
138
    }
139
140
    private function assertBlobContains($text)
141
    {
142
        $rows = $this->_conn->query('SELECT blobfield FROM blob_table')->fetchAll(FetchMode::COLUMN);
143
144
        self::assertCount(1, $rows);
145
146
        $blobValue = Type::getType('blob')->convertToPHPValue($rows[0], $this->_conn->getDatabasePlatform());
147
148
        self::assertInternalType('resource', $blobValue);
149
        self::assertEquals($text, stream_get_contents($blobValue));
150
    }
151
}
152