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
21 class ObjectFactoryTest
extends PHPUnit_Framework_TestCase
{
24 * @covers ObjectFactory::getObjectFromSpec
26 public function testClosureExpansionDisabled() {
27 $obj = ObjectFactory
::getObjectFromSpec( [
28 'class' => 'ObjectFactoryTestFixture',
36 'setter' => [ function () {
40 'closure_expansion' => false,
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]() );
50 * @covers ObjectFactory::getObjectFromSpec
51 * @covers ObjectFactory::expandClosures
53 public function testClosureExpansionEnabled() {
54 $obj = ObjectFactory
::getObjectFromSpec( [
55 'class' => 'ObjectFactoryTestFixture',
63 'setter' => [ function () {
67 'closure_expansion' => true,
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] );
75 $obj = ObjectFactory
::getObjectFromSpec( [
76 'class' => 'ObjectFactoryTestFixture',
77 'args' => [ function () {
81 'setter' => [ function () {
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] );
93 * @covers ObjectFactory::getObjectFromSpec
95 public function testGetObjectFromFactory() {
97 $obj = ObjectFactory
::getObjectFromSpec( [
98 'factory' => function ( $a, $b ) {
99 return new ObjectFactoryTestFixture( $a, $b );
103 $this->assertSame( $args, $obj->args
);
107 * @covers ObjectFactory::getObjectFromSpec
108 * @expectedException InvalidArgumentException
110 public function testGetObjectFromInvalid() {
111 $args = [ 'a', 'b' ];
112 $obj = ObjectFactory
::getObjectFromSpec( [
113 // Missing 'class' or 'factory'
119 * @covers ObjectFactory::getObjectFromSpec
120 * @dataProvider provideConstructClassInstance
122 public function testGetObjectFromClass( $args ) {
123 $obj = ObjectFactory
::getObjectFromSpec( [
124 'class' => 'ObjectFactoryTestFixture',
127 $this->assertSame( $args, $obj->args
);
131 * @covers ObjectFactory::constructClassInstance
132 * @dataProvider provideConstructClassInstance
134 public function testConstructClassInstance( $args ) {
135 $obj = ObjectFactory
::constructClassInstance(
136 'ObjectFactoryTestFixture', $args
138 $this->assertSame( $args, $obj->args
);
141 public static function provideConstructClassInstance() {
142 // These args go to 11. I thought about making 10 one louder, but 11!
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, ] ],
160 * @covers ObjectFactory::constructClassInstance
161 * @expectedException InvalidArgumentException
163 public function testNamedArgs() {
164 $args = [ 'foo' => 1, 'bar' => 2, 'baz' => 3 ];
165 $obj = ObjectFactory
::constructClassInstance(
166 'ObjectFactoryTestFixture', $args
171 class ObjectFactoryTestFixture
{
174 public function __construct( /*...*/ ) {
175 $this->args
= func_get_args();
177 public function setter( /*...*/ ) {
178 $this->setterArgs
= func_get_args();