MediaWiki  REL1_24
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 static 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(
00078             $this->getRowWithUsername(),
00079             $numRows
00080         ) );
00081         $this->assertEquals( $numRows, $object->count() );
00082     }
00083 
00087     public function testCurrentAfterConstruction() {
00088         $username = 'addshore';
00089         $userRow = $this->getRowWithUsername( $username );
00090         $object = $this->getUserArrayFromResult( $this->getMockResultWrapper( $userRow ) );
00091         $this->assertInstanceOf( 'User', $object->current() );
00092         $this->assertEquals( $username, $object->current()->mName );
00093     }
00094 
00095     public function provideTestValid() {
00096         return array(
00097             array( $this->getRowWithUsername(), true ),
00098             array( false, false ),
00099         );
00100     }
00101 
00106     public function testValid( $input, $expected ) {
00107         $object = $this->getUserArrayFromResult( $this->getMockResultWrapper( $input ) );
00108         $this->assertEquals( $expected, $object->valid() );
00109     }
00110 
00111     //@todo unit test for key()
00112     //@todo unit test for next()
00113     //@todo unit test for rewind()
00114 }