f8dda6f8af6503eb619ca5c858e6f625e10208c1
[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' => [ function() {
30 return 'unwrapped';
31 }, ],
32 'calls' => [
33 'setter' => [ function() {
34 return 'unwrapped';
35 }, ],
36 ],
37 'closure_expansion' => false,
38 ] );
39 $this->assertInstanceOf( 'Closure', $obj->args[0] );
40 $this->assertSame( 'unwrapped', $obj->args[0]() );
41 $this->assertInstanceOf( 'Closure', $obj->setterArgs[0] );
42 $this->assertSame( 'unwrapped', $obj->setterArgs[0]() );
43 }
44
45 /**
46 * @covers ObjectFactory::getObjectFromSpec
47 * @covers ObjectFactory::expandClosures
48 */
49 public function testClosureExpansionEnabled() {
50 $obj = ObjectFactory::getObjectFromSpec( [
51 'class' => 'ObjectFactoryTestFixture',
52 'args' => [ function() {
53 return 'unwrapped';
54 }, ],
55 'calls' => [
56 'setter' => [ function() {
57 return 'unwrapped';
58 }, ],
59 ],
60 'closure_expansion' => true,
61 ] );
62 $this->assertInternalType( 'string', $obj->args[0] );
63 $this->assertSame( 'unwrapped', $obj->args[0] );
64 $this->assertInternalType( 'string', $obj->setterArgs[0] );
65 $this->assertSame( 'unwrapped', $obj->setterArgs[0] );
66
67 $obj = ObjectFactory::getObjectFromSpec( [
68 'class' => 'ObjectFactoryTestFixture',
69 'args' => [ function() {
70 return 'unwrapped';
71 }, ],
72 'calls' => [
73 'setter' => [ function() {
74 return 'unwrapped';
75 }, ],
76 ],
77 ] );
78 $this->assertInternalType( 'string', $obj->args[0] );
79 $this->assertSame( 'unwrapped', $obj->args[0] );
80 $this->assertInternalType( 'string', $obj->setterArgs[0] );
81 $this->assertSame( 'unwrapped', $obj->setterArgs[0] );
82 }
83
84 /**
85 * @covers ObjectFactory::getObjectFromSpec
86 */
87 public function testGetObjectFromFactory() {
88 $args = [ 'a', 'b' ];
89 $obj = ObjectFactory::getObjectFromSpec( [
90 'factory' => function ( $a, $b ) {
91 return new ObjectFactoryTestFixture( $a, $b );
92 },
93 'args' => $args,
94 ] );
95 $this->assertSame( $args, $obj->args );
96 }
97
98 /**
99 * @covers ObjectFactory::getObjectFromSpec
100 * @expectedException InvalidArgumentException
101 */
102 public function testGetObjectFromInvalid() {
103 $args = [ 'a', 'b' ];
104 $obj = ObjectFactory::getObjectFromSpec( [
105 // Missing 'class' or 'factory'
106 'args' => $args,
107 ] );
108 }
109
110 /**
111 * @covers ObjectFactory::getObjectFromSpec
112 * @dataProvider provideConstructClassInstance
113 */
114 public function testGetObjectFromClass( $args ) {
115 $obj = ObjectFactory::getObjectFromSpec( [
116 'class' => 'ObjectFactoryTestFixture',
117 'args' => $args,
118 ] );
119 $this->assertSame( $args, $obj->args );
120 }
121
122 /**
123 * @covers ObjectFactory::constructClassInstance
124 * @dataProvider provideConstructClassInstance
125 */
126 public function testConstructClassInstance( $args ) {
127 $obj = ObjectFactory::constructClassInstance(
128 'ObjectFactoryTestFixture', $args
129 );
130 $this->assertSame( $args, $obj->args );
131 }
132
133 public static function provideConstructClassInstance() {
134 // These args go to 11. I thought about making 10 one louder, but 11!
135 return [
136 '0 args' => [ [] ],
137 '1 args' => [ [ 1, ] ],
138 '2 args' => [ [ 1, 2, ] ],
139 '3 args' => [ [ 1, 2, 3, ] ],
140 '4 args' => [ [ 1, 2, 3, 4, ] ],
141 '5 args' => [ [ 1, 2, 3, 4, 5, ] ],
142 '6 args' => [ [ 1, 2, 3, 4, 5, 6, ] ],
143 '7 args' => [ [ 1, 2, 3, 4, 5, 6, 7, ] ],
144 '8 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, ] ],
145 '9 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, ] ],
146 '10 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ] ],
147 '11 args' => [ [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ] ],
148 ];
149 }
150
151 /**
152 * @covers ObjectFactory::constructClassInstance
153 * @expectedException InvalidArgumentException
154 */
155 public function testNamedArgs() {
156 $args = [ 'foo' => 1, 'bar' => 2, 'baz' => 3 ];
157 $obj = ObjectFactory::constructClassInstance(
158 'ObjectFactoryTestFixture', $args
159 );
160 }
161 }
162
163 class ObjectFactoryTestFixture {
164 public $args;
165 public $setterArgs;
166 public function __construct( /*...*/ ) {
167 $this->args = func_get_args();
168 }
169 public function setter( /*...*/ ) {
170 $this->setterArgs = func_get_args();
171 }
172 }