ServicesManagerTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 110
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testCanConstruct() 0 11 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
use Onoi\CallbackContainer\CallbackContainerFactory;
7
8
/**
9
 * @covers \Onoi\CallbackContainer\ServicesManager
10
 * @group onoi-callback-container
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.0
14
 *
15
 * @author mwjames
16
 */
17
class ServicesManagerTest extends \PHPUnit_Framework_TestCase {
18
19
	private $servicesManager;
20
21
	protected function setUp() {
22
		parent::setUp();
23
24
		$callbackContainerFactory = new CallbackContainerFactory();
25
		$this->servicesManager = $callbackContainerFactory->newServicesManager();
26
	}
27
28
	public function testCanConstruct() {
29
30
		$containerBuilder = $this->getMockBuilder( '\Onoi\CallbackContainer\ContainerBuilder' )
31
			->disableOriginalConstructor()
32
			->getMock();
33
34
		$this->assertInstanceOf(
35
			'\Onoi\CallbackContainer\ServicesManager',
36
			new ServicesManager( $containerBuilder )
37
		);
38
	}
39
40
	public function testAddServiceWithScalarType() {
41
42
		$instance = $this->servicesManager;
43
		$instance->add( 'Foo', 123 );
44
45
		$this->assertTrue(
46
			$instance->has( 'Foo' )
47
		);
48
49
		$this->assertSame(
50
			123,
51
			$instance->get( 'Foo' )
52
		);
53
	}
54
55
	public function testAddServiceWithObjectType() {
56
57
		$instance = $this->servicesManager;
58
		$instance->add( 'Foo', $this );
59
60
		$this->assertTrue(
61
			$instance->has( 'Foo' )
62
		);
63
64
		$this->assertSame(
65
			$this,
66
			$instance->get( 'Foo' )
67
		);
68
	}
69
70
	public function testRemoveService() {
71
72
		$instance = $this->servicesManager;
73
		$instance->add( 'Foo', $this );
74
75
		$this->assertTrue(
76
			$instance->has( 'Foo' )
77
		);
78
79
		$instance->remove( 'Foo' );
80
81
		$this->assertFalse(
82
			$instance->has( 'Foo' )
83
		);
84
	}
85
86
	public function testOverrideUntypedService() {
87
88
		$instance = $this->servicesManager;
89
		$instance->add( 'Foo', $this );
90
91
		$this->assertTrue(
92
			$instance->has( 'Foo' )
93
		);
94
95
		$instance->replace( 'Foo', 123 );
96
97
		$this->assertSame(
98
			123,
99
			$instance->get( 'Foo' )
100
		);
101
	}
102
103
	public function testTryToOverrideTypedServiceWithIncompatibleTypeThrowsException() {
104
105
		$instance = $this->servicesManager;
106
		$instance->add( 'Foo', $this, '\PHPUnit_Framework_TestCase' );
107
108
		$this->assertTrue(
109
			$instance->has( 'Foo' )
110
		);
111
112
		$instance->replace( 'Foo', 123 );
113
114
		$this->setExpectedException( '\Onoi\CallbackContainer\Exception\ServiceTypeMismatchException' );
115
		$instance->get( 'Foo' );
116
	}
117
118
	public function testTryToAccessToUnknownServiceThrowsException() {
119
120
		$instance = $this->servicesManager;
121
122
		$this->setExpectedException( '\Onoi\CallbackContainer\Exception\ServiceNotFoundException' );
123
		$instance->get( 'Foo' );
124
	}
125
126
}
127