|
8 | 8 | use Kir\MySQL\Builder\Expr\RequiredValueNotFoundException;
|
9 | 9 | use Kir\MySQL\Builder\SelectTest\TestSelect;
|
10 | 10 | use Kir\MySQL\Databases\TestDB;
|
| 11 | +use Kir\MySQL\Tools\VirtualTable; |
11 | 12 |
|
12 | 13 | class SelectTestX extends \PHPUnit_Framework_TestCase {
|
13 | 14 | public function testAddition() {
|
@@ -365,4 +366,29 @@ public function testVirtualTables() {
|
365 | 366 |
|
366 | 367 | $this->assertEquals("SELECT\n\tt.field1,\n\tt.field2\nFROM\n\ttest t\nINNER JOIN\n\t(SELECT\n\t\ta.field1\n\tFROM\n\t\ttableA a) vt1 ON vt1.field1=t.field1\nINNER JOIN\n\t(SELECT\n\t\ta.field1\n\tFROM\n\t\ttableA a) vt2 ON vt2.field2=t.field2\n", $query);
|
367 | 368 | }
|
| 369 | + |
| 370 | + public function testParametrizedVirtualTables() { |
| 371 | + $vt1 = TestSelect::create() |
| 372 | + ->field('a.field1') |
| 373 | + ->from('a', 'tableA'); |
| 374 | + |
| 375 | + $db = new TestDB(); |
| 376 | + $db->getVirtualTables()->add('virt_table1', $vt1); |
| 377 | + $db->getVirtualTables()->add('virt_table2', function (array $args) { |
| 378 | + return TestSelect::create() |
| 379 | + ->field('a.field1') |
| 380 | + ->from('a', 'tableA') |
| 381 | + ->where(new DBExprFilter('a.active=?', $args, 'active')); |
| 382 | + }); |
| 383 | + |
| 384 | + $query = TestSelect::create($db) |
| 385 | + ->field('t.field1') |
| 386 | + ->field('t.field2') |
| 387 | + ->from('t', 'test') |
| 388 | + ->joinInner('vt1', 'virt_table1', 'vt1.field1=t.field1') |
| 389 | + ->joinInner('vt2', new VirtualTable('virt_table2', ['active' => true]), 'vt2.field2=t.field2') |
| 390 | + ->asString(); |
| 391 | + |
| 392 | + $this->assertEquals("SELECT\n\tt.field1,\n\tt.field2\nFROM\n\ttest t\nINNER JOIN\n\t(SELECT\n\t\ta.field1\n\tFROM\n\t\ttableA a) vt1 ON vt1.field1=t.field1\nINNER JOIN\n\t(SELECT\n\t\ta.field1\n\tFROM\n\t\ttableA a\n\tWHERE\n\t\t(a.active='1')) vt2 ON vt2.field2=t.field2\n", $query); |
| 393 | + } |
368 | 394 | }
|
0 commit comments