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