MediaWiki  1.30.0
ObjectFactoryTest.php
Go to the documentation of this file.
1 <?php
21 class ObjectFactoryTest extends PHPUnit_Framework_TestCase {
22 
26  public function testClosureExpansionDisabled() {
28  'class' => 'ObjectFactoryTestFixture',
29  'args' => [
30  function () {
31  return 'wrapped';
32  },
33  'unwrapped',
34  ],
35  'calls' => [
36  'setter' => [ function () {
37  return 'wrapped';
38  }, ],
39  ],
40  'closure_expansion' => false,
41  ] );
42  $this->assertInstanceOf( 'Closure', $obj->args[0] );
43  $this->assertSame( 'wrapped', $obj->args[0]() );
44  $this->assertSame( 'unwrapped', $obj->args[1] );
45  $this->assertInstanceOf( 'Closure', $obj->setterArgs[0] );
46  $this->assertSame( 'wrapped', $obj->setterArgs[0]() );
47  }
48 
53  public function testClosureExpansionEnabled() {
55  'class' => 'ObjectFactoryTestFixture',
56  'args' => [
57  function () {
58  return 'wrapped';
59  },
60  'unwrapped',
61  ],
62  'calls' => [
63  'setter' => [ function () {
64  return 'wrapped';
65  }, ],
66  ],
67  'closure_expansion' => true,
68  ] );
69  $this->assertInternalType( 'string', $obj->args[0] );
70  $this->assertSame( 'wrapped', $obj->args[0] );
71  $this->assertSame( 'unwrapped', $obj->args[1] );
72  $this->assertInternalType( 'string', $obj->setterArgs[0] );
73  $this->assertSame( 'wrapped', $obj->setterArgs[0] );
74 
76  'class' => 'ObjectFactoryTestFixture',
77  'args' => [ function () {
78  return 'unwrapped';
79  }, ],
80  'calls' => [
81  'setter' => [ function () {
82  return 'unwrapped';
83  }, ],
84  ],
85  ] );
86  $this->assertInternalType( 'string', $obj->args[0] );
87  $this->assertSame( 'unwrapped', $obj->args[0] );
88  $this->assertInternalType( 'string', $obj->setterArgs[0] );
89  $this->assertSame( 'unwrapped', $obj->setterArgs[0] );
90  }
91 
95  public function testGetObjectFromFactory() {
96  $args = [ 'a', 'b' ];
98  'factory' => function ( $a, $b ) {
99  return new ObjectFactoryTestFixture( $a, $b );
100  },
101  'args' => $args,
102  ] );
103  $this->assertSame( $args, $obj->args );
104  }
105 
110  public function testGetObjectFromInvalid() {
111  $args = [ 'a', 'b' ];
113  // Missing 'class' or 'factory'
114  'args' => $args,
115  ] );
116  }
117 
122  public function testGetObjectFromClass( $args ) {
124  'class' => 'ObjectFactoryTestFixture',
125  'args' => $args,
126  ] );
127  $this->assertSame( $args, $obj->args );
128  }
129 
134  public function testConstructClassInstance( $args ) {
136  'ObjectFactoryTestFixture', $args
137  );
138  $this->assertSame( $args, $obj->args );
139  }
140 
141  public static function provideConstructClassInstance() {
142  // These args go to 11. I thought about making 10 one louder, but 11!
143  return [
144  '0 args' => [ [] ],
145  '1 args' => [ [ 1, ] ],
146  '2 args' => [ [ 1, 2, ] ],
147  '3 args' => [ [ 1, 2, 3, ] ],
148  '4 args' => [ [ 1, 2, 3, 4, ] ],
149  '5 args' => [ [ 1, 2, 3, 4, 5, ] ],
150  '6 args' => [ [ 1, 2, 3, 4, 5, 6, ] ],
151  '7 args' => [ [ 1, 2, 3, 4, 5, 6, 7, ] ],
152  '8 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, ] ],
153  '9 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, ] ],
154  '10 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ] ],
155  '11 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ] ],
156  ];
157  }
158 
163  public function testNamedArgs() {
164  $args = [ 'foo' => 1, 'bar' => 2, 'baz' => 3 ];
166  'ObjectFactoryTestFixture', $args
167  );
168  }
169 }
170 
172  public $args;
173  public $setterArgs;
174  public function __construct( /*...*/ ) {
175  $this->args = func_get_args();
176  }
177  public function setter( /*...*/ ) {
178  $this->setterArgs = func_get_args();
179  }
180 }
ObjectFactoryTest\testNamedArgs
testNamedArgs()
ObjectFactory::constructClassInstance InvalidArgumentException.
Definition: ObjectFactoryTest.php:163
ObjectFactoryTestFixture\$args
$args
Definition: ObjectFactoryTest.php:172
ObjectFactoryTestFixture
Definition: ObjectFactoryTest.php:171
ObjectFactoryTest
Definition: ObjectFactoryTest.php:21
ObjectFactoryTest\testGetObjectFromFactory
testGetObjectFromFactory()
ObjectFactory::getObjectFromSpec.
Definition: ObjectFactoryTest.php:95
ObjectFactoryTest\testConstructClassInstance
testConstructClassInstance( $args)
ObjectFactory::constructClassInstance provideConstructClassInstance.
Definition: ObjectFactoryTest.php:134
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:35
ObjectFactoryTestFixture\__construct
__construct()
Definition: ObjectFactoryTest.php:174
ObjectFactoryTest\provideConstructClassInstance
static provideConstructClassInstance()
Definition: ObjectFactoryTest.php:141
ObjectFactoryTest\testGetObjectFromClass
testGetObjectFromClass( $args)
ObjectFactory::getObjectFromSpec provideConstructClassInstance.
Definition: ObjectFactoryTest.php:122
ObjectFactoryTestFixture\setter
setter()
Definition: ObjectFactoryTest.php:177
ObjectFactoryTest\testClosureExpansionEnabled
testClosureExpansionEnabled()
ObjectFactory::getObjectFromSpec ObjectFactory::expandClosures.
Definition: ObjectFactoryTest.php:53
ObjectFactory\constructClassInstance
static constructClassInstance( $clazz, $args)
Construct an instance of the given class using the given arguments.
Definition: ObjectFactory.php:129
ObjectFactory\getObjectFromSpec
static getObjectFromSpec( $spec)
Instantiate an object based on a specification array.
Definition: ObjectFactory.php:58
$args
if( $line===false) $args
Definition: cdb.php:63
ObjectFactoryTest\testClosureExpansionDisabled
testClosureExpansionDisabled()
ObjectFactory::getObjectFromSpec.
Definition: ObjectFactoryTest.php:26
ObjectFactoryTest\testGetObjectFromInvalid
testGetObjectFromInvalid()
ObjectFactory::getObjectFromSpec InvalidArgumentException.
Definition: ObjectFactoryTest.php:110
captcha-old.args
args
Definition: captcha-old.py:227
ObjectFactoryTestFixture\$setterArgs
$setterArgs
Definition: ObjectFactoryTest.php:173