NamedParametersTest.php 6.66 KB
Newer Older
1 2
<?php

3
namespace Doctrine\Tests\DBAL\Functional;
4 5

use Doctrine\DBAL\Connection;
6 7
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
8
use Doctrine\DBAL\Schema\Table;
Sergei Morozov's avatar
Sergei Morozov committed
9 10
use Doctrine\Tests\DbalFunctionalTestCase;
use Throwable;
11 12
use const CASE_LOWER;
use function array_change_key_case;
13 14 15 16

/**
 * @group DDC-1372
 */
Sergei Morozov's avatar
Sergei Morozov committed
17
class NamedParametersTest extends DbalFunctionalTestCase
18 19 20
{
    public function ticketProvider()
    {
21 22
        return [
            [
23
                'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)',
24 25 26 27 28
                [
                    'foo' => 1,
                    'bar' => [1, 2, 3],
                ],
                [
29 30
                    'foo' => ParameterType::INTEGER,
                    'bar' => Connection::PARAM_INT_ARRAY,
31 32 33 34 35 36 37 38 39
                ],
                [
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
                ],
            ],

            [
40
                'SELECT * FROM ddc1372_foobar f WHERE f.foo = :foo AND f.bar IN (:bar)',
41 42 43 44 45
                [
                    'foo' => 1,
                    'bar' => [1, 2, 3],
                ],
                [
46 47
                    'bar' => Connection::PARAM_INT_ARRAY,
                    'foo' => ParameterType::INTEGER,
48 49 50 51 52 53 54 55 56
                ],
                [
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
                ],
            ],

            [
57
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo',
58 59 60 61 62
                [
                    'foo' => 1,
                    'bar' => [1, 2, 3],
                ],
                [
63 64
                    'bar' => Connection::PARAM_INT_ARRAY,
                    'foo' => ParameterType::INTEGER,
65 66 67 68 69 70 71 72 73
                ],
                [
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
                ],
            ],

            [
74
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo = :foo',
75 76 77 78 79
                [
                    'foo' => 1,
                    'bar' => ['1', '2', '3'],
                ],
                [
80 81
                    'bar' => Connection::PARAM_STR_ARRAY,
                    'foo' => ParameterType::INTEGER,
82 83 84 85 86 87 88 89 90
                ],
                [
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
                ],
            ],

            [
91
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)',
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                [
                    'foo' => ['1'],
                    'bar' => [1, 2, 3, 4],
                ],
                [
                    'bar' => Connection::PARAM_STR_ARRAY,
                    'foo' => Connection::PARAM_INT_ARRAY,
                ],
                [
                    ['id' => 1, 'foo' => 1, 'bar' => 1],
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
                    ['id' => 4, 'foo' => 1, 'bar' => 4],
                ],
            ],

            [
109
                'SELECT * FROM ddc1372_foobar f WHERE f.bar IN (:bar) AND f.foo IN (:foo)',
110 111 112 113 114 115 116 117 118 119 120 121 122 123
                [
                    'foo' => 1,
                    'bar' => 2,
                ],
                [
                    'bar' => ParameterType::INTEGER,
                    'foo' => ParameterType::INTEGER,
                ],
                [
                    ['id' => 2, 'foo' => 1, 'bar' => 2],
                ],
            ],

            [
124
                'SELECT * FROM ddc1372_foobar f WHERE f.bar = :arg AND f.foo <> :arg',
125 126
                ['arg' => '1'],
                [
127
                    'arg' => ParameterType::STRING,
128 129 130 131 132
                ],
                [
                    ['id' => 5, 'foo' => 2, 'bar' => 1],
                ],
            ],
133

134
            [
135
                'SELECT * FROM ddc1372_foobar f WHERE f.bar NOT IN (:arg) AND f.foo IN (:arg)',
136 137 138 139 140 141 142 143 144 145 146 147
                [
                    'arg' => [1, 2],
                ],
                [
                    'arg' => Connection::PARAM_INT_ARRAY,
                ],
                [
                    ['id' => 3, 'foo' => 1, 'bar' => 3],
                    ['id' => 4, 'foo' => 1, 'bar' => 4],
                ],
            ],
        ];
148
    }
149

150
    protected function setUp()
151 152 153
    {
        parent::setUp();

Sergei Morozov's avatar
Sergei Morozov committed
154
        if ($this->connection->getSchemaManager()->tablesExist('ddc1372_foobar')) {
Sergei Morozov's avatar
Sergei Morozov committed
155 156
            return;
        }
157

Sergei Morozov's avatar
Sergei Morozov committed
158 159 160 161 162 163 164
        try {
            $table = new Table('ddc1372_foobar');
            $table->addColumn('id', 'integer');
            $table->addColumn('foo', 'string');
            $table->addColumn('bar', 'string');
            $table->setPrimaryKey(['id']);

Sergei Morozov's avatar
Sergei Morozov committed
165
            $sm = $this->connection->getSchemaManager();
Sergei Morozov's avatar
Sergei Morozov committed
166 167
            $sm->createTable($table);

Sergei Morozov's avatar
Sergei Morozov committed
168
            $this->connection->insert('ddc1372_foobar', [
Sergei Morozov's avatar
Sergei Morozov committed
169 170 171 172
                'id'  => 1,
                'foo' => 1,
                'bar' => 1,
            ]);
Sergei Morozov's avatar
Sergei Morozov committed
173
            $this->connection->insert('ddc1372_foobar', [
Sergei Morozov's avatar
Sergei Morozov committed
174 175 176 177
                'id'  => 2,
                'foo' => 1,
                'bar' => 2,
            ]);
Sergei Morozov's avatar
Sergei Morozov committed
178
            $this->connection->insert('ddc1372_foobar', [
Sergei Morozov's avatar
Sergei Morozov committed
179 180 181 182
                'id'  => 3,
                'foo' => 1,
                'bar' => 3,
            ]);
Sergei Morozov's avatar
Sergei Morozov committed
183
            $this->connection->insert('ddc1372_foobar', [
Sergei Morozov's avatar
Sergei Morozov committed
184 185 186 187
                'id'  => 4,
                'foo' => 1,
                'bar' => 4,
            ]);
Sergei Morozov's avatar
Sergei Morozov committed
188
            $this->connection->insert('ddc1372_foobar', [
Sergei Morozov's avatar
Sergei Morozov committed
189 190 191 192
                'id'  => 5,
                'foo' => 2,
                'bar' => 1,
            ]);
Sergei Morozov's avatar
Sergei Morozov committed
193
            $this->connection->insert('ddc1372_foobar', [
Sergei Morozov's avatar
Sergei Morozov committed
194 195 196 197 198 199
                'id'  => 6,
                'foo' => 2,
                'bar' => 2,
            ]);
        } catch (Throwable $e) {
            $this->fail($e->getMessage());
200 201 202 203
        }
    }

    /**
Sergei Morozov's avatar
Sergei Morozov committed
204 205 206 207
     * @param string  $query
     * @param mixed[] $params
     * @param int[]   $types
     * @param int[]   $expected
Sergei Morozov's avatar
Sergei Morozov committed
208 209
     *
     * @dataProvider ticketProvider
210
     */
Sergei Morozov's avatar
Sergei Morozov committed
211
    public function testTicket($query, $params, $types, $expected)
212
    {
Sergei Morozov's avatar
Sergei Morozov committed
213
        $stmt   = $this->connection->executeQuery($query, $params, $types);
214
        $result = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
215

216 217 218 219
        foreach ($result as $k => $v) {
            $result[$k] = array_change_key_case($v, CASE_LOWER);
        }

220
        self::assertEquals($result, $expected);
221
    }
222
}