7c2973f93f2c94eee14e98827fa4a236e8d3baf4
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleArrayFromResultTest.php
1 <?php
2
3 /**
4 * @author Addshore
5 * @covers TitleArrayFromResult
6 */
7 class TitleArrayFromResultTest extends PHPUnit_Framework_TestCase {
8
9 private function getMockResultWrapper( $row = null, $numRows = 1 ) {
10 $resultWrapper = $this->getMockBuilder( 'ResultWrapper' )
11 ->disableOriginalConstructor();
12
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 ) );
20
21 return $resultWrapper;
22 }
23
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;
29 }
30
31 private function getTitleArrayFromResult( $resultWrapper ) {
32 return new TitleArrayFromResult( $resultWrapper );
33 }
34
35 /**
36 * @covers TitleArrayFromResult::__construct
37 */
38 public function testConstructionWithFalseRow() {
39 $row = false;
40 $resultWrapper = $this->getMockResultWrapper( $row );
41
42 $object = $this->getTitleArrayFromResult( $resultWrapper );
43
44 $this->assertEquals( $resultWrapper, $object->res );
45 $this->assertSame( 0, $object->key );
46 $this->assertEquals( $row, $object->current );
47 }
48
49 /**
50 * @covers TitleArrayFromResult::__construct
51 */
52 public function testConstructionWithRow() {
53 $namespace = 0;
54 $title = 'foo';
55 $row = $this->getRowWithTitle( $namespace, $title );
56 $resultWrapper = $this->getMockResultWrapper( $row );
57
58 $object = $this->getTitleArrayFromResult( $resultWrapper );
59
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 );
65 }
66
67 public static function provideNumberOfRows() {
68 return [
69 [ 0 ],
70 [ 1 ],
71 [ 122 ],
72 ];
73 }
74
75 /**
76 * @dataProvider provideNumberOfRows
77 * @covers TitleArrayFromResult::count
78 */
79 public function testCountWithVaryingValues( $numRows ) {
80 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper(
81 $this->getRowWithTitle(),
82 $numRows
83 ) );
84 $this->assertEquals( $numRows, $object->count() );
85 }
86
87 /**
88 * @covers TitleArrayFromResult::current
89 */
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 );
98 }
99
100 public function provideTestValid() {
101 return [
102 [ $this->getRowWithTitle(), true ],
103 [ false, false ],
104 ];
105 }
106
107 /**
108 * @dataProvider provideTestValid
109 * @covers TitleArrayFromResult::valid
110 */
111 public function testValid( $input, $expected ) {
112 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper( $input ) );
113 $this->assertEquals( $expected, $object->valid() );
114 }
115
116 // @todo unit test for key()
117 // @todo unit test for next()
118 // @todo unit test for rewind()
119 }