MediaWiki  REL1_23
UserArrayFromResultTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class UserArrayFromResultTest extends MediaWikiTestCase {
00008 
00009     private function getMockResultWrapper( $row = null, $numRows = 1 ) {
00010         $resultWrapper = $this->getMockBuilder( 'ResultWrapper' )
00011             ->disableOriginalConstructor();
00012 
00013         $resultWrapper = $resultWrapper->getMock();
00014         $resultWrapper->expects( $this->atLeastOnce() )
00015             ->method( 'current' )
00016             ->will( $this->returnValue( $row ) );
00017         $resultWrapper->expects( $this->any() )
00018             ->method( 'numRows' )
00019             ->will( $this->returnValue( $numRows ) );
00020 
00021         return $resultWrapper;
00022     }
00023 
00024     private function getRowWithUsername( $username = 'fooUser' ) {
00025         $row = new stdClass();
00026         $row->user_name = $username;
00027         return $row;
00028     }
00029 
00030     private function getUserArrayFromResult( $resultWrapper ) {
00031         return new UserArrayFromResult( $resultWrapper );
00032     }
00033 
00037     public function testConstructionWithFalseRow() {
00038         $row = false;
00039         $resultWrapper = $this->getMockResultWrapper( $row );
00040 
00041         $object = $this->getUserArrayFromResult( $resultWrapper );
00042 
00043         $this->assertEquals( $resultWrapper, $object->res );
00044         $this->assertSame( 0, $object->key );
00045         $this->assertEquals( $row, $object->current );
00046     }
00047 
00051     public function testConstructionWithRow() {
00052         $username = 'addshore';
00053         $row = $this->getRowWithUsername( $username );
00054         $resultWrapper = $this->getMockResultWrapper( $row );
00055 
00056         $object = $this->getUserArrayFromResult( $resultWrapper );
00057 
00058         $this->assertEquals( $resultWrapper, $object->res );
00059         $this->assertSame( 0, $object->key );
00060         $this->assertInstanceOf( 'User', $object->current );
00061         $this->assertEquals( $username, $object->current->mName );
00062     }
00063 
00064     public function provideNumberOfRows() {
00065         return array(
00066             array( 0 ),
00067             array( 1 ),
00068             array( 122 ),
00069         );
00070     }
00071 
00076     public function testCountWithVaryingValues( $numRows ) {
00077         $object = $this->getUserArrayFromResult( $this->getMockResultWrapper( $this->getRowWithUsername(), $numRows ) );
00078         $this->assertEquals( $numRows, $object->count() );
00079     }
00080 
00084     public function testCurrentAfterConstruction() {
00085         $username = 'addshore';
00086         $userRow = $this->getRowWithUsername( $username );
00087         $object = $this->getUserArrayFromResult( $this->getMockResultWrapper( $userRow ) );
00088         $this->assertInstanceOf( 'User', $object->current() );
00089         $this->assertEquals( $username, $object->current()->mName );
00090     }
00091 
00092     public function provideTestValid() {
00093         return array(
00094             array( $this->getRowWithUsername(), true ),
00095             array( false, false ),
00096         );
00097     }
00098 
00103     public function testValid( $input, $expected ) {
00104         $object = $this->getUserArrayFromResult( $this->getMockResultWrapper( $input ) );
00105         $this->assertEquals( $expected, $object->valid() );
00106     }
00107 
00108     //@todo unit test for key()
00109     //@todo unit test for next()
00110     //@todo unit test for rewind()
00111 }