5 * @covers TitleArrayFromResult
7 class TitleArrayFromResultTest
extends PHPUnit\Framework\TestCase
{
9 use MediaWikiCoversValidator
;
11 private function getMockResultWrapper( $row = null, $numRows = 1 ) {
12 $resultWrapper = $this->getMockBuilder( Wikimedia\Rdbms\ResultWrapper
::class )
13 ->disableOriginalConstructor();
15 $resultWrapper = $resultWrapper->getMock();
16 $resultWrapper->expects( $this->atLeastOnce() )
18 ->will( $this->returnValue( $row ) );
19 $resultWrapper->expects( $this->any() )
21 ->will( $this->returnValue( $numRows ) );
23 return $resultWrapper;
26 private function getRowWithTitle( $namespace = 3, $title = 'foo' ) {
27 $row = new stdClass();
28 $row->page_namespace
= $namespace;
29 $row->page_title
= $title;
34 * @covers TitleArrayFromResult::__construct
36 public function testConstructionWithFalseRow() {
38 $resultWrapper = $this->getMockResultWrapper( $row );
40 $object = new TitleArrayFromResult( $resultWrapper );
42 $this->assertEquals( $resultWrapper, $object->res
);
43 $this->assertSame( 0, $object->key
);
44 $this->assertEquals( $row, $object->current
);
48 * @covers TitleArrayFromResult::__construct
50 public function testConstructionWithRow() {
53 $row = $this->getRowWithTitle( $namespace, $title );
54 $resultWrapper = $this->getMockResultWrapper( $row );
56 $object = new TitleArrayFromResult( $resultWrapper );
58 $this->assertEquals( $resultWrapper, $object->res
);
59 $this->assertSame( 0, $object->key
);
60 $this->assertInstanceOf( Title
::class, $object->current
);
61 $this->assertEquals( $namespace, $object->current
->mNamespace
);
62 $this->assertEquals( $title, $object->current
->mTextform
);
65 public static function provideNumberOfRows() {
74 * @dataProvider provideNumberOfRows
75 * @covers TitleArrayFromResult::count
77 public function testCountWithVaryingValues( $numRows ) {
78 $object = new TitleArrayFromResult( $this->getMockResultWrapper(
79 $this->getRowWithTitle(),
82 $this->assertEquals( $numRows, $object->count() );
86 * @covers TitleArrayFromResult::current
88 public function testCurrentAfterConstruction() {
91 $row = $this->getRowWithTitle( $namespace, $title );
92 $object = new TitleArrayFromResult( $this->getMockResultWrapper( $row ) );
93 $this->assertInstanceOf( Title
::class, $object->current() );
94 $this->assertEquals( $namespace, $object->current
->mNamespace
);
95 $this->assertEquals( $title, $object->current
->mTextform
);
98 public function provideTestValid() {
100 [ $this->getRowWithTitle(), true ],
106 * @dataProvider provideTestValid
107 * @covers TitleArrayFromResult::valid
109 public function testValid( $input, $expected ) {
110 $object = new TitleArrayFromResult( $this->getMockResultWrapper( $input ) );
111 $this->assertEquals( $expected, $object->valid() );
114 // @todo unit test for key()
115 // @todo unit test for next()
116 // @todo unit test for rewind()