Failed Conditions
Pull Request — master (#3217)
by Matthias
63:23 queued 12s
created

BlobTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 80
dl 0
loc 142
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testInsertProcessesStream() 0 15 1
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 24 1
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
        $longBlob = str_repeat('x', 4 * 8192); // send 4 chunks
56
        $this->_conn->insert('blob_table', [
57
            'id'        => 1,
58
            'clobfield' => fopen('data://text/plain,' . $longBlob, 'r'),
59
            'blobfield' => fopen('data://text/plain,' . $longBlob, 'r'),
60
        ], [
61
            ParameterType::INTEGER,
62
            ParameterType::LARGE_OBJECT,
63
            ParameterType::LARGE_OBJECT,
64
        ]);
65
66
        $this->assertClobContains($longBlob);
67
        $this->assertBlobContains($longBlob);
68
    }
69
70
    public function testSelect()
71
    {
72
        $this->_conn->insert('blob_table', [
73
            'id'          => 1,
74
            'clobfield'   => 'test',
75
            'blobfield'   => 'test',
76
        ], [
77
            ParameterType::INTEGER,
78
            ParameterType::LARGE_OBJECT,
79
            ParameterType::LARGE_OBJECT,
80
        ]);
81
82
        $this->assertClobContains('test');
83
        $this->assertBlobContains('test');
84
    }
85
86
    public function testUpdate()
87
    {
88
        $this->_conn->insert('blob_table', [
89
            'id' => 1,
90
            'clobfield' => 'test',
91
            'blobfield' => 'test',
92
        ], [
93
            ParameterType::INTEGER,
94
            ParameterType::LARGE_OBJECT,
95
            ParameterType::LARGE_OBJECT,
96
        ]);
97
98
        $this->_conn->update('blob_table', [
99
            'clobfield' => 'test2',
100
            'blobfield' => 'test2',
101
        ], ['id' => 1], [
102
            ParameterType::LARGE_OBJECT,
103
            ParameterType::LARGE_OBJECT,
104
            ParameterType::INTEGER,
105
        ]);
106
107
        $this->assertClobContains('test2');
108
        $this->assertBlobContains('test2');
109
    }
110
111
    public function testUpdateProcessesStream()
112
    {
113
        $this->_conn->insert('blob_table', [
114
            'id'          => 1,
115
            'clobfield'   => 'test',
116
            'blobfield'   => 'test',
117
        ], [
118
            ParameterType::INTEGER,
119
            ParameterType::LARGE_OBJECT,
120
            ParameterType::LARGE_OBJECT,
121
        ]);
122
123
        $this->_conn->update('blob_table', [
124
            'id'          => 1,
125
            'clobfield'   => fopen('data://text/plain,test2', 'r'),
126
            'blobfield'   => fopen('data://text/plain,test2', 'r'),
127
        ], ['id' => 1], [
128
            ParameterType::INTEGER,
129
            ParameterType::LARGE_OBJECT,
130
            ParameterType::LARGE_OBJECT,
131
        ]);
132
133
        $this->assertClobContains('test2');
134
        $this->assertBlobContains('test2');
135
    }
136
137
    private function assertBlobContains($text)
138
    {
139
        $rows = $this->_conn->query('SELECT blobfield FROM blob_table')->fetchAll(FetchMode::COLUMN);
140
141
        self::assertCount(1, $rows);
142
143
        $blobValue = Type::getType('blob')->convertToPHPValue($rows[0], $this->_conn->getDatabasePlatform());
144
145
        self::assertInternalType('resource', $blobValue);
146
        self::assertEquals($text, stream_get_contents($blobValue));
147
    }
148
149
    private function assertClobContains($text)
150
    {
151
        $rows = $this->_conn->query('SELECT clobfield FROM blob_table')->fetchAll(FetchMode::COLUMN);
152
153
        self::assertCount(1, $rows);
154
155
        $clobValue = Type::getType('text')->convertToPHPValue($rows[0], $this->_conn->getDatabasePlatform());
156
157
        self::assertInternalType('string', $clobValue);
158
        self::assertEquals($text, $clobValue);
159
    }
160
}
161