Skip to content

Commit f776438

Browse files
authored
Improve query builder tests (#53251)
* Improve query builder tests * Lint
1 parent ebd5b6e commit f776438

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,8 +2316,43 @@ public function testWhereShortcut()
23162316
$this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());
23172317
}
23182318

2319+
public function testOrWheresHaveConsistentResults()
2320+
{
2321+
$queries = [];
2322+
$builder = $this->getBuilder();
2323+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhere(['foo' => 1, 'bar' => 2]);
2324+
$queries[] = $builder->toSql();
2325+
2326+
$builder = $this->getBuilder();
2327+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhere([['foo', 1], ['bar', 2]]);
2328+
$queries[] = $builder->toSql();
2329+
2330+
$this->assertSame([
2331+
'select * from "users" where "xxxx" = ? or ("foo" = ? or "bar" = ?)',
2332+
'select * from "users" where "xxxx" = ? or ("foo" = ? or "bar" = ?)',
2333+
], $queries);
2334+
2335+
$queries = [];
2336+
$builder = $this->getBuilder();
2337+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereColumn(['foo' => '_foo', 'bar' => '_bar']);
2338+
$queries[] = $builder->toSql();
2339+
2340+
$builder = $this->getBuilder();
2341+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereColumn([['foo', '_foo'], ['bar', '_bar']]);
2342+
$queries[] = $builder->toSql();
2343+
2344+
$this->assertSame([
2345+
'select * from "users" where "xxxx" = ? or ("foo" = "_foo" or "bar" = "_bar")',
2346+
'select * from "users" where "xxxx" = ? or ("foo" = "_foo" or "bar" = "_bar")',
2347+
], $queries);
2348+
}
2349+
23192350
public function testWhereWithArrayConditions()
23202351
{
2352+
/*
2353+
* where(key, value)
2354+
*/
2355+
23212356
$builder = $this->getBuilder();
23222357
$builder->select('*')->from('users')->where([['foo', 1], ['bar', 2]]);
23232358
$this->assertSame('select * from "users" where ("foo" = ? and "bar" = ?)', $builder->toSql());
@@ -2328,6 +2363,11 @@ public function testWhereWithArrayConditions()
23282363
$this->assertSame('select * from "users" where ("foo" = ? or "bar" = ?)', $builder->toSql());
23292364
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
23302365

2366+
$builder = $this->getBuilder();
2367+
$builder->select('*')->from('users')->where([['foo', 1], ['bar', 2]], boolean: 'and');
2368+
$this->assertSame('select * from "users" where ("foo" = ? and "bar" = ?)', $builder->toSql());
2369+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2370+
23312371
$builder = $this->getBuilder();
23322372
$builder->select('*')->from('users')->where(['foo' => 1, 'bar' => 2]);
23332373
$this->assertSame('select * from "users" where ("foo" = ? and "bar" = ?)', $builder->toSql());
@@ -2338,6 +2378,15 @@ public function testWhereWithArrayConditions()
23382378
$this->assertSame('select * from "users" where ("foo" = ? or "bar" = ?)', $builder->toSql());
23392379
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
23402380

2381+
$builder = $this->getBuilder();
2382+
$builder->select('*')->from('users')->where(['foo' => 1, 'bar' => 2], boolean: 'and');
2383+
$this->assertSame('select * from "users" where ("foo" = ? and "bar" = ?)', $builder->toSql());
2384+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2385+
2386+
/*
2387+
* where(key, <, value)
2388+
*/
2389+
23412390
$builder = $this->getBuilder();
23422391
$builder->select('*')->from('users')->where([['foo', 1], ['bar', '<', 2]]);
23432392
$this->assertSame('select * from "users" where ("foo" = ? and "bar" < ?)', $builder->toSql());
@@ -2347,6 +2396,270 @@ public function testWhereWithArrayConditions()
23472396
$builder->select('*')->from('users')->where([['foo', 1], ['bar', '<', 2]], boolean: 'or');
23482397
$this->assertSame('select * from "users" where ("foo" = ? or "bar" < ?)', $builder->toSql());
23492398
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2399+
2400+
$builder = $this->getBuilder();
2401+
$builder->select('*')->from('users')->where([['foo', 1], ['bar', '<', 2]], boolean: 'and');
2402+
$this->assertSame('select * from "users" where ("foo" = ? and "bar" < ?)', $builder->toSql());
2403+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2404+
2405+
/*
2406+
* whereNot(key, value)
2407+
*/
2408+
2409+
$builder = $this->getBuilder();
2410+
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', 2]]);
2411+
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql());
2412+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2413+
2414+
$builder = $this->getBuilder();
2415+
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', 2]], boolean: 'or');
2416+
$this->assertSame('select * from "users" where not (("foo" = ? or "bar" = ?))', $builder->toSql());
2417+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2418+
2419+
$builder = $this->getBuilder();
2420+
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', 2]], boolean: 'and');
2421+
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql());
2422+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2423+
2424+
$builder = $this->getBuilder();
2425+
$builder->select('*')->from('users')->whereNot(['foo' => 1, 'bar' => 2]);
2426+
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql());
2427+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2428+
2429+
$builder = $this->getBuilder();
2430+
$builder->select('*')->from('users')->whereNot(['foo' => 1, 'bar' => 2], boolean: 'or');
2431+
$this->assertSame('select * from "users" where not (("foo" = ? or "bar" = ?))', $builder->toSql());
2432+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2433+
2434+
$builder = $this->getBuilder();
2435+
$builder->select('*')->from('users')->whereNot(['foo' => 1, 'bar' => 2], boolean: 'and');
2436+
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" = ?))', $builder->toSql());
2437+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2438+
2439+
/*
2440+
* whereNot(key, <, value)
2441+
*/
2442+
2443+
$builder = $this->getBuilder();
2444+
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', '<', 2]]);
2445+
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" < ?))', $builder->toSql());
2446+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2447+
2448+
$builder = $this->getBuilder();
2449+
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', '<', 2]], boolean: 'or');
2450+
$this->assertSame('select * from "users" where not (("foo" = ? or "bar" < ?))', $builder->toSql());
2451+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2452+
2453+
$builder = $this->getBuilder();
2454+
$builder->select('*')->from('users')->whereNot([['foo', 1], ['bar', '<', 2]], boolean: 'and');
2455+
$this->assertSame('select * from "users" where not (("foo" = ? and "bar" < ?))', $builder->toSql());
2456+
$this->assertEquals([0 => 1, 1 => 2], $builder->getBindings());
2457+
2458+
/*
2459+
* whereColumn(col1, col2)
2460+
*/
2461+
2462+
$builder = $this->getBuilder();
2463+
$builder->select('*')->from('users')->whereColumn([['foo', '_foo'], ['bar', '_bar']]);
2464+
$this->assertSame('select * from "users" where ("foo" = "_foo" and "bar" = "_bar")', $builder->toSql());
2465+
$this->assertEquals([], $builder->getBindings());
2466+
2467+
$builder = $this->getBuilder();
2468+
$builder->select('*')->from('users')->whereColumn([['foo', '_foo'], ['bar', '_bar']], boolean: 'or');
2469+
$this->assertSame('select * from "users" where ("foo" = "_foo" or "bar" = "_bar")', $builder->toSql());
2470+
$this->assertEquals([], $builder->getBindings());
2471+
2472+
$builder = $this->getBuilder();
2473+
$builder->select('*')->from('users')->whereColumn([['foo', '_foo'], ['bar', '_bar']], boolean: 'and');
2474+
$this->assertSame('select * from "users" where ("foo" = "_foo" and "bar" = "_bar")', $builder->toSql());
2475+
$this->assertEquals([], $builder->getBindings());
2476+
2477+
$builder = $this->getBuilder();
2478+
$builder->select('*')->from('users')->whereColumn(['foo' => '_foo', 'bar' => '_bar']);
2479+
$this->assertSame('select * from "users" where ("foo" = "_foo" and "bar" = "_bar")', $builder->toSql());
2480+
$this->assertEquals([], $builder->getBindings());
2481+
2482+
$builder = $this->getBuilder();
2483+
$builder->select('*')->from('users')->whereColumn(['foo' => '_foo', 'bar' => '_bar'], boolean: 'or');
2484+
$this->assertSame('select * from "users" where ("foo" = "_foo" or "bar" = "_bar")', $builder->toSql());
2485+
$this->assertEquals([], $builder->getBindings());
2486+
2487+
$builder = $this->getBuilder();
2488+
$builder->select('*')->from('users')->whereColumn(['foo' => '_foo', 'bar' => '_bar'], boolean: 'and');
2489+
$this->assertSame('select * from "users" where ("foo" = "_foo" and "bar" = "_bar")', $builder->toSql());
2490+
$this->assertEquals([], $builder->getBindings());
2491+
2492+
/*
2493+
* whereColumn(col1, <, col2)
2494+
*/
2495+
2496+
$builder = $this->getBuilder();
2497+
$builder->select('*')->from('users')->whereColumn([['foo', '_foo'], ['bar', '<', '_bar']]);
2498+
$this->assertSame('select * from "users" where ("foo" = "_foo" and "bar" < "_bar")', $builder->toSql());
2499+
$this->assertEquals([], $builder->getBindings());
2500+
2501+
$builder = $this->getBuilder();
2502+
$builder->select('*')->from('users')->whereColumn([['foo', '_foo'], ['bar', '<', '_bar']], boolean: 'or');
2503+
$this->assertSame('select * from "users" where ("foo" = "_foo" or "bar" < "_bar")', $builder->toSql());
2504+
$this->assertEquals([], $builder->getBindings());
2505+
2506+
$builder = $this->getBuilder();
2507+
$builder->select('*')->from('users')->whereColumn([['foo', '_foo'], ['bar', '<', '_bar']], boolean: 'and');
2508+
$this->assertSame('select * from "users" where ("foo" = "_foo" and "bar" < "_bar")', $builder->toSql());
2509+
$this->assertEquals([], $builder->getBindings());
2510+
2511+
/*
2512+
* whereAll([...keys], value)
2513+
*/
2514+
2515+
$builder = $this->getBuilder();
2516+
$builder->select('*')->from('users')->whereAll(['foo', 'bar'], 2);
2517+
$this->assertSame('select * from "users" where ("foo" = ? and "bar" = ?)', $builder->toSql());
2518+
$this->assertEquals([0 => 2, 1 => 2], $builder->getBindings());
2519+
2520+
$builder = $this->getBuilder();
2521+
$builder->select('*')->from('users')->whereAll(['foo', 'bar'], 2);
2522+
$this->assertSame('select * from "users" where ("foo" = ? and "bar" = ?)', $builder->toSql());
2523+
$this->assertEquals([0 => 2, 1 => 2], $builder->getBindings());
2524+
2525+
/*
2526+
* whereAny([...keys], value)
2527+
*/
2528+
2529+
$builder = $this->getBuilder();
2530+
$builder->select('*')->from('users')->whereAny(['foo', 'bar'], 2);
2531+
$this->assertSame('select * from "users" where ("foo" = ? or "bar" = ?)', $builder->toSql());
2532+
$this->assertEquals([0 => 2, 1 => 2], $builder->getBindings());
2533+
2534+
$builder = $this->getBuilder();
2535+
$builder->select('*')->from('users')->whereAny(['foo', 'bar'], 2);
2536+
$this->assertSame('select * from "users" where ("foo" = ? or "bar" = ?)', $builder->toSql());
2537+
$this->assertEquals([0 => 2, 1 => 2], $builder->getBindings());
2538+
2539+
/*
2540+
* whereNone([...keys], value)
2541+
*/
2542+
2543+
$builder = $this->getBuilder();
2544+
$builder->select('*')->from('users')->whereNone(['foo', 'bar'], 2);
2545+
$this->assertSame('select * from "users" where not ("foo" = ? or "bar" = ?)', $builder->toSql());
2546+
$this->assertEquals([0 => 2, 1 => 2], $builder->getBindings());
2547+
2548+
$builder = $this->getBuilder();
2549+
$builder->select('*')->from('users')->whereNone(['foo', 'bar'], 2);
2550+
$this->assertSame('select * from "users" where not ("foo" = ? or "bar" = ?)', $builder->toSql());
2551+
$this->assertEquals([0 => 2, 1 => 2], $builder->getBindings());
2552+
2553+
/*
2554+
* where()->orWhere(key, value)
2555+
*/
2556+
2557+
$builder = $this->getBuilder();
2558+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhere([['foo', 1], ['bar', 2]]);
2559+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = ? or "bar" = ?)', $builder->toSql());
2560+
$this->assertEquals([0 => 'xxxx', 1 => 1, 2 => 2], $builder->getBindings());
2561+
2562+
$builder = $this->getBuilder();
2563+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhere(['foo' => 1, 'bar' => 2]);
2564+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = ? or "bar" = ?)', $builder->toSql());
2565+
$this->assertEquals([0 => 'xxxx', 1 => 1, 2 => 2], $builder->getBindings());
2566+
2567+
/*
2568+
* where()->orWhere(key, <, value)
2569+
*/
2570+
2571+
$builder = $this->getBuilder();
2572+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhere([['foo', 1], ['bar', '<', 2]]);
2573+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = ? or "bar" < ?)', $builder->toSql());
2574+
$this->assertEquals([0 => 'xxxx', 1 => 1, 2 => 2], $builder->getBindings());
2575+
2576+
/*
2577+
* where()->orWhereColumn(col1, col2)
2578+
*/
2579+
2580+
$builder = $this->getBuilder();
2581+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereColumn([['foo', '_foo'], ['bar', '_bar']]);
2582+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = "_foo" or "bar" = "_bar")', $builder->toSql());
2583+
$this->assertEquals([0 => 'xxxx'], $builder->getBindings());
2584+
2585+
$builder = $this->getBuilder();
2586+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereColumn(['foo' => '_foo', 'bar' => '_bar']);
2587+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = "_foo" or "bar" = "_bar")', $builder->toSql());
2588+
$this->assertEquals([0 => 'xxxx'], $builder->getBindings());
2589+
2590+
/*
2591+
* where()->orWhere(key, <, value)
2592+
*/
2593+
2594+
$builder = $this->getBuilder();
2595+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhere([['foo', 1], ['bar', '<', 2]]);
2596+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = ? or "bar" < ?)', $builder->toSql());
2597+
$this->assertEquals([0 => 'xxxx', 1 => 1, 2 => 2], $builder->getBindings());
2598+
2599+
/*
2600+
* where()->orWhereNot(key, value)
2601+
*/
2602+
2603+
$builder = $this->getBuilder();
2604+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereNot([['foo', 1], ['bar', 2]]);
2605+
$this->assertSame('select * from "users" where "xxxx" = ? or not (("foo" = ? or "bar" = ?))', $builder->toSql());
2606+
$this->assertEquals([0 => 'xxxx', 1 => 1, 2 => 2], $builder->getBindings());
2607+
2608+
$builder = $this->getBuilder();
2609+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereNot(['foo' => 1, 'bar' => 2]);
2610+
$this->assertSame('select * from "users" where "xxxx" = ? or not (("foo" = ? or "bar" = ?))', $builder->toSql());
2611+
$this->assertEquals([0 => 'xxxx', 1 => 1, 2 => 2], $builder->getBindings());
2612+
2613+
/*
2614+
* where()->orWhereNot(key, <, value)
2615+
*/
2616+
2617+
$builder = $this->getBuilder();
2618+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereNot([['foo', 1], ['bar', '<', 2]]);
2619+
$this->assertSame('select * from "users" where "xxxx" = ? or not (("foo" = ? or "bar" < ?))', $builder->toSql());
2620+
$this->assertEquals([0 => 'xxxx', 1 => 1, 2 => 2], $builder->getBindings());
2621+
2622+
/*
2623+
* where()->orWhereAll([...keys], value)
2624+
*/
2625+
2626+
$builder = $this->getBuilder();
2627+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereAll(['foo', 'bar'], 2);
2628+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = ? and "bar" = ?)', $builder->toSql());
2629+
$this->assertEquals([0 => 'xxxx', 1 => 2, 2 => 2], $builder->getBindings());
2630+
2631+
$builder = $this->getBuilder();
2632+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereAll(['foo', 'bar'], 2);
2633+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = ? and "bar" = ?)', $builder->toSql());
2634+
$this->assertEquals([0 => 'xxxx', 1 => 2, 2 => 2], $builder->getBindings());
2635+
2636+
/*
2637+
* where()->orWhereAny([...keys], value)
2638+
*/
2639+
2640+
$builder = $this->getBuilder();
2641+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereAny(['foo', 'bar'], 2);
2642+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = ? or "bar" = ?)', $builder->toSql());
2643+
$this->assertEquals([0 => 'xxxx', 1 => 2, 2 => 2], $builder->getBindings());
2644+
2645+
$builder = $this->getBuilder();
2646+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereAny(['foo', 'bar'], 2);
2647+
$this->assertSame('select * from "users" where "xxxx" = ? or ("foo" = ? or "bar" = ?)', $builder->toSql());
2648+
$this->assertEquals([0 => 'xxxx', 1 => 2, 2 => 2], $builder->getBindings());
2649+
2650+
/*
2651+
* where()->orWhereNone([...keys], value)
2652+
*/
2653+
2654+
$builder = $this->getBuilder();
2655+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereNone(['foo', 'bar'], 2);
2656+
$this->assertSame('select * from "users" where "xxxx" = ? or not ("foo" = ? or "bar" = ?)', $builder->toSql());
2657+
$this->assertEquals([0 => 'xxxx', 1 => 2, 2 => 2], $builder->getBindings());
2658+
2659+
$builder = $this->getBuilder();
2660+
$builder->select('*')->from('users')->where('xxxx', 'xxxx')->orWhereNone(['foo', 'bar'], 2);
2661+
$this->assertSame('select * from "users" where "xxxx" = ? or not ("foo" = ? or "bar" = ?)', $builder->toSql());
2662+
$this->assertEquals([0 => 'xxxx', 1 => 2, 2 => 2], $builder->getBindings());
23502663
}
23512664

23522665
public function testNestedWheres()

0 commit comments

Comments
 (0)