| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Rdbms; |
| 4 |
|
| 5 |
use DatabaseTestHelper; |
| 6 |
use Wikimedia\Rdbms\IResultWrapper; |
| 7 |
use Wikimedia\Rdbms\UnionQueryBuilder; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class UnionQueryBuilderTest extends \MediaWikiUnitTestCase { |
| 13 |
|
| 14 |
private $db; |
| 15 |
|
| 16 |
|
| 17 |
private $uqb; |
| 18 |
|
| 19 |
protected function setUp(): void { |
| 20 |
$this->db = new DatabaseTestHelper( __CLASS__ . '::' . $this->getName() ); |
| 21 |
$this->uqb = $this->db->newUnionQueryBuilder(); |
| 22 |
} |
| 23 |
|
| 24 |
private function assertSql( $expected ) { |
| 25 |
$actual = $this->uqb->getSQL(); |
| 26 |
$actual = preg_replace( '/ +/', ' ', $actual ); |
| 27 |
$actual = rtrim( $actual, " " ); |
| 28 |
$this->assertEquals( $expected, $actual ); |
| 29 |
} |
| 30 |
|
| 31 |
private function assertLastSql( $expected ) { |
| 32 |
$actual = $this->db->getLastSqls(); |
| 33 |
$actual = preg_replace( '/ +/', ' ', $actual ); |
| 34 |
$actual = rtrim( $actual, " " ); |
| 35 |
$this->assertEquals( $expected, $actual ); |
| 36 |
} |
| 37 |
|
| 38 |
public function testGetSql() { |
| 39 |
$this->uqb |
| 40 |
->add( $this->db->newSelectQueryBuilder() |
| 41 |
->select( 'f' ) |
| 42 |
->from( 't1' ) |
| 43 |
) |
| 44 |
->add( $this->db->newSelectQueryBuilder() |
| 45 |
->select( 'f' ) |
| 46 |
->from( 't2' ) |
| 47 |
) |
| 48 |
->orderBy( 'f', UnionQueryBuilder::SORT_DESC ) |
| 49 |
->limit( 10 ) |
| 50 |
->offset( 20 ) |
| 51 |
->caller( __METHOD__ ); |
| 52 |
$this->assertSql( '(SELECT f FROM t1 ) UNION (SELECT f FROM t2 ) ' . |
| 53 |
'ORDER BY f DESC LIMIT 20,10' ); |
| 54 |
} |
| 55 |
|
| 56 |
public function testFetchResultSet() { |
| 57 |
$this->uqb |
| 58 |
->add( $this->db->newSelectQueryBuilder() |
| 59 |
->select( 'f' ) |
| 60 |
->from( 't1' ) |
| 61 |
) |
| 62 |
->add( $this->db->newSelectQueryBuilder() |
| 63 |
->select( 'f' ) |
| 64 |
->from( 't2' ) |
| 65 |
) |
| 66 |
->caller( __METHOD__ ); |
| 67 |
$res = $this->uqb->fetchResultSet(); |
| 68 |
$this->assertInstanceOf( IResultWrapper::class, $res ); |
| 69 |
$this->assertLastSql( '(SELECT f FROM t1 ) UNION (SELECT f FROM t2 )' ); |
| 70 |
} |
| 71 |
|
| 72 |
public function testFetchField() { |
| 73 |
$this->uqb |
| 74 |
->add( $this->db->newSelectQueryBuilder() |
| 75 |
->select( 'f' ) |
| 76 |
->from( 't1' ) |
| 77 |
) |
| 78 |
->add( $this->db->newSelectQueryBuilder() |
| 79 |
->select( 'f' ) |
| 80 |
->from( 't2' ) |
| 81 |
) |
| 82 |
->caller( __METHOD__ ); |
| 83 |
$f = $this->uqb->fetchField(); |
| 84 |
$this->assertFalse( $f ); |
| 85 |
$this->assertLastSql( '(SELECT f FROM t1 ) UNION (SELECT f FROM t2 ) LIMIT 1' ); |
| 86 |
} |
| 87 |
|
| 88 |
public function testFetchRow() { |
| 89 |
$this->uqb |
| 90 |
->add( $this->db->newSelectQueryBuilder() |
| 91 |
->select( 'f' ) |
| 92 |
->from( 't1' ) |
| 93 |
) |
| 94 |
->add( $this->db->newSelectQueryBuilder() |
| 95 |
->select( 'f' ) |
| 96 |
->from( 't2' ) |
| 97 |
) |
| 98 |
->caller( __METHOD__ ); |
| 99 |
$row = $this->uqb->fetchRow(); |
| 100 |
$this->assertFalse( $row ); |
| 101 |
$this->assertLastSql( '(SELECT f FROM t1 ) UNION (SELECT f FROM t2 ) LIMIT 1' ); |
| 102 |
} |
| 103 |
} |