| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\Rdbms; |
| 4 |
|
| 5 |
use DatabaseTestHelper; |
| 6 |
use MediaWikiCoversValidator; |
| 7 |
use PHPUnit\Framework\TestCase; |
| 8 |
use Wikimedia\Rdbms\DeleteQueryBuilder; |
| 9 |
use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class DeleteQueryBuilderTest extends TestCase { |
| 15 |
use MediaWikiCoversValidator; |
| 16 |
|
| 17 |
|
| 18 |
private $db; |
| 19 |
|
| 20 |
|
| 21 |
private $dqb; |
| 22 |
|
| 23 |
protected function setUp(): void { |
| 24 |
$this->db = new DatabaseTestHelper( __CLASS__ . '::' . $this->getName() ); |
| 25 |
$this->dqb = $this->db->newDeleteQueryBuilder(); |
| 26 |
} |
| 27 |
|
| 28 |
private function assertSQL( $expected, $fname ) { |
| 29 |
$this->dqb->caller( $fname )->execute(); |
| 30 |
$actual = $this->db->getLastSqls(); |
| 31 |
$actual = preg_replace( '/ +/', ' ', $actual ); |
| 32 |
$actual = rtrim( $actual, " " ); |
| 33 |
$this->assertEquals( $expected, $actual ); |
| 34 |
} |
| 35 |
|
| 36 |
public function testCondsEtc() { |
| 37 |
$this->dqb |
| 38 |
->table( 'a' ) |
| 39 |
->where( '1' ) |
| 40 |
->andWhere( '2' ) |
| 41 |
->conds( '3' ); |
| 42 |
$this->assertSQL( 'DELETE FROM a WHERE (1) AND (2) AND (3)', __METHOD__ ); |
| 43 |
} |
| 44 |
|
| 45 |
public function testConflictingConds() { |
| 46 |
$this->dqb |
| 47 |
->deleteFrom( '1' ) |
| 48 |
->where( [ 'k' => 'v1' ] ) |
| 49 |
->andWhere( [ 'k' => 'v2' ] ); |
| 50 |
$this->assertSQL( 'DELETE FROM 1 WHERE k = \'v1\' AND (k = \'v2\')', __METHOD__ ); |
| 51 |
} |
| 52 |
|
| 53 |
public function testCondsAllRows() { |
| 54 |
$this->dqb |
| 55 |
->table( 'a' ) |
| 56 |
->where( ISQLPlatform::ALL_ROWS ); |
| 57 |
$this->assertSQL( 'DELETE FROM a', __METHOD__ ); |
| 58 |
} |
| 59 |
|
| 60 |
public function testExecute() { |
| 61 |
$this->dqb->deleteFrom( 't' )->where( 'c' )->caller( __METHOD__ ); |
| 62 |
$this->dqb->execute(); |
| 63 |
$this->assertEquals( 'DELETE FROM t WHERE (c)', $this->db->getLastSqls() ); |
| 64 |
} |
| 65 |
|
| 66 |
public function testGetQueryInfo() { |
| 67 |
$this->dqb |
| 68 |
->deleteFrom( 't' ) |
| 69 |
->where( [ 'a' => 'b' ] ) |
| 70 |
->caller( 'foo' ); |
| 71 |
$this->assertEquals( |
| 72 |
[ |
| 73 |
'table' => 't', |
| 74 |
'conds' => [ 'a' => 'b' ], |
| 75 |
'caller' => 'foo', |
| 76 |
], |
| 77 |
$this->dqb->getQueryInfo() ); |
| 78 |
} |
| 79 |
|
| 80 |
public function testQueryInfo() { |
| 81 |
$this->dqb->queryInfo( |
| 82 |
[ |
| 83 |
'table' => 't', |
| 84 |
'conds' => [ 'a' => 'b' ], |
| 85 |
] |
| 86 |
); |
| 87 |
$this->assertSQL( "DELETE FROM t WHERE a = 'b'", __METHOD__ ); |
| 88 |
} |
| 89 |
} |