Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / libs / ObjectFactoryTest.php
blobf8dda6f8af6503eb619ca5c858e6f625e10208c1
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.
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.
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
18 * @file
21 class ObjectFactoryTest extends PHPUnit_Framework_TestCase {
23 /**
24 * @covers ObjectFactory::getObjectFromSpec
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 }, ],
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]() );
45 /**
46 * @covers ObjectFactory::getObjectFromSpec
47 * @covers ObjectFactory::expandClosures
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 }, ],
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] );
67 $obj = ObjectFactory::getObjectFromSpec( [
68 'class' => 'ObjectFactoryTestFixture',
69 'args' => [ function() {
70 return 'unwrapped';
71 }, ],
72 'calls' => [
73 'setter' => [ function() {
74 return 'unwrapped';
75 }, ],
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] );
84 /**
85 * @covers ObjectFactory::getObjectFromSpec
87 public function testGetObjectFromFactory() {
88 $args = [ 'a', 'b' ];
89 $obj = ObjectFactory::getObjectFromSpec( [
90 'factory' => function ( $a, $b ) {
91 return new ObjectFactoryTestFixture( $a, $b );
93 'args' => $args,
94 ] );
95 $this->assertSame( $args, $obj->args );
98 /**
99 * @covers ObjectFactory::getObjectFromSpec
100 * @expectedException InvalidArgumentException
102 public function testGetObjectFromInvalid() {
103 $args = [ 'a', 'b' ];
104 $obj = ObjectFactory::getObjectFromSpec( [
105 // Missing 'class' or 'factory'
106 'args' => $args,
107 ] );
111 * @covers ObjectFactory::getObjectFromSpec
112 * @dataProvider provideConstructClassInstance
114 public function testGetObjectFromClass( $args ) {
115 $obj = ObjectFactory::getObjectFromSpec( [
116 'class' => 'ObjectFactoryTestFixture',
117 'args' => $args,
118 ] );
119 $this->assertSame( $args, $obj->args );
123 * @covers ObjectFactory::constructClassInstance
124 * @dataProvider provideConstructClassInstance
126 public function testConstructClassInstance( $args ) {
127 $obj = ObjectFactory::constructClassInstance(
128 'ObjectFactoryTestFixture', $args
130 $this->assertSame( $args, $obj->args );
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, ] ],
152 * @covers ObjectFactory::constructClassInstance
153 * @expectedException InvalidArgumentException
155 public function testNamedArgs() {
156 $args = [ 'foo' => 1, 'bar' => 2, 'baz' => 3 ];
157 $obj = ObjectFactory::constructClassInstance(
158 'ObjectFactoryTestFixture', $args
163 class ObjectFactoryTestFixture {
164 public $args;
165 public $setterArgs;
166 public function __construct( /*...*/ ) {
167 $this->args = func_get_args();
169 public function setter( /*...*/ ) {
170 $this->setterArgs = func_get_args();