|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KochTest\Registry; |
|
4
|
|
|
|
|
5
|
|
|
use Koch\Registry\Registry; |
|
6
|
|
|
|
|
7
|
|
|
class RegistryTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var Registry |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $object; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
16
|
|
|
* This method is called before a test is executed. |
|
17
|
|
|
*/ |
|
18
|
|
|
protected function setUp() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->object = new Registry(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Tears down the fixture, for example, closes a network connection. |
|
25
|
|
|
* This method is called after a test is executed. |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function tearDown() |
|
28
|
|
|
{ |
|
29
|
|
|
unset($this->object); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @covers Koch\Registry\Registry::set |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testSet() |
|
36
|
|
|
{ |
|
37
|
|
|
// set object to registry |
|
38
|
|
|
$a = new \stdClass(); |
|
39
|
|
|
$this->object->set('A', $a); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$this->assertTrue($this->object->has('A')); |
|
42
|
|
|
|
|
43
|
|
|
// set classname to registry |
|
44
|
|
|
$this->object->set('B', 'B'); |
|
|
|
|
|
|
45
|
|
|
$this->assertTrue($this->object->has('B')); |
|
46
|
|
|
|
|
47
|
|
|
// set closure (as a resolver) to registry |
|
48
|
|
|
$closure = function () { |
|
49
|
|
|
return new \stdClass(); |
|
50
|
|
|
}; |
|
51
|
|
|
$this->object->set('C', $closure); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$this->assertTrue($this->object->has('C')); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @covers Koch\Registry\Registry::has |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testHas() |
|
60
|
|
|
{ |
|
61
|
|
|
// set object to registry |
|
62
|
|
|
$a = new \stdClass(); |
|
63
|
|
|
$this->object->set('A', $a); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
$this->assertTrue($this->object->has('A')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @covers Koch\Registry\Registry::get |
|
70
|
|
|
* @expectedException InvalidArgumentException |
|
71
|
|
|
* @expectedExceptionMessage No resolver found for "AB". Register a resolver. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testGetThrowsException() |
|
74
|
|
|
{ |
|
75
|
|
|
// throws exception on trying to fetch non existing class from registry |
|
76
|
|
|
$this->object->get('AB'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @covers Koch\Registry\Registry::get |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testGet() |
|
83
|
|
|
{ |
|
84
|
|
|
// set object to registry |
|
85
|
|
|
$a = new \stdClass(); |
|
86
|
|
|
$this->object->set('A', $a); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
$resultA = $this->object->get('A'); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertInstanceOf('StdClass', $resultA); |
|
91
|
|
|
|
|
92
|
|
|
// set classname to registry |
|
93
|
|
|
$this->object->set('B', 'KochTest\Registry\B'); |
|
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
$resultB = $this->object->get('B'); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertInstanceOf('KochTest\Registry\B', $resultB); |
|
98
|
|
|
|
|
99
|
|
|
// set closure (as a resolver) to registry |
|
100
|
|
|
$closure = function () { |
|
101
|
|
|
return new \stdClass(); |
|
102
|
|
|
}; |
|
103
|
|
|
$this->object->set('C', $closure); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
$resultC = $this->object->get('C'); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertInstanceOf('StdClass', $resultC); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
class B |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
// nothing |
|
114
|
|
|
} |
|
115
|
|
|
|
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: