1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouterTests\Di; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Vectorface\SnappyRouter\Di\ServiceProvider; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Tests the ServiceProvider class. |
10
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
11
|
|
|
* @author Dan Bruce <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class ServiceProviderTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* An overview of how to use the ServiceProvider class. |
17
|
|
|
* @test |
18
|
|
|
*/ |
19
|
|
|
public function synopsis() |
20
|
|
|
{ |
21
|
|
|
// instantiate the class |
22
|
|
|
$config = array( |
23
|
|
|
'TestController' => 'Vectorface\SnappyRouterTests\Controller\TestDummyController' |
24
|
|
|
); |
25
|
|
|
$serviceProvider = new ServiceProvider($config); |
26
|
|
|
|
27
|
|
|
// public setters (object chaining) |
28
|
|
|
$services = array_merge( |
29
|
|
|
$config, |
30
|
|
|
array( |
31
|
|
|
'AnotherService' => '/path/to/anotherService.php', |
32
|
|
|
'AnotherServiceForFileClass' => null |
33
|
|
|
) |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
$serviceProvider->setService('AnotherService', '/path/to/anotherService.php'); |
37
|
|
|
$serviceProvider->setService( |
38
|
|
|
'AnotherServiceForFileClass', |
39
|
|
|
array( |
40
|
|
|
'file' => 'tests/Vectorface/SnappyRouterTests/Controller/NonNamespacedController.php', |
41
|
|
|
'class' => 'NonNamespacedController', |
42
|
|
|
) |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
// public getters |
46
|
|
|
$this->assertEquals( |
47
|
|
|
array_keys($services), |
48
|
|
|
$serviceProvider->getServices() |
49
|
|
|
); |
50
|
|
|
$this->assertEquals( |
51
|
|
|
'/path/to/anotherService.php', |
52
|
|
|
$serviceProvider->getService('AnotherService') |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->assertInstanceOf( |
56
|
|
|
'Vectorface\SnappyRouterTests\Controller\TestDummyController', |
57
|
|
|
$serviceProvider->getServiceInstance('TestController') |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
//Tests instanceCache |
61
|
|
|
$this->assertInstanceOf( |
62
|
|
|
'Vectorface\SnappyRouterTests\Controller\TestDummyController', |
63
|
|
|
$serviceProvider->getServiceInstance('TestController') |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->assertInstanceOf( |
67
|
|
|
'NonNamespacedController', |
68
|
|
|
$serviceProvider->getServiceInstance('AnotherServiceForFileClass') |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Test that we can retrieve a non namespaced service. |
74
|
|
|
*/ |
75
|
|
|
public function testNonNamespacedService() |
76
|
|
|
{ |
77
|
|
|
$config = array( |
78
|
|
|
'NonNamespacedController' => 'tests/Vectorface/SnappyRouterTests/Controller/NonNamespacedController.php' |
79
|
|
|
); |
80
|
|
|
$serviceProvider = new ServiceProvider($config); |
81
|
|
|
|
82
|
|
|
$this->assertInstanceOf( |
83
|
|
|
'NonNamespacedController', |
84
|
|
|
$serviceProvider->getServiceInstance('NonNamespacedController') |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Test that we can retrieve a service while in namespace provisioning mode. |
90
|
|
|
*/ |
91
|
|
|
public function testNamespaceProvisioning() |
92
|
|
|
{ |
93
|
|
|
$serviceProvider = new ServiceProvider(array()); |
94
|
|
|
$namespaces = array('Vectorface\SnappyRouterTests\Controller'); |
95
|
|
|
$serviceProvider->setNamespaces($namespaces); |
96
|
|
|
|
97
|
|
|
$this->assertInstanceOf( |
98
|
|
|
'Vectorface\SnappyRouterTests\Controller\TestDummyController', |
99
|
|
|
$serviceProvider->getServiceInstance('TestDummyController') |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Test that we get an exception if we cannot find the service in any |
105
|
|
|
* of the given namespaces. |
106
|
|
|
* @expectedException Exception |
107
|
|
|
* @expectedExceptionMessage Controller class TestDummyController was not found in any listed namespace. |
108
|
|
|
*/ |
109
|
|
|
public function testNamespaceProvisioningMissingService() |
110
|
|
|
{ |
111
|
|
|
$serviceProvider = new ServiceProvider(array()); |
112
|
|
|
$serviceProvider->setNamespaces(array()); |
113
|
|
|
|
114
|
|
|
$this->assertInstanceOf( |
115
|
|
|
'Vectorface\SnappyRouterTests\Controller\TestDummyController', |
116
|
|
|
$serviceProvider->getServiceInstance('TestDummyController') |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Test that we can retrieve a service while in folder provisioning mode. |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function testFolderProvisioning() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$serviceProvider = new ServiceProvider(array()); |
126
|
|
|
$folders = array(realpath(__DIR__.'/../')); |
127
|
|
|
$serviceProvider->setFolders($folders); |
128
|
|
|
|
129
|
|
|
$this->assertInstanceOf( |
130
|
|
|
'NonNamespacedController', |
131
|
|
|
$serviceProvider->getServiceInstance('NonNamespacedController') |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Test that we get an exception if we cannot find the service in any |
137
|
|
|
* of the given folders (recursively checking). |
138
|
|
|
* @expectedException Exception |
139
|
|
|
* @expectedExceptionMessage Controller class NonExistantController not found in any listed folder. |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function testFolderProvisioningMissingService() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$serviceProvider = new ServiceProvider(array()); |
144
|
|
|
$folders = array(realpath(__DIR__.'/../')); |
145
|
|
|
$serviceProvider->setFolders($folders); |
146
|
|
|
|
147
|
|
|
$serviceProvider->getServiceInstance('NonExistantController'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.