Merge "Fix sessionfailure i18n message during authentication"
[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 use MediaWikiCoversValidator;
10
11 private function getMockResultWrapper( $row = null, $numRows = 1 ) {
12 $resultWrapper = $this->getMockBuilder( Wikimedia\Rdbms\ResultWrapper::class )
13 ->disableOriginalConstructor();
14
15 $resultWrapper = $resultWrapper->getMock();
16 $resultWrapper->expects( $this->atLeastOnce() )
17 ->method( 'current' )
18 ->will( $this->returnValue( $row ) );
19 $resultWrapper->expects( $this->any() )
20 ->method( 'numRows' )
21 ->will( $this->returnValue( $numRows ) );
22
23 return $resultWrapper;
24 }
25
26 private function getRowWithTitle( $namespace = 3, $title = 'foo' ) {
27 $row = new stdClass();
28 $row->page_namespace = $namespace;
29 $row->page_title = $title;
30 return $row;
31 }
32
33 private function getTitleArrayFromResult( $resultWrapper ) {
34 return new TitleArrayFromResult( $resultWrapper );
35 }
36
37 /**
38 * @covers TitleArrayFromResult::__construct
39 */
40 public function testConstructionWithFalseRow() {
41 $row = false;
42 $resultWrapper = $this->getMockResultWrapper( $row );
43
44 $object = $this->getTitleArrayFromResult( $resultWrapper );
45
46 $this->assertEquals( $resultWrapper, $object->res );
47 $this->assertSame( 0, $object->key );
48 $this->assertEquals( $row, $object->current );
49 }
50
51 /**
52 * @covers TitleArrayFromResult::__construct
53 */
54 public function testConstructionWithRow() {
55 $namespace = 0;
56 $title = 'foo';
57 $row = $this->getRowWithTitle( $namespace, $title );
58 $resultWrapper = $this->getMockResultWrapper( $row );
59
60 $object = $this->getTitleArrayFromResult( $resultWrapper );
61
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 );
67 }
68
69 public static function provideNumberOfRows() {
70 return [
71 [ 0 ],
72 [ 1 ],
73 [ 122 ],
74 ];
75 }
76
77 /**
78 * @dataProvider provideNumberOfRows
79 * @covers TitleArrayFromResult::count
80 */
81 public function testCountWithVaryingValues( $numRows ) {
82 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper(
83 $this->getRowWithTitle(),
84 $numRows
85 ) );
86 $this->assertEquals( $numRows, $object->count() );
87 }
88
89 /**
90 * @covers TitleArrayFromResult::current
91 */
92 public function testCurrentAfterConstruction() {
93 $namespace = 0;
94 $title = 'foo';
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 );
100 }
101
102 public function provideTestValid() {
103 return [
104 [ $this->getRowWithTitle(), true ],
105 [ false, false ],
106 ];
107 }
108
109 /**
110 * @dataProvider provideTestValid
111 * @covers TitleArrayFromResult::valid
112 */
113 public function testValid( $input, $expected ) {
114 $object = $this->getTitleArrayFromResult( $this->getMockResultWrapper( $input ) );
115 $this->assertEquals( $expected, $object->valid() );
116 }
117
118 // @todo unit test for key()
119 // @todo unit test for next()
120 // @todo unit test for rewind()
121 }