This changeset resumes work on T89432 and related tickets by porting an initial set of tests to the new unit test suite separated out in I69b92db3e70093570e05cc0a64c7780a278b321a. The tests were only ported if they worked immediately without requiring any changes other than changing the test case class to MediaWikiUnitTestCase and moving the test to the new suite. If a test failed for any reason (even trivial misconfiguration), it was NOT ported. With this change, the unit tests suite now consits of a total of 455 tests. As before, you can run these tests via the following command: $ composer phpunit:unit Bug: T84948 Bug: T89432 Bug: T87781 Change-Id: Ibb8175981092d7f41864e641cc3c118af70a5c76
110 lines
2.9 KiB
PHP
110 lines
2.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Addshore
|
|
* @covers UserArrayFromResult
|
|
*/
|
|
class UserArrayFromResultTest extends \MediaWikiUnitTestCase {
|
|
|
|
private function getMockResultWrapper( $row = null, $numRows = 1 ) {
|
|
$resultWrapper = $this->getMockBuilder( Wikimedia\Rdbms\ResultWrapper::class )
|
|
->disableOriginalConstructor();
|
|
|
|
$resultWrapper = $resultWrapper->getMock();
|
|
$resultWrapper->expects( $this->atLeastOnce() )
|
|
->method( 'current' )
|
|
->will( $this->returnValue( $row ) );
|
|
$resultWrapper->expects( $this->any() )
|
|
->method( 'numRows' )
|
|
->will( $this->returnValue( $numRows ) );
|
|
|
|
return $resultWrapper;
|
|
}
|
|
|
|
private function getRowWithUsername( $username = 'fooUser' ) {
|
|
$row = new stdClass();
|
|
$row->user_name = $username;
|
|
return $row;
|
|
}
|
|
|
|
/**
|
|
* @covers UserArrayFromResult::__construct
|
|
*/
|
|
public function testConstructionWithFalseRow() {
|
|
$row = false;
|
|
$resultWrapper = $this->getMockResultWrapper( $row );
|
|
|
|
$object = new UserArrayFromResult( $resultWrapper );
|
|
|
|
$this->assertEquals( $resultWrapper, $object->res );
|
|
$this->assertSame( 0, $object->key );
|
|
$this->assertEquals( $row, $object->current );
|
|
}
|
|
|
|
/**
|
|
* @covers UserArrayFromResult::__construct
|
|
*/
|
|
public function testConstructionWithRow() {
|
|
$username = 'addshore';
|
|
$row = $this->getRowWithUsername( $username );
|
|
$resultWrapper = $this->getMockResultWrapper( $row );
|
|
|
|
$object = new UserArrayFromResult( $resultWrapper );
|
|
|
|
$this->assertEquals( $resultWrapper, $object->res );
|
|
$this->assertSame( 0, $object->key );
|
|
$this->assertInstanceOf( User::class, $object->current );
|
|
$this->assertEquals( $username, $object->current->mName );
|
|
}
|
|
|
|
public static function provideNumberOfRows() {
|
|
return [
|
|
[ 0 ],
|
|
[ 1 ],
|
|
[ 122 ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideNumberOfRows
|
|
* @covers UserArrayFromResult::count
|
|
*/
|
|
public function testCountWithVaryingValues( $numRows ) {
|
|
$object = new UserArrayFromResult( $this->getMockResultWrapper(
|
|
$this->getRowWithUsername(),
|
|
$numRows
|
|
) );
|
|
$this->assertEquals( $numRows, $object->count() );
|
|
}
|
|
|
|
/**
|
|
* @covers UserArrayFromResult::current
|
|
*/
|
|
public function testCurrentAfterConstruction() {
|
|
$username = 'addshore';
|
|
$userRow = $this->getRowWithUsername( $username );
|
|
$object = new UserArrayFromResult( $this->getMockResultWrapper( $userRow ) );
|
|
$this->assertInstanceOf( User::class, $object->current() );
|
|
$this->assertEquals( $username, $object->current()->mName );
|
|
}
|
|
|
|
public function provideTestValid() {
|
|
return [
|
|
[ $this->getRowWithUsername(), true ],
|
|
[ false, false ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideTestValid
|
|
* @covers UserArrayFromResult::valid
|
|
*/
|
|
public function testValid( $input, $expected ) {
|
|
$object = new UserArrayFromResult( $this->getMockResultWrapper( $input ) );
|
|
$this->assertEquals( $expected, $object->valid() );
|
|
}
|
|
|
|
// @todo unit test for key()
|
|
// @todo unit test for next()
|
|
// @todo unit test for rewind()
|
|
}
|