1010namespace ZendTest \InputFilter ;
1111
1212use PHPUnit_Framework_MockObject_MockObject as MockObject ;
13+ use Zend \Filter \FilterPluginManager ;
14+ use Zend \InputFilter \CollectionInputFilter ;
1315use Zend \InputFilter \Exception \RuntimeException ;
16+ use Zend \InputFilter \InputFilter ;
1417use Zend \InputFilter \InputFilterInterface ;
1518use Zend \InputFilter \InputFilterPluginManager ;
1619use Zend \InputFilter \InputInterface ;
20+ use Zend \ServiceManager \AbstractPluginManager ;
21+ use Zend \ServiceManager \ServiceLocatorInterface ;
22+ use Zend \Stdlib \InitializableInterface ;
23+ use Zend \Validator \ValidatorPluginManager ;
1724
1825/**
1926 * @covers Zend\InputFilter\InputFilterPluginManager
@@ -30,6 +37,16 @@ public function setUp()
3037 $ this ->manager = new InputFilterPluginManager ();
3138 }
3239
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+
3350 public function testRegisteringInvalidElementRaisesException ()
3451 {
3552 $ this ->setExpectedException (RuntimeException::class);
@@ -43,25 +60,152 @@ public function testLoadingInvalidElementRaisesException()
4360 $ this ->manager ->get ('test ' );
4461 }
4562
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+
46164 /**
47- * @covers Zend\InputFilter\InputFilterPluginManager::validatePlugin
165+ * @dataProvider serviceProvider
48166 */
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 ()
50183 {
51184 /** @var InputFilterInterface|MockObject $inputFilter */
52185 $ inputFilter = $ this ->getMock (InputFilterInterface::class);
53186
54- $ this -> assertNull ( $ this -> manager -> validatePlugin ( $ inputFilter)) ;
187+ return $ inputFilter ;
55188 }
56189
57190 /**
58- * @covers Zend\InputFilter\InputFilterPluginManager::validatePlugin
191+ * @return MockObject|InputInterface
59192 */
60- public function testAllowLoadingInstancesOfInputInterface ()
193+ protected function createInputInterfaceMock ()
61194 {
62195 /** @var InputInterface|MockObject $input */
63196 $ input = $ this ->getMock (InputInterface::class);
64197
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 ;
66210 }
67211}
0 commit comments