MediaWiki  REL1_31
TitleArrayFromResultTest.php
Go to the documentation of this file.
1 <?php
2 
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 
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 
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 
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 
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 
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 }
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
TitleArrayFromResultTest\testCountWithVaryingValues
testCountWithVaryingValues( $numRows)
provideNumberOfRows TitleArrayFromResult::count
Definition: TitleArrayFromResultTest.php:81
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:2006
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
TitleArrayFromResultTest
Definition: TitleArrayFromResultTest.php:7
TitleArrayFromResultTest\testConstructionWithFalseRow
testConstructionWithFalseRow()
TitleArrayFromResult::__construct.
Definition: TitleArrayFromResultTest.php:40
TitleArrayFromResultTest\testConstructionWithRow
testConstructionWithRow()
TitleArrayFromResult::__construct.
Definition: TitleArrayFromResultTest.php:54
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:145
TitleArrayFromResult
Definition: TitleArrayFromResult.php:29
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
TitleArrayFromResultTest\testCurrentAfterConstruction
testCurrentAfterConstruction()
TitleArrayFromResult::current.
Definition: TitleArrayFromResultTest.php:92
TitleArrayFromResultTest\getRowWithTitle
getRowWithTitle( $namespace=3, $title='foo')
Definition: TitleArrayFromResultTest.php:26
TitleArrayFromResultTest\getTitleArrayFromResult
getTitleArrayFromResult( $resultWrapper)
Definition: TitleArrayFromResultTest.php:33
TitleArrayFromResultTest\getMockResultWrapper
getMockResultWrapper( $row=null, $numRows=1)
Definition: TitleArrayFromResultTest.php:11
Wikimedia
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
TitleArrayFromResultTest\testValid
testValid( $input, $expected)
provideTestValid TitleArrayFromResult::valid
Definition: TitleArrayFromResultTest.php:113
TitleArrayFromResultTest\provideTestValid
provideTestValid()
Definition: TitleArrayFromResultTest.php:102
TitleArrayFromResultTest\provideNumberOfRows
static provideNumberOfRows()
Definition: TitleArrayFromResultTest.php:69
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187