3 use PHPUnit\Framework\Assert
;
4 use Wikimedia\Rdbms\SelectQueryBuilder
;
6 class TestSelectQueryBuilder
extends SelectQueryBuilder
{
7 public function fields( $fields ) {
8 $this->orderBy( $fields );
9 return parent
::fields( $fields );
12 public function field( $field, $alias = null ) {
13 $this->orderBy( $field );
14 return parent
::field( $field, $alias );
18 * Asserts that the current database query yields the rows given by $expectedRows.
19 * The expected rows should be given as indexed (not associative) arrays, with
20 * the values given in the order of the columns in the $fields parameter.
21 * Note that the rows are sorted by the columns given in $fields.
23 * @param array $expectedRows
25 public function assertResultSet( $expectedRows ) {
26 $res = $this->fetchResultSet();
30 foreach ( $expectedRows as $expected ) {
31 $r = $res->fetchRow();
32 MediaWikiIntegrationTestCase
::stripStringKeys( $r );
35 Assert
::assertNotFalse( $r, "row #$i missing" );
37 Assert
::assertEquals( $expected, $r, "row #$i mismatches" );
40 $r = $res->fetchRow();
41 MediaWikiIntegrationTestCase
::stripStringKeys( $r );
43 Assert
::assertFalse( $r, "found extra row (after #$i)" );
46 public function assertEmptyResult() {
47 $res = $this->fetchResultSet();
48 Assert
::assertSame( 0, $res->numRows(), "Result set should be empty" );
52 * Execute the query, and assert that it returns a single row with a single
53 * field with the given value.
55 * Unlike fetchField(), LIMIT 1 is not automatically added.
57 * @param mixed $expectedValue
59 public function assertFieldValue( $expectedValue ) {
60 $res = $this->fetchResultSet();
61 Assert
::assertSame( 1, $res->numRows(),
62 "There should be one row in the result set" );
63 $row = (array)$res->fetchObject();
64 Assert
::assertCount( 1, $row,
65 "There should be one field in the result set" );
66 $value = reset( $row );
67 Assert
::assertEquals( $expectedValue, $value,
68 "The field value should match" );
72 * Execute the query, and assert that it returns the given values in $expectedValues.
74 * This method only can be used if you selected one field in the query.
77 * @param array $expectedValues
79 public function assertFieldValues( array $expectedValues ) {
80 Assert
::assertSame( $expectedValues, $this->fetchFieldValues() );
84 * Execute the query, and assert that it returns a single row with the given value.
86 * Unlike fetchRow(), LIMIT 1 is not automatically added.
89 * @param array $expectedRow
91 public function assertRowValue( array $expectedRow ) {
92 $res = $this->fetchResultSet();
93 Assert
::assertSame( 1, $res->numRows(), "There should be one row in the result set" );
94 $row = $res->fetchRow();
95 MediaWikiIntegrationTestCase
::stripStringKeys( $row );
96 Assert
::assertEquals( $expectedRow, $row, "The row should match" );