1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fwk\Di\Definitions; |
4
|
|
|
use Fwk\Di\Container; |
5
|
|
|
use Fwk\Di\Reference; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Test class for ClassDefinition. |
9
|
|
|
* Generated by PHPUnit on 2013-06-27 at 22:50:32. |
10
|
|
|
*/ |
11
|
|
|
class ClassDefinitionTest extends \PHPUnit_Framework_TestCase { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var ClassDefinition |
15
|
|
|
*/ |
16
|
|
|
protected $object; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Sets up the fixture, for example, opens a network connection. |
20
|
|
|
* This method is called before a test is executed. |
21
|
|
|
*/ |
22
|
|
|
protected function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->object = new ClassDefinition('\stdClass'); |
25
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function getContainer() |
29
|
|
|
{ |
30
|
|
|
$container = new Container(); |
31
|
|
|
$container['className'] = '\stdClass'; |
32
|
|
|
$container->set('temp.dir', function($c) { return sys_get_temp_dir(); }); |
33
|
|
|
|
34
|
|
|
return $container; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
*/ |
39
|
|
|
public function testStdClassInvocation() |
40
|
|
|
{ |
41
|
|
|
$this->assertEquals('\stdClass', $this->object->getClassName()); |
42
|
|
|
$this->assertInstanceOf('\stdClass', $this->object->invoke($this->getContainer())); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
*/ |
47
|
|
|
public function testStdClassReferenceInvocation() |
48
|
|
|
{ |
49
|
|
|
$this->object->setClassName($ref = new Reference('className')); |
50
|
|
|
$this->assertInstanceOf('\Fwk\Di\Reference', $this->object->getClassName()); |
51
|
|
|
$this->object = new ClassDefinition($ref); |
52
|
|
|
$this->assertInstanceOf('\stdClass', $this->object->invoke($this->getContainer())); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
*/ |
57
|
|
|
public function testToBeLoadedClassInvocation() |
58
|
|
|
{ |
59
|
|
|
$this->object->setClassName('Fwk\Xml\Map'); |
60
|
|
|
$this->assertInstanceOf('\Fwk\Xml\Map', $this->object->invoke($this->getContainer())); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
*/ |
65
|
|
|
public function testStdClassInvalidReferenceInvocation() |
66
|
|
|
{ |
67
|
|
|
$this->object = new ClassDefinition(new Reference('inexistant_ref')); |
68
|
|
|
$this->setExpectedException('Fwk\Di\Exceptions\InvalidClassDefinitionException'); |
69
|
|
|
$this->object->invoke($this->getContainer()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testInvalidArgumentInvocation() |
73
|
|
|
{ |
74
|
|
|
$this->object = new ClassDefinition(new \stdClass()); |
|
|
|
|
75
|
|
|
$this->setExpectedException('Fwk\Di\Exceptions\InvalidClassDefinitionException'); |
76
|
|
|
$this->object->invoke($this->getContainer()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
*/ |
81
|
|
|
public function testClassNotFound() { |
82
|
|
|
$this->object = new ClassDefinition('InexistantClassDi'); |
83
|
|
|
$this->setExpectedException('Fwk\Di\Exceptions\ClassNotFoundException'); |
84
|
|
|
$this->object->invoke($this->getContainer()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
*/ |
89
|
|
|
public function testInvoke() { |
90
|
|
|
$this->object = new ClassDefinition('DirectoryIterator'); |
91
|
|
|
$this->object->addArgument(sys_get_temp_dir()); |
92
|
|
|
$it = $this->object->invoke($this->getContainer()); |
93
|
|
|
$this->assertInstanceOf('DirectoryIterator', $it); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
*/ |
98
|
|
|
public function testInvokeWithErroneousArguments() { |
99
|
|
|
$this->object = new ClassDefinition('DirectoryIterator'); |
100
|
|
|
$this->object->addArgument(new Reference('invalid_ref')); |
101
|
|
|
$this->setExpectedException('\Fwk\Di\Exceptions\InvalidClassDefinitionException'); |
102
|
|
|
$it = $this->object->invoke($this->getContainer()); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
*/ |
107
|
|
|
public function testInvokeWithReferences() { |
108
|
|
|
$this->object = new ClassDefinition('DirectoryIterator'); |
109
|
|
|
$this->object->addArgument(new Reference('temp.dir')); |
110
|
|
|
$it = $this->object->invoke($this->getContainer()); |
111
|
|
|
$this->assertInstanceOf('DirectoryIterator', $it); |
112
|
|
|
$this->assertEquals($it->getPath(), sys_get_temp_dir()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testInvokeWithReferencesShortcuts() { |
116
|
|
|
$this->object = new ClassDefinition('DirectoryIterator'); |
117
|
|
|
$this->object->addArgument('@temp.dir'); |
118
|
|
|
$it = $this->object->invoke($this->getContainer()); |
119
|
|
|
$this->assertInstanceOf('DirectoryIterator', $it); |
120
|
|
|
$this->assertEquals($it->getPath(), sys_get_temp_dir()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testInvokeWithMethodCalls() { |
124
|
|
|
$this->object = new ClassDefinition('Fwk\Di\Reference'); |
125
|
|
|
$this->object->addArgument('testRef'); |
126
|
|
|
$this->object->addMethodCall('setName', array('changedName')); |
127
|
|
|
$inst = $this->object->invoke($this->getContainer()); |
128
|
|
|
$this->assertInstanceOf('\Fwk\Di\Reference', $inst); |
129
|
|
|
$this->assertEquals("changedName", (string)$inst); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testInvokeWithRemovedMethodCalls() { |
133
|
|
|
$this->testInvokeWithMethodCalls(); |
134
|
|
|
$this->object->removeMethodClass('setName'); |
135
|
|
|
$inst = $this->object->invoke($this->getContainer()); |
136
|
|
|
$this->assertInstanceOf('\Fwk\Di\Reference', $inst); |
137
|
|
|
$this->assertEquals("testRef", $inst); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testInvokeWithInvalidMethodCallsArguments() { |
141
|
|
|
$this->object = new ClassDefinition('Fwk\Di\Reference'); |
142
|
|
|
$this->object->addArgument('testRef'); |
143
|
|
|
$this->object->addMethodCall('setName', array('@invalid_ref')); |
144
|
|
|
|
145
|
|
|
$this->setExpectedException('\Fwk\Di\Exceptions\InvalidCallableDefinitionException'); |
146
|
|
|
$this->object->invoke($this->getContainer()); |
147
|
|
|
} |
148
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: