| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Title\Title; |
| 4 |
use MediaWiki\Title\TitleArrayFromResult; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class TitleArrayFromResultTest extends MediaWikiUnitTestCase { |
| 11 |
|
| 12 |
private function getMockResultWrapper( $row = null, $numRows = 1 ) { |
| 13 |
$resultWrapper = $this->createMock( Wikimedia\Rdbms\IResultWrapper::class ); |
| 14 |
$resultWrapper->expects( $this->atLeastOnce() ) |
| 15 |
->method( 'current' ) |
| 16 |
->willReturn( $row ); |
| 17 |
$resultWrapper->method( 'numRows' ) |
| 18 |
->willReturn( $numRows ); |
| 19 |
|
| 20 |
return $resultWrapper; |
| 21 |
} |
| 22 |
|
| 23 |
private function getRowWithTitle( $namespace = 3, $title = 'foo' ) { |
| 24 |
return (object)[ |
| 25 |
'page_namespace' => $namespace, |
| 26 |
'page_title' => $title, |
| 27 |
]; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function testConstructionWithFalseRow() { |
| 34 |
$row = false; |
| 35 |
$resultWrapper = $this->getMockResultWrapper( $row ); |
| 36 |
|
| 37 |
$object = new TitleArrayFromResult( $resultWrapper ); |
| 38 |
|
| 39 |
$this->assertEquals( $resultWrapper, $object->res ); |
| 40 |
$this->assertSame( 0, $object->key ); |
| 41 |
$this->assertEquals( $row, $object->current ); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function testConstructionWithRow() { |
| 48 |
$namespace = 0; |
| 49 |
$title = 'foo'; |
| 50 |
$row = $this->getRowWithTitle( $namespace, $title ); |
| 51 |
$resultWrapper = $this->getMockResultWrapper( $row ); |
| 52 |
|
| 53 |
$object = new TitleArrayFromResult( $resultWrapper ); |
| 54 |
|
| 55 |
$this->assertEquals( $resultWrapper, $object->res ); |
| 56 |
$this->assertSame( 0, $object->key ); |
| 57 |
$this->assertInstanceOf( Title::class, $object->current ); |
| 58 |
$this->assertEquals( $namespace, $object->current->getNamespace() ); |
| 59 |
$this->assertEquals( $title, $object->current->getText() ); |
| 60 |
} |
| 61 |
|
| 62 |
public static function provideNumberOfRows() { |
| 63 |
return [ |
| 64 |
[ 0 ], |
| 65 |
[ 1 ], |
| 66 |
[ 122 ], |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function testCountWithVaryingValues( $numRows ) { |
| 75 |
$object = new TitleArrayFromResult( $this->getMockResultWrapper( |
| 76 |
$this->getRowWithTitle(), |
| 77 |
$numRows |
| 78 |
) ); |
| 79 |
$this->assertEquals( $numRows, $object->count() ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function testCurrentAfterConstruction() { |
| 86 |
$namespace = 0; |
| 87 |
$title = 'foo'; |
| 88 |
$row = $this->getRowWithTitle( $namespace, $title ); |
| 89 |
$object = new TitleArrayFromResult( $this->getMockResultWrapper( $row ) ); |
| 90 |
$this->assertInstanceOf( Title::class, $object->current() ); |
| 91 |
$this->assertEquals( $namespace, $object->current->getNamespace() ); |
| 92 |
$this->assertEquals( $title, $object->current->getText() ); |
| 93 |
} |
| 94 |
|
| 95 |
public function provideTestValid() { |
| 96 |
return [ |
| 97 |
[ $this->getRowWithTitle(), true ], |
| 98 |
[ false, false ], |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function testValid( $input, $expected ) { |
| 107 |
$object = new TitleArrayFromResult( $this->getMockResultWrapper( $input ) ); |
| 108 |
$this->assertEquals( $expected, $object->valid() ); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
} |