5 * @covers UserArrayFromResult
7 class UserArrayFromResultTest
extends \MediaWikiUnitTestCase
{
9 private function getMockResultWrapper( $row = null, $numRows = 1 ) {
10 $resultWrapper = $this->getMockBuilder( Wikimedia\Rdbms\ResultWrapper
::class )
11 ->disableOriginalConstructor();
13 $resultWrapper = $resultWrapper->getMock();
14 $resultWrapper->expects( $this->atLeastOnce() )
16 ->will( $this->returnValue( $row ) );
17 $resultWrapper->expects( $this->any() )
19 ->will( $this->returnValue( $numRows ) );
21 return $resultWrapper;
24 private function getRowWithUsername( $username = 'fooUser' ) {
25 $row = new stdClass();
26 $row->user_name
= $username;
31 * @covers UserArrayFromResult::__construct
33 public function testConstructionWithFalseRow() {
35 $resultWrapper = $this->getMockResultWrapper( $row );
37 $object = new UserArrayFromResult( $resultWrapper );
39 $this->assertEquals( $resultWrapper, $object->res
);
40 $this->assertSame( 0, $object->key
);
41 $this->assertEquals( $row, $object->current
);
45 * @covers UserArrayFromResult::__construct
47 public function testConstructionWithRow() {
48 $username = 'addshore';
49 $row = $this->getRowWithUsername( $username );
50 $resultWrapper = $this->getMockResultWrapper( $row );
52 $object = new UserArrayFromResult( $resultWrapper );
54 $this->assertEquals( $resultWrapper, $object->res
);
55 $this->assertSame( 0, $object->key
);
56 $this->assertInstanceOf( User
::class, $object->current
);
57 $this->assertEquals( $username, $object->current
->mName
);
60 public static function provideNumberOfRows() {
69 * @dataProvider provideNumberOfRows
70 * @covers UserArrayFromResult::count
72 public function testCountWithVaryingValues( $numRows ) {
73 $object = new UserArrayFromResult( $this->getMockResultWrapper(
74 $this->getRowWithUsername(),
77 $this->assertEquals( $numRows, $object->count() );
81 * @covers UserArrayFromResult::current
83 public function testCurrentAfterConstruction() {
84 $username = 'addshore';
85 $userRow = $this->getRowWithUsername( $username );
86 $object = new UserArrayFromResult( $this->getMockResultWrapper( $userRow ) );
87 $this->assertInstanceOf( User
::class, $object->current() );
88 $this->assertEquals( $username, $object->current()->mName
);
91 public function provideTestValid() {
93 [ $this->getRowWithUsername(), true ],
99 * @dataProvider provideTestValid
100 * @covers UserArrayFromResult::valid
102 public function testValid( $input, $expected ) {
103 $object = new UserArrayFromResult( $this->getMockResultWrapper( $input ) );
104 $this->assertEquals( $expected, $object->valid() );
107 // @todo unit test for key()
108 // @todo unit test for next()
109 // @todo unit test for rewind()