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',
29 'args' => [ function() {
33 'setter' => [ function() {
37 'closure_expansion' => false,
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]() );
46 * @covers ObjectFactory::getObjectFromSpec
47 * @covers ObjectFactory::expandClosures
49 public function testClosureExpansionEnabled() {
50 $obj = ObjectFactory
::getObjectFromSpec( [
51 'class' => 'ObjectFactoryTestFixture',
52 'args' => [ function() {
56 'setter' => [ function() {
60 'closure_expansion' => true,
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() {
73 'setter' => [ function() {
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] );
85 * @covers ObjectFactory::getObjectFromSpec
87 public function testGetObjectFromFactory() {
89 $obj = ObjectFactory
::getObjectFromSpec( [
90 'factory' => function ( $a, $b ) {
91 return new ObjectFactoryTestFixture( $a, $b );
95 $this->assertSame( $args, $obj->args
);
99 * @covers ObjectFactory::getObjectFromSpec
100 * @expectedException InvalidArgumentException
102 public function testGetObjectFromInvalid() {
103 $args = [ 'a', 'b' ];
104 $obj = ObjectFactory
::getObjectFromSpec( [
105 // Missing 'class' or 'factory'
111 * @covers ObjectFactory::getObjectFromSpec
112 * @dataProvider provideConstructClassInstance
114 public function testGetObjectFromClass( $args ) {
115 $obj = ObjectFactory
::getObjectFromSpec( [
116 'class' => 'ObjectFactoryTestFixture',
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!
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
{
166 public function __construct( /*...*/ ) {
167 $this->args
= func_get_args();
169 public function setter( /*...*/ ) {
170 $this->setterArgs
= func_get_args();