Completed
Pull Request — 2.11.x (#3891)
by Malte
65:11
created

MysqliStatementTest::prepareProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 40
rs 9.552
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Driver\Mysqli;
4
5
use Doctrine\DBAL\Driver\Mysqli\Driver;
6
use Doctrine\DBAL\FetchMode;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\Tests\DbalFunctionalTestCase;
9
use Throwable;
10
use const CASE_LOWER;
11
use function array_change_key_case;
12
use function extension_loaded;
13
14
final class MysqliStatementTest extends DbalFunctionalTestCase
15
{
16
    /**
17
     * @return iterable<string, array<string, mixed[], mixed[]>>
18
     */
19
    public static function prepareProvider() : iterable
20
    {
21
        return [
22
            'single named parameter' => [
23
                'SELECT * FROM mysqli_statement_test f WHERE f.foo = :foo',
24
                ['foo' => 1],
25
                [
26
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
27
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
28
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
29
                    ['id' => 4, 'foo' => 1, 'bar' => 4],
30
                ],
31
            ],
32
33
            'multiple parameters' => [
34
                'SELECT * FROM mysqli_statement_test f WHERE f.foo = :foo AND f.bar = :bar',
35
                [
36
                    'foo' => 1,
37
                    'bar' => 2,
38
                ],
39
                [
40
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
41
                ],
42
            ],
43
44
            'same parameter at multiple positions' => [
45
                'SELECT * FROM mysqli_statement_test f WHERE f.foo = :foo AND f.foo IN (:foo)',
46
                ['foo' => 1],
47
                [
48
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
49
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
50
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
51
                    ['id' => 4, 'foo' => 1, 'bar' => 4],
52
                ],
53
            ],
54
55
            'parameter with string value' => [
56
                'SELECT * FROM mysqli_statement_test f WHERE f.foo = :foo',
57
                ['foo' => '"\''],
58
                [],
59
            ],
60
        ];
61
    }
62
63
    protected function setUp() : void
64
    {
65
        if (! extension_loaded('mysqli')) {
66
            $this->markTestSkipped('mysqli is not installed.');
67
        }
68
69
        parent::setUp();
70
71
        if (! ($this->connection->getDriver() instanceof Driver)) {
72
            $this->markTestSkipped('MySQLi only test.');
73
        }
74
75
        if ($this->connection->getSchemaManager()->tablesExist('mysqli_statement_test')) {
76
            return;
77
        }
78
79
        try {
80
            $table = new Table('mysqli_statement_test');
81
            $table->addColumn('id', 'integer');
82
            $table->addColumn('foo', 'string');
83
            $table->addColumn('bar', 'string');
84
            $table->setPrimaryKey(['id']);
85
86
            $sm = $this->connection->getSchemaManager();
87
            $sm->createTable($table);
88
89
            $this->connection->insert('mysqli_statement_test', [
90
                'id'  => 1,
91
                'foo' => 1,
92
                'bar' => 1,
93
            ]);
94
            $this->connection->insert('mysqli_statement_test', [
95
                'id'  => 2,
96
                'foo' => 1,
97
                'bar' => 2,
98
            ]);
99
            $this->connection->insert('mysqli_statement_test', [
100
                'id'  => 3,
101
                'foo' => 1,
102
                'bar' => 3,
103
            ]);
104
            $this->connection->insert('mysqli_statement_test', [
105
                'id'  => 4,
106
                'foo' => 1,
107
                'bar' => 4,
108
            ]);
109
            $this->connection->insert('mysqli_statement_test', [
110
                'id'  => 5,
111
                'foo' => 2,
112
                'bar' => 1,
113
            ]);
114
            $this->connection->insert('mysqli_statement_test', [
115
                'id'  => 6,
116
                'foo' => 2,
117
                'bar' => 2,
118
            ]);
119
        } catch (Throwable $e) {
120
            $this->fail($e->getMessage());
121
        }
122
    }
123
124
    /**
125
     * @param array<string, mixed>             $params
126
     * @param array<int, array<string, mixed>> $expected
127
     *
128
     * @dataProvider prepareProvider
129
     */
130
    public function testPrepare(string $query, array $params, array $expected) : void
131
    {
132
        $stmt = $this->connection->prepare($query);
133
        $stmt->execute($params);
134
135
        $result = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
136
        foreach ($result as $k => $v) {
137
            $result[$k] = array_change_key_case($v, CASE_LOWER);
138
        }
139
140
        self::assertEquals($result, $expected);
141
    }
142
}
143