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;
33 private function getTitleArrayFromResult( $resultWrapper ) {
34 return new TitleArrayFromResult( $resultWrapper );
38 * @covers TitleArrayFromResult::__construct
40 public function testConstructionWithFalseRow() {
42 $resultWrapper = $this->getMockResultWrapper( $row );
44 $object = $this->getTitleArrayFromResult( $resultWrapper );
46 $this->assertEquals( $resultWrapper, $object->res
);
47 $this->assertSame( 0, $object->key
);
48 $this->assertEquals( $row, $object->current
);
52 * @covers TitleArrayFromResult::__construct
54 public function testConstructionWithRow() {
57 $row = $this->getRowWithTitle( $namespace, $title );
58 $resultWrapper = $this->getMockResultWrapper( $row );
60 $object = $this->getTitleArrayFromResult( $resultWrapper );
62 $this->assertEquals( $resultWrapper, $object->res
);
63 $this->assertSame( 0, $object->key
);
64 $this->assertInstanceOf( Title
::class, $object->current
);
65 $this->assertEquals( $namespace, $object->current
->mNamespace
);
66 $this->assertEquals( $title, $object->current
->mTextform
);
69 public static function provideNumberOfRows() {
78 * @dataProvider provideNumberOfRows
79 * @covers TitleArrayFromResult::count
81 public function testCountWithVaryingValues( $numRows ) {
82 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper(
83 $this->getRowWithTitle(),
86 $this->assertEquals( $numRows, $object->count() );
90 * @covers TitleArrayFromResult::current
92 public function testCurrentAfterConstruction() {
95 $row = $this->getRowWithTitle( $namespace, $title );
96 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper( $row ) );
97 $this->assertInstanceOf( Title
::class, $object->current() );
98 $this->assertEquals( $namespace, $object->current
->mNamespace
);
99 $this->assertEquals( $title, $object->current
->mTextform
);
102 public function provideTestValid() {
104 [ $this->getRowWithTitle(), true ],
110 * @dataProvider provideTestValid
111 * @covers TitleArrayFromResult::valid
113 public function testValid( $input, $expected ) {
114 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper( $input ) );
115 $this->assertEquals( $expected, $object->valid() );
118 // @todo unit test for key()
119 // @todo unit test for next()
120 // @todo unit test for rewind()