MediaWiki  REL1_31
UserArrayFromResultTest.php
Go to the documentation of this file.
1 <?php
2 
8 
9  private function getMockResultWrapper( $row = null, $numRows = 1 ) {
10  $resultWrapper = $this->getMockBuilder( Wikimedia\Rdbms\ResultWrapper::class )
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 getRowWithUsername( $username = 'fooUser' ) {
25  $row = new stdClass();
26  $row->user_name = $username;
27  return $row;
28  }
29 
30  private function getUserArrayFromResult( $resultWrapper ) {
31  return new UserArrayFromResult( $resultWrapper );
32  }
33 
37  public function testConstructionWithFalseRow() {
38  $row = false;
39  $resultWrapper = $this->getMockResultWrapper( $row );
40 
41  $object = $this->getUserArrayFromResult( $resultWrapper );
42 
43  $this->assertEquals( $resultWrapper, $object->res );
44  $this->assertSame( 0, $object->key );
45  $this->assertEquals( $row, $object->current );
46  }
47 
51  public function testConstructionWithRow() {
52  $username = 'addshore';
53  $row = $this->getRowWithUsername( $username );
54  $resultWrapper = $this->getMockResultWrapper( $row );
55 
56  $object = $this->getUserArrayFromResult( $resultWrapper );
57 
58  $this->assertEquals( $resultWrapper, $object->res );
59  $this->assertSame( 0, $object->key );
60  $this->assertInstanceOf( User::class, $object->current );
61  $this->assertEquals( $username, $object->current->mName );
62  }
63 
64  public static function provideNumberOfRows() {
65  return [
66  [ 0 ],
67  [ 1 ],
68  [ 122 ],
69  ];
70  }
71 
76  public function testCountWithVaryingValues( $numRows ) {
77  $object = $this->getUserArrayFromResult( $this->getMockResultWrapper(
78  $this->getRowWithUsername(),
79  $numRows
80  ) );
81  $this->assertEquals( $numRows, $object->count() );
82  }
83 
87  public function testCurrentAfterConstruction() {
88  $username = 'addshore';
89  $userRow = $this->getRowWithUsername( $username );
90  $object = $this->getUserArrayFromResult( $this->getMockResultWrapper( $userRow ) );
91  $this->assertInstanceOf( User::class, $object->current() );
92  $this->assertEquals( $username, $object->current()->mName );
93  }
94 
95  public function provideTestValid() {
96  return [
97  [ $this->getRowWithUsername(), true ],
98  [ false, false ],
99  ];
100  }
101 
106  public function testValid( $input, $expected ) {
107  $object = $this->getUserArrayFromResult( $this->getMockResultWrapper( $input ) );
108  $this->assertEquals( $expected, $object->valid() );
109  }
110 
111  // @todo unit test for key()
112  // @todo unit test for next()
113  // @todo unit test for rewind()
114 }
UserArrayFromResultTest\testCurrentAfterConstruction
testCurrentAfterConstruction()
UserArrayFromResult::current.
Definition: UserArrayFromResultTest.php:87
UserArrayFromResultTest\getRowWithUsername
getRowWithUsername( $username='fooUser')
Definition: UserArrayFromResultTest.php:24
UserArrayFromResultTest\testConstructionWithRow
testConstructionWithRow()
UserArrayFromResult::__construct.
Definition: UserArrayFromResultTest.php:51
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
UserArrayFromResultTest
Definition: UserArrayFromResultTest.php:7
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:145
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
UserArrayFromResultTest\provideNumberOfRows
static provideNumberOfRows()
Definition: UserArrayFromResultTest.php:64
UserArrayFromResult
Definition: UserArrayFromResult.php:25
UserArrayFromResultTest\provideTestValid
provideTestValid()
Definition: UserArrayFromResultTest.php:95
UserArrayFromResultTest\getMockResultWrapper
getMockResultWrapper( $row=null, $numRows=1)
Definition: UserArrayFromResultTest.php:9
UserArrayFromResultTest\testCountWithVaryingValues
testCountWithVaryingValues( $numRows)
provideNumberOfRows UserArrayFromResult::count
Definition: UserArrayFromResultTest.php:76
Wikimedia
UserArrayFromResultTest\testValid
testValid( $input, $expected)
provideTestValid UserArrayFromResult::valid
Definition: UserArrayFromResultTest.php:106
$username
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:785
UserArrayFromResultTest\testConstructionWithFalseRow
testConstructionWithFalseRow()
UserArrayFromResult::__construct.
Definition: UserArrayFromResultTest.php:37
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
UserArrayFromResultTest\getUserArrayFromResult
getUserArrayFromResult( $resultWrapper)
Definition: UserArrayFromResultTest.php:30
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187