ClassDefinitionTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
cbo 3
dl 0
loc 138
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A getContainer() 0 8 1
A testStdClassInvocation() 0 5 1
A testStdClassReferenceInvocation() 0 7 1
A testToBeLoadedClassInvocation() 0 5 1
A testStdClassInvalidReferenceInvocation() 0 6 1
A testInvalidArgumentInvocation() 0 6 1
A testClassNotFound() 0 5 1
A testInvoke() 0 6 1
A testInvokeWithErroneousArguments() 0 6 1
A testInvokeWithReferences() 0 7 1
A testInvokeWithReferencesShortcuts() 0 7 1
A testInvokeWithMethodCalls() 0 8 1
A testInvokeWithRemovedMethodCalls() 0 7 1
A testInvokeWithInvalidMethodCallsArguments() 0 8 1
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());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a string.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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());
0 ignored issues
show
Unused Code introduced by
$it is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
}