2014-01-26 17:46:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2016-01-27 09:59:31 +00:00
|
|
|
* @author Addshore
|
2014-01-26 17:46:20 +00:00
|
|
|
* @covers TitleArrayFromResult
|
|
|
|
|
*/
|
2019-07-13 20:50:28 +00:00
|
|
|
class TitleArrayFromResultTest extends MediaWikiUnitTestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
2014-01-26 17:46:20 +00:00
|
|
|
private function getMockResultWrapper( $row = null, $numRows = 1 ) {
|
2019-10-04 17:58:19 +00:00
|
|
|
$resultWrapper = $this->getMockBuilder( Wikimedia\Rdbms\IResultWrapper::class )
|
2014-01-26 17:46:20 +00:00
|
|
|
->disableOriginalConstructor();
|
|
|
|
|
|
|
|
|
|
$resultWrapper = $resultWrapper->getMock();
|
|
|
|
|
$resultWrapper->expects( $this->atLeastOnce() )
|
|
|
|
|
->method( 'current' )
|
2021-04-22 08:28:11 +00:00
|
|
|
->willReturn( $row );
|
|
|
|
|
$resultWrapper->method( 'numRows' )
|
|
|
|
|
->willReturn( $numRows );
|
2014-01-26 17:46:20 +00:00
|
|
|
|
|
|
|
|
return $resultWrapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getRowWithTitle( $namespace = 3, $title = 'foo' ) {
|
2020-02-28 15:13:53 +00:00
|
|
|
return (object)[
|
|
|
|
|
'page_namespace' => $namespace,
|
|
|
|
|
'page_title' => $title,
|
|
|
|
|
];
|
2014-01-26 17:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers TitleArrayFromResult::__construct
|
|
|
|
|
*/
|
|
|
|
|
public function testConstructionWithFalseRow() {
|
|
|
|
|
$row = false;
|
|
|
|
|
$resultWrapper = $this->getMockResultWrapper( $row );
|
|
|
|
|
|
2019-05-28 14:11:30 +00:00
|
|
|
$object = new TitleArrayFromResult( $resultWrapper );
|
2014-01-26 17:46:20 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( $resultWrapper, $object->res );
|
|
|
|
|
$this->assertSame( 0, $object->key );
|
|
|
|
|
$this->assertEquals( $row, $object->current );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers TitleArrayFromResult::__construct
|
|
|
|
|
*/
|
|
|
|
|
public function testConstructionWithRow() {
|
|
|
|
|
$namespace = 0;
|
|
|
|
|
$title = 'foo';
|
|
|
|
|
$row = $this->getRowWithTitle( $namespace, $title );
|
|
|
|
|
$resultWrapper = $this->getMockResultWrapper( $row );
|
|
|
|
|
|
2019-05-28 14:11:30 +00:00
|
|
|
$object = new TitleArrayFromResult( $resultWrapper );
|
2014-01-26 17:46:20 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( $resultWrapper, $object->res );
|
|
|
|
|
$this->assertSame( 0, $object->key );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( Title::class, $object->current );
|
2014-01-26 17:46:20 +00:00
|
|
|
$this->assertEquals( $namespace, $object->current->mNamespace );
|
|
|
|
|
$this->assertEquals( $title, $object->current->mTextform );
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 01:28:26 +00:00
|
|
|
public static function provideNumberOfRows() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 0 ],
|
|
|
|
|
[ 1 ],
|
|
|
|
|
[ 122 ],
|
|
|
|
|
];
|
2014-01-26 17:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideNumberOfRows
|
|
|
|
|
* @covers TitleArrayFromResult::count
|
|
|
|
|
*/
|
|
|
|
|
public function testCountWithVaryingValues( $numRows ) {
|
2019-05-28 14:11:30 +00:00
|
|
|
$object = new TitleArrayFromResult( $this->getMockResultWrapper(
|
2014-04-24 12:50:36 +00:00
|
|
|
$this->getRowWithTitle(),
|
|
|
|
|
$numRows
|
|
|
|
|
) );
|
2014-01-26 17:46:20 +00:00
|
|
|
$this->assertEquals( $numRows, $object->count() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers TitleArrayFromResult::current
|
|
|
|
|
*/
|
|
|
|
|
public function testCurrentAfterConstruction() {
|
|
|
|
|
$namespace = 0;
|
|
|
|
|
$title = 'foo';
|
|
|
|
|
$row = $this->getRowWithTitle( $namespace, $title );
|
2019-05-28 14:11:30 +00:00
|
|
|
$object = new TitleArrayFromResult( $this->getMockResultWrapper( $row ) );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( Title::class, $object->current() );
|
2014-01-26 17:46:20 +00:00
|
|
|
$this->assertEquals( $namespace, $object->current->mNamespace );
|
|
|
|
|
$this->assertEquals( $title, $object->current->mTextform );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideTestValid() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ $this->getRowWithTitle(), true ],
|
|
|
|
|
[ false, false ],
|
|
|
|
|
];
|
2014-01-26 17:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTestValid
|
|
|
|
|
* @covers TitleArrayFromResult::valid
|
|
|
|
|
*/
|
|
|
|
|
public function testValid( $input, $expected ) {
|
2019-05-28 14:11:30 +00:00
|
|
|
$object = new TitleArrayFromResult( $this->getMockResultWrapper( $input ) );
|
2014-01-26 17:46:20 +00:00
|
|
|
$this->assertEquals( $expected, $object->valid() );
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-11 13:44:59 +00:00
|
|
|
// @todo unit test for key()
|
|
|
|
|
// @todo unit test for next()
|
|
|
|
|
// @todo unit test for rewind()
|
2014-03-20 18:59:20 +00:00
|
|
|
}
|