1
1
<?php
2
2
/**
3
- * Created by PhpStorm.
4
- * User: GeeH
5
- * Date: 06/09/2016
6
- * Time: 12:34
3
+ * Zend Framework (http://framework.zend.com/)
4
+ *
5
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
6
+ * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7
+ * @license http://framework.zend.com/license/new-bsd New BSD License
7
8
*/
8
9
9
- namespace ZendTest \ServiceManager \Tool ;
10
10
11
+ namespace ZendTest \ServiceManager \Tool ;
11
12
12
13
use Zend \ServiceManager \AbstractFactory \ConfigAbstractFactory ;
13
14
use Zend \ServiceManager \Exception \InvalidArgumentException ;
14
15
use Zend \ServiceManager \Tool \CliTool ;
15
16
use ZendTest \ServiceManager \TestAsset \FailingFactory ;
16
17
use ZendTest \ServiceManager \TestAsset \InvokableObject ;
17
18
use ZendTest \ServiceManager \TestAsset \ObjectWithScalarDependency ;
19
+ use ZendTest \ServiceManager \TestAsset \SecondComplexDependencyObject ;
18
20
use ZendTest \ServiceManager \TestAsset \SimpleDependencyObject ;
19
21
20
22
class CliToolTest extends \PHPUnit_Framework_TestCase
21
23
{
22
- public function testExceptsIfClassNameIsNotString ()
24
+ public function testCreateDependencyConfigExceptsIfClassNameIsNotString ()
23
25
{
24
26
self ::expectException (InvalidArgumentException::class);
25
27
self ::expectExceptionMessage ('Class name must be a string, integer given ' );
26
- CliTool::handle ([], 42 );
28
+ CliTool::createDependencyConfig ([], 42 );
27
29
}
28
30
29
- public function testExceptsIfClassDoesNotExist ()
31
+ public function testCreateDependencyConfigExceptsIfClassDoesNotExist ()
30
32
{
31
33
$ className = 'Dirk\Gentley\Holistic\Detective\Agency ' ;
32
34
self ::expectException (InvalidArgumentException::class);
33
35
self ::expectExceptionMessage ('Cannot find class with name ' . $ className );
34
- CliTool::handle ([], $ className );
36
+ CliTool::createDependencyConfig ([], $ className );
35
37
}
36
38
37
- public function testInvokableObjectReturnsEmptyArray ()
39
+ public function testCreateDependencyConfigInvokableObjectReturnsEmptyArray ()
38
40
{
39
- $ config = CliTool::handle ([], InvokableObject::class);
41
+ $ config = CliTool::createDependencyConfig ([], InvokableObject::class);
40
42
self ::assertEquals (
41
43
[
42
44
ConfigAbstractFactory::class => [
@@ -47,9 +49,9 @@ public function testInvokableObjectReturnsEmptyArray()
47
49
);
48
50
}
49
51
50
- public function testSimpleDependencyReturnsCorrectly ()
52
+ public function testCreateDependencyConfigSimpleDependencyReturnsCorrectly ()
51
53
{
52
- $ config = CliTool::handle ([], SimpleDependencyObject::class);
54
+ $ config = CliTool::createDependencyConfig ([], SimpleDependencyObject::class);
53
55
self ::assertEquals (
54
56
[
55
57
ConfigAbstractFactory::class => [
@@ -63,20 +65,127 @@ public function testSimpleDependencyReturnsCorrectly()
63
65
);
64
66
}
65
67
66
- public function testClassWithoutConstructorChangesNothing ()
68
+ public function testCreateDependencyConfigClassWithoutConstructorChangesNothing ()
67
69
{
68
- $ config = CliTool::handle ([ConfigAbstractFactory::class => []], FailingFactory::class);
70
+ $ config = CliTool::createDependencyConfig ([ConfigAbstractFactory::class => []], FailingFactory::class);
69
71
self ::assertEquals ([ConfigAbstractFactory::class => []], $ config );
70
72
}
71
73
72
- public function testWhatHappensWhenYouHaveNoTypeHint ()
74
+ public function testCreateDependencyConfigWithoutTypeHintedParameterExcepts ()
73
75
{
74
76
self ::expectException (InvalidArgumentException::class);
75
77
self ::expectExceptionMessage (
76
78
'Cannot create config for ' . ObjectWithScalarDependency::class . ', it has no type hints in constructor '
77
79
);
78
- $ config = CliTool::handle ([ConfigAbstractFactory::class => []], ObjectWithScalarDependency::class);
80
+ $ config = CliTool::createDependencyConfig (
81
+ [ConfigAbstractFactory::class => []],
82
+ ObjectWithScalarDependency::class
83
+ );
84
+ }
85
+
86
+ public function testCreateFactoryMappingsExceptsIfClassNameIsNotString ()
87
+ {
88
+ self ::expectException (InvalidArgumentException::class);
89
+ self ::expectExceptionMessage ('Class name must be a string, integer given ' );
90
+ CliTool::createFactoryMappings ([], 42 );
91
+ }
92
+
93
+ public function testCreateFactoryMappingsExceptsIfClassDoesNotExist ()
94
+ {
95
+ $ className = 'Dirk\Gentley\Holistic\Detective\Agency ' ;
96
+ self ::expectException (InvalidArgumentException::class);
97
+ self ::expectExceptionMessage ('Cannot find class with name ' . $ className );
98
+ CliTool::createFactoryMappings ([], $ className );
99
+ }
100
+
101
+ public function testCreateFactoryMappingsReturnsUnmodifiedArrayIfMappingExists ()
102
+ {
103
+ $ config = [
104
+ 'service_manager ' => [
105
+ 'factories ' => [
106
+ InvokableObject::class => ConfigAbstractFactory::class,
107
+ ],
108
+ ],
109
+ ];
110
+ self ::assertEquals ($ config , CliTool::createFactoryMappings ($ config , InvokableObject::class));
111
+ }
112
+
113
+ public function testCreateFactoryMappingsAddsClassIfNotExists ()
114
+ {
115
+ $ expectedConfig = [
116
+ 'service_manager ' => [
117
+ 'factories ' => [
118
+ InvokableObject::class => ConfigAbstractFactory::class,
119
+ ],
120
+ ],
121
+ ];
122
+ self ::assertEquals ($ expectedConfig , CliTool::createFactoryMappings ([], InvokableObject::class));
123
+ }
124
+
125
+ public function testCreateFactoryMappingsIgnoresExistingsMappings ()
126
+ {
127
+ $ config = [
128
+ 'service_manager ' => [
129
+ 'factories ' => [
130
+ InvokableObject::class => 'SomeOtherExistingFactory ' ,
131
+ ],
132
+ ],
133
+ ];
134
+ self ::assertEquals ($ config , CliTool::createFactoryMappings ($ config , InvokableObject::class));
135
+ }
136
+
137
+ public function testCreateFactoryMappingsFromConfigReturnsIfNoConfigKey ()
138
+ {
139
+ self ::assertEquals ([], CliTool::createFactoryMappingsFromConfig ([]));
140
+ }
141
+
142
+ public function testCreateFactoryMappingsFromConfigExceptsWhenConfigNotArray ()
143
+ {
144
+ self ::expectException (InvalidArgumentException::class);
145
+ self ::expectExceptionMessage (
146
+ 'Config key for ' . ConfigAbstractFactory::class . ' should be an array, boolean given '
147
+ );
79
148
149
+ CliTool::createFactoryMappingsFromConfig (
150
+ [
151
+ ConfigAbstractFactory::class => true ,
152
+ ]
153
+ );
80
154
}
81
155
156
+ public function testCreateFactoryMappingsFromConfigWithWorkingConfig ()
157
+ {
158
+ $ config = [
159
+ ConfigAbstractFactory::class => [
160
+ InvokableObject::class => [],
161
+ SimpleDependencyObject::class => [
162
+ InvokableObject::class,
163
+ ],
164
+ SecondComplexDependencyObject::class => [
165
+ InvokableObject::class,
166
+ ],
167
+ ],
168
+ ];
169
+
170
+ $ expectedConfig = [
171
+ ConfigAbstractFactory::class => [
172
+ InvokableObject::class => [],
173
+ SimpleDependencyObject::class => [
174
+ InvokableObject::class,
175
+ ],
176
+ SecondComplexDependencyObject::class => [
177
+ InvokableObject::class,
178
+ ],
179
+ ],
180
+ 'service_manager ' => [
181
+ 'factories ' => [
182
+ InvokableObject::class => ConfigAbstractFactory::class,
183
+ SimpleDependencyObject::class => ConfigAbstractFactory::class,
184
+ SecondComplexDependencyObject::class => ConfigAbstractFactory::class,
185
+ ],
186
+ ],
187
+ ];
188
+
189
+ self ::assertEquals ($ expectedConfig , CliTool::createFactoryMappingsFromConfig ($ config ));
190
+ }
82
191
}
0 commit comments