10
10
namespace ZendTest \InputFilter ;
11
11
12
12
use PHPUnit_Framework_MockObject_MockObject as MockObject ;
13
+ use Zend \Filter \FilterPluginManager ;
14
+ use Zend \InputFilter \CollectionInputFilter ;
13
15
use Zend \InputFilter \Exception \RuntimeException ;
16
+ use Zend \InputFilter \InputFilter ;
14
17
use Zend \InputFilter \InputFilterInterface ;
15
18
use Zend \InputFilter \InputFilterPluginManager ;
16
19
use Zend \InputFilter \InputInterface ;
20
+ use Zend \ServiceManager \AbstractPluginManager ;
21
+ use Zend \ServiceManager \ServiceLocatorInterface ;
22
+ use Zend \Stdlib \InitializableInterface ;
23
+ use Zend \Validator \ValidatorPluginManager ;
17
24
18
25
/**
19
26
* @covers Zend\InputFilter\InputFilterPluginManager
@@ -30,6 +37,16 @@ public function setUp()
30
37
$ this ->manager = new InputFilterPluginManager ();
31
38
}
32
39
40
+ public function testIsASubclassOfAbstractPluginManager ()
41
+ {
42
+ $ this ->assertInstanceOf (AbstractPluginManager::class, $ this ->manager );
43
+ }
44
+
45
+ public function testIsNotSharedByDefault ()
46
+ {
47
+ $ this ->assertFalse ($ this ->manager ->shareByDefault ());
48
+ }
49
+
33
50
public function testRegisteringInvalidElementRaisesException ()
34
51
{
35
52
$ this ->setExpectedException (RuntimeException::class);
@@ -43,25 +60,152 @@ public function testLoadingInvalidElementRaisesException()
43
60
$ this ->manager ->get ('test ' );
44
61
}
45
62
63
+ public function defaultInvokableClassesProvider ()
64
+ {
65
+ return [
66
+ // Description => [$alias, $expectedInstance]
67
+ 'inputfilter ' => ['inputfilter ' , InputFilter::class],
68
+ 'collection ' => ['collection ' , CollectionInputFilter::class],
69
+ ];
70
+ }
71
+
72
+ /**
73
+ * @dataProvider defaultInvokableClassesProvider
74
+ */
75
+ public function testDefaultInvokableClasses ($ alias , $ expectedInstance )
76
+ {
77
+ $ service = $ this ->manager ->get ($ alias );
78
+
79
+ $ this ->assertInstanceOf ($ expectedInstance , $ service , 'get() return type not match ' );
80
+ }
81
+
82
+ public function testInputFilterInvokableClassSMDependenciesArePopulatedWithoutServiceLocator ()
83
+ {
84
+ $ this ->assertNull ($ this ->manager ->getServiceLocator (), 'Plugin manager is expected to no have a service locator ' );
85
+
86
+ /** @var InputFilter $service */
87
+ $ service = $ this ->manager ->get ('inputfilter ' );
88
+
89
+ $ factory = $ service ->getFactory ();
90
+ $ this ->assertSame (
91
+ $ this ->manager ,
92
+ $ factory ->getInputFilterManager (),
93
+ 'Factory::getInputFilterManager() is not populated with the expected plugin manager '
94
+ );
95
+ }
96
+
97
+ public function testInputFilterInvokableClassSMDependenciesArePopulatedWithServiceLocator ()
98
+ {
99
+ $ filterManager = $ this ->getMock (FilterPluginManager::class);
100
+ $ validatorManager = $ this ->getMock (ValidatorPluginManager::class);
101
+
102
+ $ serviceLocator = $ this ->createServiceLocatorInterfaceMock ();
103
+ $ serviceLocator ->method ('get ' )
104
+ ->willReturnMap (
105
+ [
106
+ ['FilterManager ' , $ filterManager ],
107
+ ['ValidatorManager ' , $ validatorManager ],
108
+ ]
109
+ )
110
+ ;
111
+
112
+ $ this ->manager ->setServiceLocator ($ serviceLocator );
113
+ $ this ->assertSame ($ serviceLocator , $ this ->manager ->getServiceLocator (), 'getServiceLocator() value not match ' );
114
+
115
+ /** @var InputFilter $service */
116
+ $ service = $ this ->manager ->get ('inputfilter ' );
117
+
118
+ $ factory = $ service ->getFactory ();
119
+ $ this ->assertSame (
120
+ $ this ->manager ,
121
+ $ factory ->getInputFilterManager (),
122
+ 'Factory::getInputFilterManager() is not populated with the expected plugin manager '
123
+ );
124
+
125
+ $ defaultFilterChain = $ factory ->getDefaultFilterChain ();
126
+ $ this ->assertSame (
127
+ $ filterManager ,
128
+ $ defaultFilterChain ->getPluginManager (),
129
+ 'Factory::getDefaultFilterChain() is not populated with the expected plugin manager '
130
+ );
131
+
132
+ $ defaultValidatorChain = $ factory ->getDefaultValidatorChain ();
133
+ $ this ->assertSame (
134
+ $ validatorManager ,
135
+ $ defaultValidatorChain ->getPluginManager (),
136
+ 'Factory::getDefaultValidatorChain() is not populated with the expected plugin manager '
137
+ );
138
+ }
139
+
140
+ public function serviceProvider ()
141
+ {
142
+ $ inputFilterInterfaceMock = $ this ->createInputFilterInterfaceMock ();
143
+ $ inputInterfaceMock = $ this ->createInputInterfaceMock ();
144
+
145
+ // @formatter:off
146
+ return [
147
+ // Description => [$serviceName, $service, $instanceOf]
148
+ 'InputFilterInterface ' => ['inputFilterInterfaceService ' , $ inputFilterInterfaceMock , InputFilterInterface::class],
149
+ 'InputInterface ' => ['inputInterfaceService ' , $ inputInterfaceMock , InputInterface::class],
150
+ ];
151
+ // @formatter:on
152
+ }
153
+
154
+ /**
155
+ * @dataProvider serviceProvider
156
+ */
157
+ public function testGet ($ serviceName , $ service )
158
+ {
159
+ $ this ->manager ->setService ($ serviceName , $ service );
160
+
161
+ $ this ->assertSame ($ service , $ this ->manager ->get ($ serviceName ), 'get() value not match ' );
162
+ }
163
+
46
164
/**
47
- * @covers Zend\InputFilter\InputFilterPluginManager::validatePlugin
165
+ * @dataProvider serviceProvider
48
166
*/
49
- public function testAllowLoadingInstancesOfInputFilterInterface ()
167
+ public function testServicesAreInitiatedIfImplementsInitializableInterface ($ serviceName , $ service , $ instanceOf )
168
+ {
169
+ $ initializableProphecy = $ this ->prophesize ($ instanceOf )->willImplement (InitializableInterface::class);
170
+ $ service = $ initializableProphecy ->reveal ();
171
+
172
+ $ this ->manager ->setService ($ serviceName , $ service );
173
+ $ this ->assertSame ($ service , $ this ->manager ->get ($ serviceName ), 'get() value not match ' );
174
+
175
+ /** @noinspection PhpUndefinedMethodInspection */
176
+ $ initializableProphecy ->init ()->shouldBeCalled ();
177
+ }
178
+
179
+ /**
180
+ * @return MockObject|InputFilterInterface
181
+ */
182
+ protected function createInputFilterInterfaceMock ()
50
183
{
51
184
/** @var InputFilterInterface|MockObject $inputFilter */
52
185
$ inputFilter = $ this ->getMock (InputFilterInterface::class);
53
186
54
- $ this -> assertNull ( $ this -> manager -> validatePlugin ( $ inputFilter)) ;
187
+ return $ inputFilter ;
55
188
}
56
189
57
190
/**
58
- * @covers Zend\InputFilter\InputFilterPluginManager::validatePlugin
191
+ * @return MockObject|InputInterface
59
192
*/
60
- public function testAllowLoadingInstancesOfInputInterface ()
193
+ protected function createInputInterfaceMock ()
61
194
{
62
195
/** @var InputInterface|MockObject $input */
63
196
$ input = $ this ->getMock (InputInterface::class);
64
197
65
- $ this ->assertNull ($ this ->manager ->validatePlugin ($ input ));
198
+ return $ input ;
199
+ }
200
+
201
+ /**
202
+ * @return MockObject|ServiceLocatorInterface
203
+ */
204
+ protected function createServiceLocatorInterfaceMock ()
205
+ {
206
+ /** @var ServiceLocatorInterface|MockObject $serviceLocator */
207
+ $ serviceLocator = $ this ->getMock (ServiceLocatorInterface::class);
208
+
209
+ return $ serviceLocator ;
66
210
}
67
211
}
0 commit comments