Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / TitleArrayFromResultTest.php
blob7c2973f93f2c94eee14e98827fa4a236e8d3baf4
1 <?php
3 /**
4 * @author Addshore
5 * @covers TitleArrayFromResult
6 */
7 class TitleArrayFromResultTest extends PHPUnit_Framework_TestCase {
9 private function getMockResultWrapper( $row = null, $numRows = 1 ) {
10 $resultWrapper = $this->getMockBuilder( 'ResultWrapper' )
11 ->disableOriginalConstructor();
13 $resultWrapper = $resultWrapper->getMock();
14 $resultWrapper->expects( $this->atLeastOnce() )
15 ->method( 'current' )
16 ->will( $this->returnValue( $row ) );
17 $resultWrapper->expects( $this->any() )
18 ->method( 'numRows' )
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;
28 return $row;
31 private function getTitleArrayFromResult( $resultWrapper ) {
32 return new TitleArrayFromResult( $resultWrapper );
35 /**
36 * @covers TitleArrayFromResult::__construct
38 public function testConstructionWithFalseRow() {
39 $row = false;
40 $resultWrapper = $this->getMockResultWrapper( $row );
42 $object = $this->getTitleArrayFromResult( $resultWrapper );
44 $this->assertEquals( $resultWrapper, $object->res );
45 $this->assertSame( 0, $object->key );
46 $this->assertEquals( $row, $object->current );
49 /**
50 * @covers TitleArrayFromResult::__construct
52 public function testConstructionWithRow() {
53 $namespace = 0;
54 $title = 'foo';
55 $row = $this->getRowWithTitle( $namespace, $title );
56 $resultWrapper = $this->getMockResultWrapper( $row );
58 $object = $this->getTitleArrayFromResult( $resultWrapper );
60 $this->assertEquals( $resultWrapper, $object->res );
61 $this->assertSame( 0, $object->key );
62 $this->assertInstanceOf( 'Title', $object->current );
63 $this->assertEquals( $namespace, $object->current->mNamespace );
64 $this->assertEquals( $title, $object->current->mTextform );
67 public static function provideNumberOfRows() {
68 return [
69 [ 0 ],
70 [ 1 ],
71 [ 122 ],
75 /**
76 * @dataProvider provideNumberOfRows
77 * @covers TitleArrayFromResult::count
79 public function testCountWithVaryingValues( $numRows ) {
80 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper(
81 $this->getRowWithTitle(),
82 $numRows
83 ) );
84 $this->assertEquals( $numRows, $object->count() );
87 /**
88 * @covers TitleArrayFromResult::current
90 public function testCurrentAfterConstruction() {
91 $namespace = 0;
92 $title = 'foo';
93 $row = $this->getRowWithTitle( $namespace, $title );
94 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper( $row ) );
95 $this->assertInstanceOf( 'Title', $object->current() );
96 $this->assertEquals( $namespace, $object->current->mNamespace );
97 $this->assertEquals( $title, $object->current->mTextform );
100 public function provideTestValid() {
101 return [
102 [ $this->getRowWithTitle(), true ],
103 [ false, false ],
108 * @dataProvider provideTestValid
109 * @covers TitleArrayFromResult::valid
111 public function testValid( $input, $expected ) {
112 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper( $input ) );
113 $this->assertEquals( $expected, $object->valid() );
116 // @todo unit test for key()
117 // @todo unit test for next()
118 // @todo unit test for rewind()