Completed
Pull Request — master (#3)
by mw
01:47
created

ServicesManagerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 106
wmc 7
lcom 0
cbo 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstruct() 0 16 1
A testAddServiceWithScalarType() 0 14 1
A testAddServiceWithObjectType() 0 14 1
A testRemoveService() 0 15 1
A testOverrideUntypedService() 0 16 1
A testTryToOverrideTypedServiceWithIncompatibleTypeThrowsException() 0 14 1
A testTryToAccessToUnknownServiceThrowsException() 0 7 1
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