5 * @covers TitleArrayFromResult
7 class TitleArrayFromResultTest
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 getRowWithTitle( $namespace = 3, $title = 'foo' ) {
25 $row = new stdClass();
26 $row->page_namespace
= $namespace;
27 $row->page_title
= $title;
32 * @covers TitleArrayFromResult::__construct
34 public function testConstructionWithFalseRow() {
36 $resultWrapper = $this->getMockResultWrapper( $row );
38 $object = new TitleArrayFromResult( $resultWrapper );
40 $this->assertEquals( $resultWrapper, $object->res
);
41 $this->assertSame( 0, $object->key
);
42 $this->assertEquals( $row, $object->current
);
46 * @covers TitleArrayFromResult::__construct
48 public function testConstructionWithRow() {
51 $row = $this->getRowWithTitle( $namespace, $title );
52 $resultWrapper = $this->getMockResultWrapper( $row );
54 $object = new TitleArrayFromResult( $resultWrapper );
56 $this->assertEquals( $resultWrapper, $object->res
);
57 $this->assertSame( 0, $object->key
);
58 $this->assertInstanceOf( Title
::class, $object->current
);
59 $this->assertEquals( $namespace, $object->current
->mNamespace
);
60 $this->assertEquals( $title, $object->current
->mTextform
);
63 public static function provideNumberOfRows() {
72 * @dataProvider provideNumberOfRows
73 * @covers TitleArrayFromResult::count
75 public function testCountWithVaryingValues( $numRows ) {
76 $object = new TitleArrayFromResult( $this->getMockResultWrapper(
77 $this->getRowWithTitle(),
80 $this->assertEquals( $numRows, $object->count() );
84 * @covers TitleArrayFromResult::current
86 public function testCurrentAfterConstruction() {
89 $row = $this->getRowWithTitle( $namespace, $title );
90 $object = new TitleArrayFromResult( $this->getMockResultWrapper( $row ) );
91 $this->assertInstanceOf( Title
::class, $object->current() );
92 $this->assertEquals( $namespace, $object->current
->mNamespace
);
93 $this->assertEquals( $title, $object->current
->mTextform
);
96 public function provideTestValid() {
98 [ $this->getRowWithTitle(), true ],
104 * @dataProvider provideTestValid
105 * @covers TitleArrayFromResult::valid
107 public function testValid( $input, $expected ) {
108 $object = new TitleArrayFromResult( $this->getMockResultWrapper( $input ) );
109 $this->assertEquals( $expected, $object->valid() );
112 // @todo unit test for key()
113 // @todo unit test for next()
114 // @todo unit test for rewind()