Merge "resourceloader: Add @covers for stripBom and makeModuleResponse tests"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / ObjectFactoryTest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 class ObjectFactoryTest extends PHPUnit_Framework_TestCase {
22
23 /**
24 * @covers ObjectFactory::getObjectFromSpec
25 */
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
49 /**
50 * @covers ObjectFactory::getObjectFromSpec
51 * @covers ObjectFactory::expandClosures
52 */
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
92 /**
93 * @covers ObjectFactory::getObjectFromSpec
94 */
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
106 /**
107 * @covers ObjectFactory::getObjectFromSpec
108 * @expectedException InvalidArgumentException
109 */
110 public function testGetObjectFromInvalid() {
111 $args = [ 'a', 'b' ];
112 $obj = ObjectFactory::getObjectFromSpec( [
113 // Missing 'class' or 'factory'
114 'args' => $args,
115 ] );
116 }
117
118 /**
119 * @covers ObjectFactory::getObjectFromSpec
120 * @dataProvider provideConstructClassInstance
121 */
122 public function testGetObjectFromClass( $args ) {
123 $obj = ObjectFactory::getObjectFromSpec( [
124 'class' => 'ObjectFactoryTestFixture',
125 'args' => $args,
126 ] );
127 $this->assertSame( $args, $obj->args );
128 }
129
130 /**
131 * @covers ObjectFactory::constructClassInstance
132 * @dataProvider provideConstructClassInstance
133 */
134 public function testConstructClassInstance( $args ) {
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
159 /**
160 * @covers ObjectFactory::constructClassInstance
161 * @expectedException InvalidArgumentException
162 */
163 public function testNamedArgs() {
164 $args = [ 'foo' => 1, 'bar' => 2, 'baz' => 3 ];
165 $obj = ObjectFactory::constructClassInstance(
166 'ObjectFactoryTestFixture', $args
167 );
168 }
169 }
170
171 class ObjectFactoryTestFixture {
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 }