1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\CallbackContainer\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\CallbackContainer\ServicesManager; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\CallbackContainer\ServicesManager |
9
|
|
|
* @group onoi-callback-container |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.2 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class ServicesManagerTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testCanConstruct() { |
19
|
|
|
|
20
|
|
|
$callbackInstantiator = $this->getMockBuilder( '\Onoi\CallbackContainer\CallbackInstantiator' ) |
21
|
|
|
->disableOriginalConstructor() |
22
|
|
|
->getMock(); |
23
|
|
|
|
24
|
|
|
$this->assertInstanceOf( |
25
|
|
|
'\Onoi\CallbackContainer\ServicesManager', |
26
|
|
|
new ServicesManager( $callbackInstantiator ) |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
$this->assertInstanceOf( |
30
|
|
|
'\Onoi\CallbackContainer\ServicesManager', |
31
|
|
|
ServicesManager::newManager() |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testAddServiceWithScalarType() { |
36
|
|
|
|
37
|
|
|
$instance = ServicesManager::newManager(); |
38
|
|
|
$instance->add( 'Foo', 123 ); |
39
|
|
|
|
40
|
|
|
$this->assertTrue( |
41
|
|
|
$instance->has( 'Foo' ) |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$this->assertSame( |
45
|
|
|
123, |
46
|
|
|
$instance->getBy( 'Foo' ) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testAddServiceWithObjectType() { |
51
|
|
|
|
52
|
|
|
$instance = ServicesManager::newManager(); |
53
|
|
|
$instance->add( 'Foo', $this ); |
54
|
|
|
|
55
|
|
|
$this->assertTrue( |
56
|
|
|
$instance->has( 'Foo' ) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->assertSame( |
60
|
|
|
$this, |
61
|
|
|
$instance->getBy( 'Foo' ) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testRemoveService() { |
66
|
|
|
|
67
|
|
|
$instance = ServicesManager::newManager(); |
68
|
|
|
$instance->add( 'Foo', $this ); |
69
|
|
|
|
70
|
|
|
$this->assertTrue( |
71
|
|
|
$instance->has( 'Foo' ) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$instance->removeBy( 'Foo' ); |
75
|
|
|
|
76
|
|
|
$this->assertFalse( |
77
|
|
|
$instance->has( 'Foo' ) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testOverrideUntypedService() { |
82
|
|
|
|
83
|
|
|
$instance = ServicesManager::newManager(); |
84
|
|
|
$instance->add( 'Foo', $this ); |
85
|
|
|
|
86
|
|
|
$this->assertTrue( |
87
|
|
|
$instance->has( 'Foo' ) |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$instance->overrideWith( 'Foo', 123 ); |
91
|
|
|
|
92
|
|
|
$this->assertSame( |
93
|
|
|
123, |
94
|
|
|
$instance->getBy( 'Foo' ) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testTryToOverrideTypedServiceWithIncompatibleTypeThrowsException() { |
99
|
|
|
|
100
|
|
|
$instance = ServicesManager::newManager(); |
101
|
|
|
$instance->add( 'Foo', $this, '\PHPUnit_Framework_TestCase' ); |
102
|
|
|
|
103
|
|
|
$this->assertTrue( |
104
|
|
|
$instance->has( 'Foo' ) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$instance->overrideWith( 'Foo', 123 ); |
108
|
|
|
|
109
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
110
|
|
|
$instance->getBy( 'Foo' ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testTryToAccessToUnknownServiceThrowsException() { |
114
|
|
|
|
115
|
|
|
$instance = ServicesManager::newManager(); |
116
|
|
|
|
117
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
118
|
|
|
$instance->getBy( 'Foo' ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|