Completed
Push — feat-events-tags ( 033a2d...8b9dcd )
by Julien
02:32
created

ContainerTest::testServicesSearch()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.8571
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace Fwk\Di;
4
use Fwk\Di\Definitions\ClassDefinition;
5
6
/**
7
 */
8
class ContainerTest extends \PHPUnit_Framework_TestCase {
9
10
    /**
11
     * @var Container
12
     */
13
    protected $object;
14
15
    /**
16
     */
17
    protected function setUp() {
18
        $this->object = new Container;
19
    }
20
21
    /**
22
     */
23
    public function testBasicSetAndGet() {
24
        $this->assertFalse($this->object->has('test'));
25
        $this->object->set('test', 'just a string');
26
        $this->assertTrue($this->object->has('test'));
27
        $this->assertEquals('just a string', $this->object->get('test'));
28
    }
29
    
30
    /**
31
     */
32
    public function testNonSharedSetAndGetCallable() {
33
        $this->assertFalse($this->object->has('test'));
34
        $callable = function () { $a = new \stdClass(); $a->mt = microtime(true); return $a; };
35
        $this->object->set('test', $callable);
36
        $this->assertTrue($this->object->has('test'));
37
        $this->assertFalse($this->object->isShared('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', $callable, true);
47
        $this->assertTrue($this->object->has('test'));
48
        $this->assertTrue($this->object->isShared('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');
0 ignored issues
show
Unused Code introduced by
$inst 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...
57
    }
58
    
59
    public function testInvalidSharedDefinition() {
60
        $this->setExpectedException('Fwk\Di\Exceptions\DefinitionNotFoundException');
61
        $inst = $this->object->isShared('test');
0 ignored issues
show
Unused Code introduced by
$inst 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...
62
    }
63
    
64
    /**
65
     *
66
     */
67
    public function testUnregisterNotShared() {
68
        $this->testNonSharedSetAndGetCallable();
69
        
70
        $this->assertTrue($this->object->has('test'));
71
        $inst = $this->object->get('test');
0 ignored issues
show
Unused Code introduced by
$inst 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...
72
        $this->object->unregister('test');
73
        $this->assertFalse($this->object->has('test'));
74
    }
75
    
76
    /**
77
     *
78
     */
79
    public function testUnregisterShared() {
80
        $this->testSharedSetAndGetCallable();
81
        
82
        $this->assertTrue($this->object->has('test'));
83
        $this->assertTrue($this->object->isShared('test'));
84
        $inst = $this->object->get('test');
0 ignored issues
show
Unused Code introduced by
$inst 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...
85
        $this->object->unregister('test');
86
        $this->assertFalse($this->object->has('test'));
87
    }
88
    
89
    public function testUnregisterInvalidSharedDefinition() {
90
        $this->setExpectedException('Fwk\Di\Exceptions\DefinitionNotFoundException');
91
        $inst = $this->object->unregister('test');
0 ignored issues
show
Unused Code introduced by
$inst 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...
92
    }
93
    
94
    public function testNotSharedSetAndGetDefinition() {
95
        $this->assertFalse($this->object->has('test'));
96
        $def = ClassDefinition::factory('stdClass');
97
        $this->object->set('test', $def, false);
98
        $this->assertTrue($this->object->has('test'));
99
        $this->assertFalse($this->object->isShared('test'));
100
        $inst = $this->object->get('test');
101
        $this->assertInstanceOf('\stdClass', $inst);
102
        $this->assertFalse($inst === $this->object->get('test'));
103
    }
104
    
105
    public function testSharedSetAndGetDefinition() {
106
        $this->assertFalse($this->object->has('test'));
107
        $def = ClassDefinition::factory('stdClass');
108
        $this->object->set('test', $def, true);
109
        $this->assertTrue($this->object->has('test'));
110
        $this->assertTrue($this->object->isShared('test'));
111
        $inst = $this->object->get('test');
112
        $this->assertInstanceOf('\stdClass', $inst);
113
        $this->assertTrue($inst === $this->object->get('test'));
114
    }
115
116
    public function testProperties()
117
    {
118
        $this->object->setProperty('testPropOne', 'one');
119
        $this->object->setProperty('testPropTwo', 'two');
120
        $this->object->setProperty('testPhrase', ':testPropOne+:testPropOne=:testPropTwo');
121
122
        $this->assertEquals('one+one=two', $this->object->getProperty('testPhrase'));
123
    }
124
125
    public function testDefinitionData()
126
    {
127
        $this->object->set('testDef', 'definitionDataTest', false, array('one' => 1, 'two' => 2, 'three' => 3));
128
129
        $data = $this->object->getDefinitionData('testDef');
130
        $this->assertTrue(is_array($data));
131
        $this->assertEquals(3, count($data));
132
        foreach ($data as $k => $v) {
133
            $this->assertFalse(strpos($k, '__fwk_di', 0));
134
        }
135
    }
136
137
    public function testServicesSearch()
138
    {
139
        $this->object->set('testDef', 'definitionDataTest', false, array('dataOne' => true, 'text' => 'hello John'));
140
        $this->object->set('testDef2', 'definitionDataTest', false, array('dataOne' => false, 'text' => 'hello Doe'));
141
        $this->object->set('testDef3', 'definitionDataTest', false, array('dataTwo' => false, 'text' => 'Hey guys!'));
142
143
        $results = $this->object->search(array('dataOne' => true));
144
        $this->assertEquals(1, count($results));
145
        $this->assertArrayHasKey('testDef', $results);
146
        $results = $this->object->search(array('dataOne' => false));
147
        $this->assertEquals(1, count($results));
148
        $this->assertArrayHasKey('testDef2', $results);
149
150
        $results = $this->object->search(array('nothing'));
151
        $this->assertEquals(0, count($results));
152
153
        $results = $this->object->search(array('text' => 'hello*'));
154
        $this->assertEquals(2, count($results));
155
        $this->assertArrayHasKey('testDef', $results);
156
        $this->assertArrayHasKey('testDef2', $results);
157
158
        $results = $this->object->search(array('text' => '*guys?'));
159
        $this->assertEquals(1, count($results));
160
        $this->assertArrayHasKey('testDef3', $results);
161
    }
162
}