Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / user / UserArrayFromResultTest.php
blobcf980b127849b64737dbae8ecd2b2d87e5d813d3
1 <?php
3 /**
4 * @author Addshore
5 * @covers UserArrayFromResult
6 */
7 class UserArrayFromResultTest extends MediaWikiTestCase {
9 private function getMockResultWrapper( $row = null, $numRows = 1 ) {
10 $resultWrapper = $this->getMockBuilder( 'ResultWrapper' )
11 ->disableOriginalConstructor();
13 $resultWrapper = $resultWrapper->getMock();
14 $resultWrapper->expects( $this->atLeastOnce() )
15 ->method( 'current' )
16 ->will( $this->returnValue( $row ) );
17 $resultWrapper->expects( $this->any() )
18 ->method( 'numRows' )
19 ->will( $this->returnValue( $numRows ) );
21 return $resultWrapper;
24 private function getRowWithUsername( $username = 'fooUser' ) {
25 $row = new stdClass();
26 $row->user_name = $username;
27 return $row;
30 private function getUserArrayFromResult( $resultWrapper ) {
31 return new UserArrayFromResult( $resultWrapper );
34 /**
35 * @covers UserArrayFromResult::__construct
37 public function testConstructionWithFalseRow() {
38 $row = false;
39 $resultWrapper = $this->getMockResultWrapper( $row );
41 $object = $this->getUserArrayFromResult( $resultWrapper );
43 $this->assertEquals( $resultWrapper, $object->res );
44 $this->assertSame( 0, $object->key );
45 $this->assertEquals( $row, $object->current );
48 /**
49 * @covers UserArrayFromResult::__construct
51 public function testConstructionWithRow() {
52 $username = 'addshore';
53 $row = $this->getRowWithUsername( $username );
54 $resultWrapper = $this->getMockResultWrapper( $row );
56 $object = $this->getUserArrayFromResult( $resultWrapper );
58 $this->assertEquals( $resultWrapper, $object->res );
59 $this->assertSame( 0, $object->key );
60 $this->assertInstanceOf( 'User', $object->current );
61 $this->assertEquals( $username, $object->current->mName );
64 public static function provideNumberOfRows() {
65 return [
66 [ 0 ],
67 [ 1 ],
68 [ 122 ],
72 /**
73 * @dataProvider provideNumberOfRows
74 * @covers UserArrayFromResult::count
76 public function testCountWithVaryingValues( $numRows ) {
77 $object = $this->getUserArrayFromResult( $this->getMockResultWrapper(
78 $this->getRowWithUsername(),
79 $numRows
80 ) );
81 $this->assertEquals( $numRows, $object->count() );
84 /**
85 * @covers UserArrayFromResult::current
87 public function testCurrentAfterConstruction() {
88 $username = 'addshore';
89 $userRow = $this->getRowWithUsername( $username );
90 $object = $this->getUserArrayFromResult( $this->getMockResultWrapper( $userRow ) );
91 $this->assertInstanceOf( 'User', $object->current() );
92 $this->assertEquals( $username, $object->current()->mName );
95 public function provideTestValid() {
96 return [
97 [ $this->getRowWithUsername(), true ],
98 [ false, false ],
103 * @dataProvider provideTestValid
104 * @covers UserArrayFromResult::valid
106 public function testValid( $input, $expected ) {
107 $object = $this->getUserArrayFromResult( $this->getMockResultWrapper( $input ) );
108 $this->assertEquals( $expected, $object->valid() );
111 // @todo unit test for key()
112 // @todo unit test for next()
113 // @todo unit test for rewind()