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