Merge "rdbms: avoid LoadBalancer::getConnection waste when using $groups"
[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 /**
34 * @covers TitleArrayFromResult::__construct
35 */
36 public function testConstructionWithFalseRow() {
37 $row = false;
38 $resultWrapper = $this->getMockResultWrapper( $row );
39
40 $object = new TitleArrayFromResult( $resultWrapper );
41
42 $this->assertEquals( $resultWrapper, $object->res );
43 $this->assertSame( 0, $object->key );
44 $this->assertEquals( $row, $object->current );
45 }
46
47 /**
48 * @covers TitleArrayFromResult::__construct
49 */
50 public function testConstructionWithRow() {
51 $namespace = 0;
52 $title = 'foo';
53 $row = $this->getRowWithTitle( $namespace, $title );
54 $resultWrapper = $this->getMockResultWrapper( $row );
55
56 $object = new TitleArrayFromResult( $resultWrapper );
57
58 $this->assertEquals( $resultWrapper, $object->res );
59 $this->assertSame( 0, $object->key );
60 $this->assertInstanceOf( Title::class, $object->current );
61 $this->assertEquals( $namespace, $object->current->mNamespace );
62 $this->assertEquals( $title, $object->current->mTextform );
63 }
64
65 public static function provideNumberOfRows() {
66 return [
67 [ 0 ],
68 [ 1 ],
69 [ 122 ],
70 ];
71 }
72
73 /**
74 * @dataProvider provideNumberOfRows
75 * @covers TitleArrayFromResult::count
76 */
77 public function testCountWithVaryingValues( $numRows ) {
78 $object = new TitleArrayFromResult( $this->getMockResultWrapper(
79 $this->getRowWithTitle(),
80 $numRows
81 ) );
82 $this->assertEquals( $numRows, $object->count() );
83 }
84
85 /**
86 * @covers TitleArrayFromResult::current
87 */
88 public function testCurrentAfterConstruction() {
89 $namespace = 0;
90 $title = 'foo';
91 $row = $this->getRowWithTitle( $namespace, $title );
92 $object = new TitleArrayFromResult( $this->getMockResultWrapper( $row ) );
93 $this->assertInstanceOf( Title::class, $object->current() );
94 $this->assertEquals( $namespace, $object->current->mNamespace );
95 $this->assertEquals( $title, $object->current->mTextform );
96 }
97
98 public function provideTestValid() {
99 return [
100 [ $this->getRowWithTitle(), true ],
101 [ false, false ],
102 ];
103 }
104
105 /**
106 * @dataProvider provideTestValid
107 * @covers TitleArrayFromResult::valid
108 */
109 public function testValid( $input, $expected ) {
110 $object = new TitleArrayFromResult( $this->getMockResultWrapper( $input ) );
111 $this->assertEquals( $expected, $object->valid() );
112 }
113
114 // @todo unit test for key()
115 // @todo unit test for next()
116 // @todo unit test for rewind()
117 }