Merge "Exclude redirects from Special:Fewestrevisions"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / rdbms / resultwrapper / ResultWrapperTest.php
1 <?php
2
3 /**
4 * Holds tests for ResultWrapper MediaWiki class.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 */
23
24 use Wikimedia\Rdbms\IDatabase;
25 use Wikimedia\Rdbms\ResultWrapper;
26
27 /**
28 * @group Database
29 * @covers \Wikimedia\Rdbms\ResultWrapper
30 */
31 class ResultWrapperTest extends PHPUnit\Framework\TestCase {
32 /**
33 * @return IDatabase
34 * @param array[] $rows
35 */
36 private function getDatabaseMock( array $rows ) {
37 $db = $this->getMockBuilder( IDatabase::class )
38 ->disableOriginalConstructor()
39 ->getMock();
40 $db->method( 'select' )->willReturnCallback(
41 function () use ( $db, $rows ) {
42 return new ResultWrapper( $db, $rows );
43 }
44 );
45 $db->method( 'dataSeek' )->willReturnCallback(
46 function ( ResultWrapper $res, $pos ) use ( $db ) {
47 // Position already set in ResultWrapper
48 }
49 );
50 $db->method( 'fetchRow' )->willReturnCallback(
51 function ( ResultWrapper $res ) use ( $db ) {
52 $row = $res::unwrap( $res )[$res->key()] ?? false;
53
54 return $row;
55 }
56 );
57 $db->method( 'fetchObject' )->willReturnCallback(
58 function ( ResultWrapper $res ) use ( $db ) {
59 $row = $res::unwrap( $res )[$res->key()] ?? false;
60
61 return $row ? (object)$row : false;
62 }
63 );
64 $db->method( 'numRows' )->willReturnCallback(
65 function ( ResultWrapper $res ) use ( $db ) {
66 return count( $res::unwrap( $res ) );
67 }
68 );
69
70 return $db;
71 }
72
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' ]
83 ] );
84
85 $expectedRows = [
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' ]
94 ];
95
96 $res = $db->select( 'faketable', [ 'colA', 'colB' ], '1 = 1', __METHOD__ );
97 $this->assertEquals( 8, $res->numRows() );
98
99 $res->seek( 7 );
100 $this->assertEquals( [ 'colA' => 8, 'colB' => 'h' ], $res->fetchRow() );
101 $res->seek( 7 );
102 $this->assertEquals( (object)[ 'colA' => 8, 'colB' => 'h' ], $res->fetchObject() );
103
104 $this->assertEquals( $expectedRows, iterator_to_array( $res, true ) );
105
106 $rows = [];
107 foreach ( $res as $i => $row ) {
108 $rows[$i] = $row;
109 }
110 $this->assertEquals( $expectedRows, $rows );
111 }
112 }