4 * Holds tests for ResultWrapper MediaWiki class.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
24 use Wikimedia\Rdbms\IDatabase
;
25 use Wikimedia\Rdbms\ResultWrapper
;
29 * @covers \Wikimedia\Rdbms\ResultWrapper
31 class ResultWrapperTest
extends PHPUnit\Framework\TestCase
{
34 * @param array[] $rows
36 private function getDatabaseMock( array $rows ) {
37 $db = $this->getMockBuilder( IDatabase
::class )
38 ->disableOriginalConstructor()
40 $db->method( 'select' )->willReturnCallback(
41 function () use ( $db, $rows ) {
42 return new ResultWrapper( $db, $rows );
45 $db->method( 'dataSeek' )->willReturnCallback(
46 function ( ResultWrapper
$res, $pos ) use ( $db ) {
47 // Position already set in ResultWrapper
50 $db->method( 'fetchRow' )->willReturnCallback(
51 function ( ResultWrapper
$res ) use ( $db ) {
52 $row = $res::unwrap( $res )[$res->key()] ??
false;
57 $db->method( 'fetchObject' )->willReturnCallback(
58 function ( ResultWrapper
$res ) use ( $db ) {
59 $row = $res::unwrap( $res )[$res->key()] ??
false;
61 return $row ?
(object)$row : false;
64 $db->method( 'numRows' )->willReturnCallback(
65 function ( ResultWrapper
$res ) use ( $db ) {
66 return count( $res::unwrap( $res ) );
73 public function testIteration() {
74 $db = $this->getDatabaseMock( [
75 [ 'colA' => 1, 'colB' => 'a' ],
76 [ 'colA' => 2, 'colB' => 'b' ],
77 [ 'colA' => 3, 'colB' => 'c' ],
78 [ 'colA' => 4, 'colB' => 'd' ],
79 [ 'colA' => 5, 'colB' => 'e' ],
80 [ 'colA' => 6, 'colB' => 'f' ],
81 [ 'colA' => 7, 'colB' => 'g' ],
82 [ 'colA' => 8, 'colB' => 'h' ]
86 0 => (object)[ 'colA' => 1, 'colB' => 'a' ],
87 1 => (object)[ 'colA' => 2, 'colB' => 'b' ],
88 2 => (object)[ 'colA' => 3, 'colB' => 'c' ],
89 3 => (object)[ 'colA' => 4, 'colB' => 'd' ],
90 4 => (object)[ 'colA' => 5, 'colB' => 'e' ],
91 5 => (object)[ 'colA' => 6, 'colB' => 'f' ],
92 6 => (object)[ 'colA' => 7, 'colB' => 'g' ],
93 7 => (object)[ 'colA' => 8, 'colB' => 'h' ]
96 $res = $db->select( 'faketable', [ 'colA', 'colB' ], '1 = 1', __METHOD__
);
97 $this->assertEquals( 8, $res->numRows() );
100 $this->assertEquals( [ 'colA' => 8, 'colB' => 'h' ], $res->fetchRow() );
102 $this->assertEquals( (object)[ 'colA' => 8, 'colB' => 'h' ], $res->fetchObject() );
104 $this->assertEquals( $expectedRows, iterator_to_array( $res, true ) );
107 foreach ( $res as $i => $row ) {
110 $this->assertEquals( $expectedRows, $rows );