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