Completed
Pull Request — master (#3738)
by
unknown
60:54
created

StatementTest::testStatementBindParameters()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 20
rs 9.8666
c 1
b 0
f 0
cc 4
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional\Driver\OCI8;
6
7
use Doctrine\DBAL\Driver\OCI8\Driver;
8
use Doctrine\Tests\DbalFunctionalTestCase;
9
use function array_key_exists;
10
use function extension_loaded;
11
use function is_int;
12
13
class StatementTest extends DbalFunctionalTestCase
14
{
15
    protected function setUp() : void
16
    {
17
        if (! extension_loaded('oci8')) {
18
            $this->markTestSkipped('oci8 is not installed.');
19
        }
20
21
        parent::setUp();
22
23
        if ($this->connection->getDriver() instanceof Driver) {
24
            return;
25
        }
26
27
        $this->markTestSkipped('oci8 only test.');
28
    }
29
30
    /**
31
     * @param mixed[] $params
32
     * @param mixed[] $expected
33
     *
34
     * @dataProvider queryConversionProvider
35
     */
36
    public function testQueryConversion(string $query, array $params, array $expected) : void
37
    {
38
        self::assertEquals(
39
            $expected,
40
            $this->connection->executeQuery($query, $params)->fetch()
41
        );
42
    }
43
44
    /**
45
     * Low-level approach to working with parameter binding
46
     *
47
     * @param mixed[] $params
48
     * @param mixed[] $expected
49
     *
50
     * @dataProvider queryConversionProvider
51
     */
52
    public function testStatementBindParameters(string $query, array $params, array $expected) : void
53
    {
54
        $stmt         = $this->connection->prepare($query);
55
        $hasZeroIndex = array_key_exists(0, $params);
56
57
        foreach ($params as $key => $val) {
58
            if ($hasZeroIndex && is_int($key)) {
59
                $param = $key + 1;
60
            } else {
61
                $param = $key;
62
            }
63
64
            $stmt->bindParam($param, $val);
65
        }
66
67
        $stmt->execute();
68
69
        self::assertEquals(
70
            $expected,
71
            $stmt->fetch()
72
        );
73
    }
74
75
    /**
76
     * @return array<string, array<int, mixed>>
77
     */
78
    public static function queryConversionProvider() : iterable
79
    {
80
        return [
81
            'positional' => [
82
                'SELECT ? COL1 FROM DUAL',
83
                [1],
84
                ['COL1' => 1],
85
            ],
86
            'named' => [
87
                'SELECT :COL COL1 FROM DUAL',
88
                [':COL' => 1],
89
                ['COL1' => 1],
90
            ],
91
            'literal-with-placeholder' => [
92
                "SELECT '?' COL1, ? COL2 FROM DUAL",
93
                [2],
94
                [
95
                    'COL1' => '?',
96
                    'COL2' => 2,
97
                ],
98
            ],
99
            'literal-with-quotes' => [
100
                "SELECT ? COL1, '?\"?''?' \"COL?\" FROM DUAL",
101
                [3],
102
                [
103
                    'COL1' => 3,
104
                    'COL?' => '?"?\'?',
105
                ],
106
            ],
107
            'placeholder-at-the-end' => [
108
                'SELECT ? COL1 FROM DUAL WHERE 1 = ?',
109
                [4, 1],
110
                ['COL1' => 4],
111
            ],
112
            'multi-line-literal' => [
113
                "SELECT 'Hello,
114
World?!' COL1 FROM DUAL WHERE 1 = ?",
115
                [1],
116
                [
117
                    'COL1' => 'Hello,
118
World?!',
119
                ],
120
            ],
121
            'empty-literal' => [
122
                "SELECT '' COL1 FROM DUAL",
123
                [],
124
                ['COL1' => ''],
125
            ],
126
        ];
127
    }
128
}
129