| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\User\User; |
| 4 |
use MediaWiki\User\UserArrayFromResult; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class UserArrayFromResultTest extends \MediaWikiUnitTestCase { |
| 11 |
|
| 12 |
private function getMockResultWrapper( $row = null, $numRows = 1 ) { |
| 13 |
$resultWrapper = $this->createMock( Wikimedia\Rdbms\IResultWrapper::class ); |
| 14 |
$resultWrapper->expects( $this->atLeastOnce() ) |
| 15 |
->method( 'current' ) |
| 16 |
->willReturn( $row ); |
| 17 |
$resultWrapper->method( 'numRows' ) |
| 18 |
->willReturn( $numRows ); |
| 19 |
$resultWrapper->method( 'fetchObject' ) |
| 20 |
->willReturn( $row ); |
| 21 |
|
| 22 |
return $resultWrapper; |
| 23 |
} |
| 24 |
|
| 25 |
private function getRowWithUsername( $username = 'fooUser' ) { |
| 26 |
return (object)[ 'user_name' => $username ]; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function testConstructionWithFalseRow() { |
| 33 |
$row = false; |
| 34 |
$resultWrapper = $this->getMockResultWrapper( $row ); |
| 35 |
|
| 36 |
$object = new UserArrayFromResult( $resultWrapper ); |
| 37 |
|
| 38 |
$this->assertEquals( $resultWrapper, $object->res ); |
| 39 |
$this->assertSame( 0, $object->key ); |
| 40 |
$this->assertEquals( $row, $object->current ); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function testConstructionWithRow() { |
| 47 |
$username = 'addshore'; |
| 48 |
$row = $this->getRowWithUsername( $username ); |
| 49 |
$resultWrapper = $this->getMockResultWrapper( $row ); |
| 50 |
|
| 51 |
$object = new UserArrayFromResult( $resultWrapper ); |
| 52 |
|
| 53 |
$this->assertEquals( $resultWrapper, $object->res ); |
| 54 |
$this->assertSame( 0, $object->key ); |
| 55 |
$this->assertInstanceOf( User::class, $object->current ); |
| 56 |
$this->assertEquals( $username, $object->current->mName ); |
| 57 |
} |
| 58 |
|
| 59 |
public static function provideNumberOfRows() { |
| 60 |
return [ |
| 61 |
[ 0 ], |
| 62 |
[ 1 ], |
| 63 |
[ 122 ], |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function testCountWithVaryingValues( $numRows ) { |
| 72 |
$object = new UserArrayFromResult( $this->getMockResultWrapper( |
| 73 |
$this->getRowWithUsername(), |
| 74 |
$numRows |
| 75 |
) ); |
| 76 |
$this->assertEquals( $numRows, $object->count() ); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function testCurrentAfterConstruction() { |
| 83 |
$username = 'addshore'; |
| 84 |
$userRow = $this->getRowWithUsername( $username ); |
| 85 |
$object = new UserArrayFromResult( $this->getMockResultWrapper( $userRow ) ); |
| 86 |
$this->assertInstanceOf( User::class, $object->current() ); |
| 87 |
$this->assertEquals( $username, $object->current()->mName ); |
| 88 |
} |
| 89 |
|
| 90 |
public function provideTestValid() { |
| 91 |
return [ |
| 92 |
[ $this->getRowWithUsername(), true ], |
| 93 |
[ false, false ], |
| 94 |
]; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function testValid( $input, $expected ) { |
| 102 |
$object = new UserArrayFromResult( $this->getMockResultWrapper( $input ) ); |
| 103 |
$this->assertEquals( $expected, $object->valid() ); |
| 104 |
} |
| 105 |
|
| 106 |
public function provideTestKey() { |
| 107 |
return [ |
| 108 |
[ $this->getRowWithUsername(), 0 ], |
| 109 |
[ $this->getRowWithUsername( 'xSavitar' ), 0 ], |
| 110 |
[ (object)[], 0 ], |
| 111 |
[ false, false ], |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function testKey( $input, $expected ) { |
| 120 |
$object = new UserArrayFromResult( $this->getMockResultWrapper( $input ) ); |
| 121 |
$this->assertEquals( $expected, $object->key() ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function testNextOnce() { |
| 128 |
$object = new UserArrayFromResult( |
| 129 |
$this->getMockResultWrapper( $this->getRowWithUsername() ) |
| 130 |
); |
| 131 |
$object->next(); |
| 132 |
$this->assertSame( 1, $object->key() ); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function testNextTwice() { |
| 140 |
$object = new UserArrayFromResult( |
| 141 |
$this->getMockResultWrapper( $this->getRowWithUsername() ) |
| 142 |
); |
| 143 |
$object->next(); |
| 144 |
$object->next(); |
| 145 |
$this->assertSame( 2, $object->key() ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function testRewind() { |
| 154 |
$object = new UserArrayFromResult( |
| 155 |
$this->getMockResultWrapper( $this->getRowWithUsername() ) |
| 156 |
); |
| 157 |
|
| 158 |
$object->next(); |
| 159 |
$this->assertSame( 1, $object->key() ); |
| 160 |
|
| 161 |
$object->next(); |
| 162 |
$this->assertSame( 2, $object->key() ); |
| 163 |
|
| 164 |
$object->rewind(); |
| 165 |
$this->assertSame( 0, $object->key() ); |
| 166 |
|
| 167 |
$object->rewind(); |
| 168 |
$this->assertSame( 0, $object->key() ); |
| 169 |
} |
| 170 |
} |