MediaWiki REL1_29
ObjectFactoryTest.php
Go to the documentation of this file.
1<?php
21class ObjectFactoryTest extends PHPUnit_Framework_TestCase {
22
26 public function testClosureExpansionDisabled() {
27 $obj = ObjectFactory::getObjectFromSpec( [
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() {
54 $obj = ObjectFactory::getObjectFromSpec( [
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
75 $obj = ObjectFactory::getObjectFromSpec( [
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' ];
97 $obj = ObjectFactory::getObjectFromSpec( [
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' ];
112 $obj = ObjectFactory::getObjectFromSpec( [
113 // Missing 'class' or 'factory'
114 'args' => $args,
115 ] );
116 }
117
122 public function testGetObjectFromClass( $args ) {
123 $obj = ObjectFactory::getObjectFromSpec( [
124 'class' => 'ObjectFactoryTestFixture',
125 'args' => $args,
126 ] );
127 $this->assertSame( $args, $obj->args );
128 }
129
135 $obj = ObjectFactory::constructClassInstance(
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 ];
165 $obj = ObjectFactory::constructClassInstance(
166 'ObjectFactoryTestFixture', $args
167 );
168 }
169}
170
172 public $args;
174 public function __construct( /*...*/ ) {
175 $this->args = func_get_args();
176 }
177 public function setter( /*...*/ ) {
178 $this->setterArgs = func_get_args();
179 }
180}
if( $line===false) $args
Definition cdb.php:63
testClosureExpansionDisabled()
ObjectFactory::getObjectFromSpec.
testGetObjectFromClass( $args)
ObjectFactory::getObjectFromSpec provideConstructClassInstance.
testGetObjectFromInvalid()
ObjectFactory::getObjectFromSpec InvalidArgumentException.
testClosureExpansionEnabled()
ObjectFactory::getObjectFromSpec ObjectFactory::expandClosures.
testGetObjectFromFactory()
ObjectFactory::getObjectFromSpec.
testNamedArgs()
ObjectFactory::constructClassInstance InvalidArgumentException.
testConstructClassInstance( $args)
ObjectFactory::constructClassInstance provideConstructClassInstance.
static provideConstructClassInstance()
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