Completed
Push — feat-events-tags ( 343dc9...26067f )
by Julien
02:09
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\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');
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...
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');
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...
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');
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...
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');
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
    }
86
    
87
    public function testNotSharedSetAndGetDefinition() {
88
        $this->assertFalse($this->object->has('test'));
89
        $def = ClassDefinition::factory('stdClass');
90
        $this->object->set('test', $def, false);
0 ignored issues
show
Unused Code introduced by
The call to Container::set() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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'));
0 ignored issues
show
Unused Code introduced by
The call to Container::set() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
119
        $this->object->set('testDef2', 'definitionDataTest', false, array('dataOne' => false, 'text' => 'hello Doe'));
0 ignored issues
show
Unused Code introduced by
The call to Container::set() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
120
        $this->object->set('testDef3', 'definitionDataTest', false, array('dataTwo' => false, 'text' => 'Hey guys!'));
0 ignored issues
show
Unused Code introduced by
The call to Container::set() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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