Failed Conditions
Push — travis-db2 ( 1974ca...e41f38 )
by Sergei
08:20
created

BinaryTest::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional\Types;
6
7
use Doctrine\DBAL\Driver\IBMDB2\DB2Driver;
8
use Doctrine\DBAL\Driver\OCI8\Driver as OCI8Driver;
9
use Doctrine\DBAL\ParameterType;
10
use Doctrine\DBAL\Schema\Table;
11
use function is_resource;
12
use function random_bytes;
13
use function str_replace;
14
use function stream_get_contents;
15
16
/**
17
 * @group DBAL-6
18
 */
19
class BinaryTest extends \Doctrine\Tests\DbalFunctionalTestCase
20
{
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
25
        /** @see https://github.com/doctrine/dbal/issues/2787 */
26
        if ($this->_conn->getDriver() instanceof OCI8Driver) {
27
            $this->markTestSkipped('Filtering by binary fields is currently not supported on Oracle');
28
        }
29
30
        /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
31
        $table = new Table('binary_table');
32
        $table->addColumn('id', 'binary', [
33
            'length' => 16,
34
            'fixed' => true,
35
        ]);
36
        $table->addColumn('val', 'binary', ['length' => 64]);
37
        $table->setPrimaryKey(['id']);
38
39
        $sm = $this->_conn->getSchemaManager();
40
        $sm->dropAndCreateTable($table);
41
    }
42
43
    public function testInsertAndSelect()
44
    {
45
        $id1 = random_bytes(16);
46
        $id2 = random_bytes(16);
47
48
        $value1 = random_bytes(64);
49
        $value2 = random_bytes(64);
50
51
        /** @see https://bugs.php.net/bug.php?id=76322 */
52
        if ($this->_conn->getDriver() instanceof DB2Driver) {
53
            $value1 = str_replace("\x00", "\xFF", $value1);
54
            $value2 = str_replace("\x00", "\xFF", $value2);
55
        }
56
57
        $this->insert($id1, $value1);
58
        $this->insert($id2, $value2);
59
60
        $this->assertSame($value1, $this->select($id1));
61
        $this->assertSame($value2, $this->select($id2));
62
    }
63
64
    private function insert(string $id, string $value) : void
65
    {
66
        $result = $this->_conn->insert('binary_table', [
67
            'id'  => $id,
68
            'val' => $value,
69
        ], [
70
            ParameterType::LARGE_OBJECT,
71
            ParameterType::LARGE_OBJECT,
72
        ]);
73
74
        self::assertSame(1, $result);
75
    }
76
77
    private function select(string $id) : string
78
    {
79
        $value = $this->_conn->fetchColumn(
80
            'SELECT val FROM binary_table WHERE id = ?',
81
            [$id],
82
            0,
83
            [ParameterType::LARGE_OBJECT]
84
        );
85
86
        // Currently, `BinaryType` mistakenly converts string values fetched from the DB to a stream.
87
        // It should be the opposite. Streams should be used to represent large objects, not binary
88
        // strings. The confusion comes from the PostgreSQL's type system where binary strings and
89
        // large objects are represented by the same BYTEA type
90
        if (is_resource($value)) {
0 ignored issues
show
introduced by
The condition is_resource($value) is always false.
Loading history...
91
            $value = stream_get_contents($value);
92
        }
93
94
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value could return the type boolean which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
95
    }
96
}
97