1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fwk\Di; |
4
|
|
|
use Fwk\Di\Definitions\CallableDefinition; |
5
|
|
|
use Fwk\Di\Definitions\ClassDefinition; |
6
|
|
|
use Fwk\Di\Definitions\ScalarDefinition; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
*/ |
10
|
|
|
class ContainerTest extends \PHPUnit_Framework_TestCase { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var Container |
14
|
|
|
*/ |
15
|
|
|
protected $object; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
*/ |
19
|
|
|
protected function setUp() { |
20
|
|
|
$this->object = new Container; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
*/ |
25
|
|
|
public function testBasicSetAndGet() { |
26
|
|
|
$this->assertFalse($this->object->has('test')); |
27
|
|
|
$this->object->set('test', 'just a string'); |
28
|
|
|
$this->assertTrue($this->object->has('test')); |
29
|
|
|
$this->assertEquals('just a string', $this->object->get('test')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
*/ |
34
|
|
|
public function testNonSharedSetAndGetCallable() { |
35
|
|
|
$this->assertFalse($this->object->has('test')); |
36
|
|
|
$callable = function () { $a = new \stdClass(); $a->mt = microtime(true); return $a; }; |
37
|
|
|
$this->object->set('test', $callable); |
38
|
|
|
$this->assertTrue($this->object->has('test')); |
39
|
|
|
$inst = $this->object->get('test'); |
40
|
|
|
$this->assertInstanceOf('\stdClass', $inst); |
41
|
|
|
$this->assertFalse($inst === $this->object->get('test')); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testSharedSetAndGetCallable() { |
45
|
|
|
$this->assertFalse($this->object->has('test')); |
46
|
|
|
$callable = function () { $a = new \stdClass(); $a->mt = microtime(true); return $a; }; |
47
|
|
|
$this->object->set('test', CallableDefinition::factory($callable)->setShared(true)); |
48
|
|
|
$this->assertTrue($this->object->has('test')); |
49
|
|
|
$inst = $this->object->get('test'); |
50
|
|
|
$this->assertInstanceOf('stdClass', $inst); |
51
|
|
|
$this->assertTrue($inst === $this->object->get('test')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testInvalidDefinition() { |
55
|
|
|
$this->setExpectedException('Fwk\Di\Exceptions\DefinitionNotFoundException'); |
56
|
|
|
$inst = $this->object->get('test'); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
public function testUnregisterNotShared() { |
63
|
|
|
$this->testNonSharedSetAndGetCallable(); |
64
|
|
|
|
65
|
|
|
$this->assertTrue($this->object->has('test')); |
66
|
|
|
$inst = $this->object->get('test'); |
|
|
|
|
67
|
|
|
$this->object->unregister('test'); |
68
|
|
|
$this->assertFalse($this->object->has('test')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
|
|
public function testUnregisterShared() { |
75
|
|
|
$this->testSharedSetAndGetCallable(); |
76
|
|
|
|
77
|
|
|
$this->assertTrue($this->object->has('test')); |
78
|
|
|
$inst = $this->object->get('test'); |
|
|
|
|
79
|
|
|
$this->object->unregister('test'); |
80
|
|
|
$this->assertFalse($this->object->has('test')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testUnregisterInvalidSharedDefinition() { |
84
|
|
|
$this->setExpectedException('Fwk\Di\Exceptions\DefinitionNotFoundException'); |
85
|
|
|
$inst = $this->object->unregister('test'); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testNotSharedSetAndGetDefinition() { |
89
|
|
|
$this->assertFalse($this->object->has('test')); |
90
|
|
|
$def = ClassDefinition::factory('stdClass'); |
91
|
|
|
$this->object->set('test', $def, false); |
|
|
|
|
92
|
|
|
$this->assertTrue($this->object->has('test')); |
93
|
|
|
$inst = $this->object->get('test'); |
94
|
|
|
$this->assertInstanceOf('\stdClass', $inst); |
95
|
|
|
$this->assertFalse($inst === $this->object->get('test')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testSharedSetAndGetDefinition() { |
99
|
|
|
$this->assertFalse($this->object->has('test')); |
100
|
|
|
$def = ClassDefinition::factory('stdClass'); |
101
|
|
|
$this->object->set('test', $def->setShared(true)); |
102
|
|
|
$this->assertTrue($this->object->has('test')); |
103
|
|
|
$inst = $this->object->get('test'); |
104
|
|
|
$this->assertInstanceOf('\stdClass', $inst); |
105
|
|
|
$this->assertTrue($inst === $this->object->get('test')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testProperties() |
109
|
|
|
{ |
110
|
|
|
$this->object->setProperty('testPropOne', 'one'); |
111
|
|
|
$this->object->setProperty('testPropTwo', 'two'); |
112
|
|
|
$this->object->setProperty('testPhrase', ':testPropOne+:testPropOne=:testPropTwo'); |
113
|
|
|
|
114
|
|
|
$this->assertEquals('one+one=two', $this->object->getProperty('testPhrase')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testServicesSearch() |
118
|
|
|
{ |
119
|
|
|
$this->object->set('testDef', ScalarDefinition::factory('definitionDataTest')->setData(array('dataOne' => true, 'text' => 'hello John'))); |
120
|
|
|
$this->object->set('testDef2', ScalarDefinition::factory('definitionDataTest')->setData(array('dataOne' => false, 'text' => 'hello Doe'))); |
121
|
|
|
$this->object->set('testDef3', ScalarDefinition::factory('definitionDataTest')->setData(array('dataTwo' => false, 'text' => 'Hey guys!'))); |
122
|
|
|
|
123
|
|
|
$results = $this->object->search(array('dataOne' => true)); |
124
|
|
|
$this->assertEquals(1, count($results)); |
125
|
|
|
$this->assertArrayHasKey('testDef', $results); |
126
|
|
|
$results = $this->object->search(array('dataOne' => false)); |
127
|
|
|
$this->assertEquals(1, count($results)); |
128
|
|
|
$this->assertArrayHasKey('testDef2', $results); |
129
|
|
|
|
130
|
|
|
$results = $this->object->search(array('nothing')); |
131
|
|
|
$this->assertEquals(0, count($results)); |
132
|
|
|
|
133
|
|
|
$results = $this->object->search(array('text' => 'hello*')); |
134
|
|
|
$this->assertEquals(2, count($results)); |
135
|
|
|
$this->assertArrayHasKey('testDef', $results); |
136
|
|
|
$this->assertArrayHasKey('testDef2', $results); |
137
|
|
|
|
138
|
|
|
$results = $this->object->search(array('text' => '*guys?')); |
139
|
|
|
$this->assertEquals(1, count($results)); |
140
|
|
|
$this->assertArrayHasKey('testDef3', $results); |
141
|
|
|
} |
142
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.