|
5 | 5 | namespace Cycle\Database\Tests\Functional\Driver\Common\Query;
|
6 | 6 |
|
7 | 7 | use Cycle\Database\Exception\BuilderException;
|
| 8 | +use Cycle\Database\Exception\CompilerException\UnexpectedOperatorException; |
8 | 9 | use Cycle\Database\Injection\Expression;
|
9 | 10 | use Cycle\Database\Injection\Fragment;
|
10 | 11 | use Cycle\Database\Injection\Parameter;
|
@@ -334,12 +335,13 @@ public function testSelectWithWhereOrWhere(): void
|
334 | 335 |
|
335 | 336 | public function testSelectInvalidArrayArgument(): void
|
336 | 337 | {
|
337 |
| - $this->expectException(BuilderException::class); |
| 338 | + $this->expectException(UnexpectedOperatorException::class); |
338 | 339 |
|
339 | 340 | $this->database->select()->distinct()
|
340 | 341 | ->from(['users'])
|
341 | 342 | ->where('name', 'Anton')
|
342 |
| - ->orWhere('id', 'like', [1, 2, 3]); |
| 343 | + ->orWhere('id', 'like', [1, 2, 3]) |
| 344 | + ->sqlStatement(); |
343 | 345 | }
|
344 | 346 |
|
345 | 347 | public function testSelectWithWhereOrWhereAndWhere(): void
|
@@ -1917,16 +1919,16 @@ public function testInOperatorWithBadArrayParameter(): void
|
1917 | 1919 |
|
1918 | 1920 | public function testBadArrayParameterInShortWhere(): void
|
1919 | 1921 | {
|
1920 |
| - $this->expectException(BuilderException::class); |
1921 |
| - $this->expectExceptionMessage('Arrays must be wrapped with Parameter instance'); |
| 1922 | + $this->expectException(UnexpectedOperatorException::class); |
1922 | 1923 |
|
1923 |
| - $this->database->select() |
1924 |
| - ->from(['users']) |
1925 |
| - ->where( |
1926 |
| - [ |
1927 |
| - 'status' => ['LIKE' => ['active', 'blocked']], |
1928 |
| - ] |
1929 |
| - ); |
| 1924 | + $this->database |
| 1925 | + ->select() |
| 1926 | + ->from(['users']) |
| 1927 | + ->where( |
| 1928 | + [ |
| 1929 | + 'status' => ['LIKE' => ['active', 'blocked']], |
| 1930 | + ] |
| 1931 | + )->sqlStatement(); |
1930 | 1932 | }
|
1931 | 1933 |
|
1932 | 1934 | public function testGoodArrayParameter(): void
|
@@ -2165,4 +2167,104 @@ public function testSelectWithFragmentedColumns(): void
|
2165 | 2167 | $select
|
2166 | 2168 | );
|
2167 | 2169 | }
|
| 2170 | + |
| 2171 | + public function testWhereInWithoutSpecifiedOperator(): void |
| 2172 | + { |
| 2173 | + $select = $this->database |
| 2174 | + ->select() |
| 2175 | + ->from(['users']) |
| 2176 | + ->where( |
| 2177 | + 'uuid', |
| 2178 | + new Parameter(['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013']) |
| 2179 | + ); |
| 2180 | + |
| 2181 | + $this->assertSameQuery('SELECT * FROM {users} WHERE {uuid} IN (?, ?)', $select); |
| 2182 | + |
| 2183 | + $this->assertSameParameters( |
| 2184 | + ['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013'], |
| 2185 | + $select, |
| 2186 | + ); |
| 2187 | + } |
| 2188 | + |
| 2189 | + public function testWhereInWithEqualSpecifiedOperator(): void |
| 2190 | + { |
| 2191 | + $select = $this->database |
| 2192 | + ->select() |
| 2193 | + ->from(['users']) |
| 2194 | + ->where( |
| 2195 | + 'uuid', |
| 2196 | + '=', |
| 2197 | + new Parameter(['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013']) |
| 2198 | + )->orWhere( |
| 2199 | + 'uuid', |
| 2200 | + '=', |
| 2201 | + ['23456789-1234-1234-1234-123456789012', '23456789-1234-1234-1234-123456789013'] |
| 2202 | + ); |
| 2203 | + |
| 2204 | + $this->assertSameQuery('SELECT * FROM {users} WHERE {uuid} IN (?, ?) OR {uuid} IN (?, ?)', $select); |
| 2205 | + |
| 2206 | + $this->assertSameParameters( |
| 2207 | + [ |
| 2208 | + '12345678-1234-1234-1234-123456789012', |
| 2209 | + '12345678-1234-1234-1234-123456789013', |
| 2210 | + '23456789-1234-1234-1234-123456789012', |
| 2211 | + '23456789-1234-1234-1234-123456789013', |
| 2212 | + ], |
| 2213 | + $select, |
| 2214 | + ); |
| 2215 | + } |
| 2216 | + |
| 2217 | + public function testWhereInWithNotEqualSpecifiedOperator(): void |
| 2218 | + { |
| 2219 | + $select = $this->database |
| 2220 | + ->select() |
| 2221 | + ->from(['users']) |
| 2222 | + ->where( |
| 2223 | + 'uuid', |
| 2224 | + '!=', |
| 2225 | + new Parameter(['12345678-1234-1234-1234-123456789012', '12345678-1234-1234-1234-123456789013']) |
| 2226 | + )->orWhere( |
| 2227 | + 'uuid', |
| 2228 | + '!=', |
| 2229 | + ['23456789-1234-1234-1234-123456789012', '23456789-1234-1234-1234-123456789013'] |
| 2230 | + ); |
| 2231 | + |
| 2232 | + $this->assertSameQuery('SELECT * FROM {users} WHERE {uuid} NOT IN (?, ?) OR {uuid} NOT IN (?, ?)', $select); |
| 2233 | + |
| 2234 | + $this->assertSameParameters( |
| 2235 | + [ |
| 2236 | + '12345678-1234-1234-1234-123456789012', |
| 2237 | + '12345678-1234-1234-1234-123456789013', |
| 2238 | + '23456789-1234-1234-1234-123456789012', |
| 2239 | + '23456789-1234-1234-1234-123456789013', |
| 2240 | + ], |
| 2241 | + $select, |
| 2242 | + ); |
| 2243 | + } |
| 2244 | + |
| 2245 | + public function testFragmentInWhereInClause(): void |
| 2246 | + { |
| 2247 | + $select = $this->database |
| 2248 | + ->select() |
| 2249 | + ->from(['users']) |
| 2250 | + ->where('uuid', new Fragment('UUID_TO_BIN(?)', '12345678-1234-1234-1234-123456789012')) |
| 2251 | + ->andWhere('uuid', 'IN', [ |
| 2252 | + new Fragment('UUID_TO_BIN(?)', '12345678-1234-1234-1234-123456789013'), |
| 2253 | + new Fragment('UUID_TO_BIN(?)', '12345678-1234-1234-1234-123456789014'), |
| 2254 | + ]); |
| 2255 | + |
| 2256 | + $this->assertSameQuery( |
| 2257 | + 'SELECT * FROM {users} WHERE {uuid} = UUID_TO_BIN (?) AND {uuid} IN (UUID_TO_BIN (?), UUID_TO_BIN (?))', |
| 2258 | + $select |
| 2259 | + ); |
| 2260 | + |
| 2261 | + $this->assertSameParameters( |
| 2262 | + [ |
| 2263 | + '12345678-1234-1234-1234-123456789012', |
| 2264 | + '12345678-1234-1234-1234-123456789013', |
| 2265 | + '12345678-1234-1234-1234-123456789014', |
| 2266 | + ], |
| 2267 | + $select |
| 2268 | + ); |
| 2269 | + } |
2168 | 2270 | }
|
0 commit comments