Debian Codegraph @

tests/phpunit/integration/includes/libs/rdbms/resultwrapper/ResultWrapperTest.php

raw symbols SCIP
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\IMaintainableDatabase;
25
26 /**
27 * @group Database
28 * @covers \Wikimedia\Rdbms\ResultWrapper
29 * @covers \Wikimedia\Rdbms\MysqliResultWrapper
30 * @covers \Wikimedia\Rdbms\PostgresResultWrapper
31 * @covers \Wikimedia\Rdbms\SqliteResultWrapper
32 */
33 class ResultWrapperTest extends MediaWikiIntegrationTestCase {
34 public function getSchemaOverrides( IMaintainableDatabase $db ) {
35 return [
36 'create' => [ 'ResultWrapperTest' ],
37 'scripts' => [ __DIR__ . '/ResultWrapperTest.sql' ]
38 ];
39 }
40
41 public function testIteration() {
42 $this->getDb()->insert(
43 'ResultWrapperTest', [
44 [ 'col_a' => '1', 'col_b' => 'a' ],
45 [ 'col_a' => '2', 'col_b' => 'b' ],
46 [ 'col_a' => '3', 'col_b' => 'c' ],
47 [ 'col_a' => '4', 'col_b' => 'd' ],
48 [ 'col_a' => '5', 'col_b' => 'e' ],
49 [ 'col_a' => '6', 'col_b' => 'f' ],
50 [ 'col_a' => '7', 'col_b' => 'g' ],
51 [ 'col_a' => '8', 'col_b' => 'h' ]
52 ],
53 __METHOD__
54 );
55
56 $expectedRows = [
57 0 => (object)[ 'col_a' => '1', 'col_b' => 'a' ],
58 1 => (object)[ 'col_a' => '2', 'col_b' => 'b' ],
59 2 => (object)[ 'col_a' => '3', 'col_b' => 'c' ],
60 3 => (object)[ 'col_a' => '4', 'col_b' => 'd' ],
61 4 => (object)[ 'col_a' => '5', 'col_b' => 'e' ],
62 5 => (object)[ 'col_a' => '6', 'col_b' => 'f' ],
63 6 => (object)[ 'col_a' => '7', 'col_b' => 'g' ],
64 7 => (object)[ 'col_a' => '8', 'col_b' => 'h' ]
65 ];
66
67 $res = $this->getDb()->newSelectQueryBuilder()
68 ->select( [ 'col_a', 'col_b' ] )
69 ->from( 'ResultWrapperTest' )
70 ->where( '1 = 1' )
71 ->caller( __METHOD__ )->fetchResultSet();
72 $this->assertSame( 8, $res->numRows() );
73 $this->assertTrue( $res->valid() );
74
75 $res->seek( 7 );
76 $this->assertSame( 7, $res->key() );
77 $this->assertArrayEquals( [ 'col_a' => '8', 0 => '8', 'col_b' => 'h', 1 => 'h', ],
78 $res->fetchRow(), false, true );
79 $this->assertSame( 7, $res->key() );
80
81 $res->seek( 7 );
82 $this->assertSame( 7, $res->key() );
83 $this->assertEquals( (object)[ 'col_a' => '8', 'col_b' => 'h' ], $res->fetchObject() );
84 $this->assertEquals( (object)[ 'col_a' => '8', 'col_b' => 'h' ], $res->current() );
85 $this->assertSame( 7, $res->key() );
86
87 $res->seek( 6 );
88 $this->assertTrue( $res->valid() );
89 $this->assertSame( 6, $res->key() );
90 $this->assertEquals( (object)[ 'col_a' => '7', 'col_b' => 'g' ], $res->fetchObject() );
91 $this->assertTrue( $res->valid() );
92 $this->assertSame( 6, $res->key() );
93 $this->assertEquals( (object)[ 'col_a' => '8', 'col_b' => 'h' ], $res->fetchObject() );
94 $this->assertSame( 7, $res->key() );
95 $this->assertFalse( $res->fetchObject() );
96 $this->assertFalse( $res->current() );
97 $this->assertFalse( $res->valid() );
98
99 $this->assertArrayEquals( $expectedRows, iterator_to_array( $res, true ),
100 false, true );
101
102 $rows = [];
103 foreach ( $res as $i => $row ) {
104 $rows[$i] = $row;
105 }
106 $this->assertEquals( $expectedRows, $rows );
107 }
108
109 public function testCurrentNoResults() {
110 $res = $this->getDb()->newSelectQueryBuilder()
111 ->select( [ 'col_a', 'col_b' ] )
112 ->from( 'ResultWrapperTest' )
113 ->where( '1 = 0' )
114 ->caller( __METHOD__ )->fetchResultSet();
115 $this->assertFalse( $res->current() );
116 }
117
118 public function testValidNoResults() {
119 $res = $this->getDb()->newSelectQueryBuilder()
120 ->select( [ 'col_a', 'col_b' ] )
121 ->from( 'ResultWrapperTest' )
122 ->where( '1 = 0' )
123 ->caller( __METHOD__ )->fetchResultSet();
124 $this->assertFalse( $res->valid() );
125 }
126
127 public function testSeekNoResults() {
128 $res = $this->getDb()->newSelectQueryBuilder()
129 ->select( [ 'col_a', 'col_b' ] )
130 ->from( 'ResultWrapperTest' )
131 ->where( '1 = 0' )
132 ->caller( __METHOD__ )->fetchResultSet();
133 $res->seek( 0 );
134 $this->assertTrue( true ); // no error
135 }
136
137 public static function provideSeekOutOfBounds() {
138 return [ [ 0, 1 ], [ 1, 1 ], [ 1, 2 ], [ 1, -1 ] ];
139 }
140
141 /** @dataProvider provideSeekOutOfBounds */
142 public function testSeekOutOfBounds( $numRows, $seekPos ) {
143 for ( $i = 0; $i < $numRows; $i++ ) {
144 $this->getDb()->insert( 'ResultWrapperTest',
145 [ [ 'col_a' => $i, 'col_b' => $i ] ],
146 __METHOD__ );
147 }
148 $res = $this->getDb()->newSelectQueryBuilder()
149 ->select( [ 'col_a', 'col_b' ] )
150 ->from( 'ResultWrapperTest' )
151 ->where( '1 = 0' )
152 ->caller( __METHOD__ )->fetchResultSet();
153 $this->expectException( OutOfBoundsException::class );
154 $res->seek( $seekPos );
155 }
156 }