From d88d6846dc003cc962738c1b7b3a09440782fc11 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Mon, 24 Aug 2015 10:31:23 +0200 Subject: [PATCH] Consolidate InputFilterPluginManager tests --- composer.json | 2 +- test/InputFilterPluginManagerTest.php | 156 +++++++++++++++++++++++++- 2 files changed, 151 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index e899bc0b..18a91390 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "zendframework/zend-i18n": "~2.5", "zendframework/zend-servicemanager": "~2.5", "fabpot/php-cs-fixer": "1.7.*", - "phpunit/PHPUnit": "~4.0" + "phpunit/PHPUnit": "^4.5" }, "suggest": { "zendframework/zend-servicemanager": "To support plugin manager support" diff --git a/test/InputFilterPluginManagerTest.php b/test/InputFilterPluginManagerTest.php index 56b6c1cf..23314b89 100644 --- a/test/InputFilterPluginManagerTest.php +++ b/test/InputFilterPluginManagerTest.php @@ -10,10 +10,17 @@ namespace ZendTest\InputFilter; use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Zend\Filter\FilterPluginManager; +use Zend\InputFilter\CollectionInputFilter; use Zend\InputFilter\Exception\RuntimeException; +use Zend\InputFilter\InputFilter; use Zend\InputFilter\InputFilterInterface; use Zend\InputFilter\InputFilterPluginManager; use Zend\InputFilter\InputInterface; +use Zend\ServiceManager\AbstractPluginManager; +use Zend\ServiceManager\ServiceLocatorInterface; +use Zend\Stdlib\InitializableInterface; +use Zend\Validator\ValidatorPluginManager; /** * @covers Zend\InputFilter\InputFilterPluginManager @@ -30,6 +37,16 @@ public function setUp() $this->manager = new InputFilterPluginManager(); } + public function testIsASubclassOfAbstractPluginManager() + { + $this->assertInstanceOf(AbstractPluginManager::class, $this->manager); + } + + public function testIsNotSharedByDefault() + { + $this->assertFalse($this->manager->shareByDefault()); + } + public function testRegisteringInvalidElementRaisesException() { $this->setExpectedException(RuntimeException::class); @@ -43,25 +60,152 @@ public function testLoadingInvalidElementRaisesException() $this->manager->get('test'); } + public function defaultInvokableClassesProvider() + { + return [ + // Description => [$alias, $expectedInstance] + 'inputfilter' => ['inputfilter', InputFilter::class], + 'collection' => ['collection', CollectionInputFilter::class], + ]; + } + + /** + * @dataProvider defaultInvokableClassesProvider + */ + public function testDefaultInvokableClasses($alias, $expectedInstance) + { + $service = $this->manager->get($alias); + + $this->assertInstanceOf($expectedInstance, $service, 'get() return type not match'); + } + + public function testInputFilterInvokableClassSMDependenciesArePopulatedWithoutServiceLocator() + { + $this->assertNull($this->manager->getServiceLocator(), 'Plugin manager is expected to no have a service locator'); + + /** @var InputFilter $service */ + $service = $this->manager->get('inputfilter'); + + $factory = $service->getFactory(); + $this->assertSame( + $this->manager, + $factory->getInputFilterManager(), + 'Factory::getInputFilterManager() is not populated with the expected plugin manager' + ); + } + + public function testInputFilterInvokableClassSMDependenciesArePopulatedWithServiceLocator() + { + $filterManager = $this->getMock(FilterPluginManager::class); + $validatorManager = $this->getMock(ValidatorPluginManager::class); + + $serviceLocator = $this->createServiceLocatorInterfaceMock(); + $serviceLocator->method('get') + ->willReturnMap( + [ + ['FilterManager', $filterManager], + ['ValidatorManager', $validatorManager], + ] + ) + ; + + $this->manager->setServiceLocator($serviceLocator); + $this->assertSame($serviceLocator, $this->manager->getServiceLocator(), 'getServiceLocator() value not match'); + + /** @var InputFilter $service */ + $service = $this->manager->get('inputfilter'); + + $factory = $service->getFactory(); + $this->assertSame( + $this->manager, + $factory->getInputFilterManager(), + 'Factory::getInputFilterManager() is not populated with the expected plugin manager' + ); + + $defaultFilterChain = $factory->getDefaultFilterChain(); + $this->assertSame( + $filterManager, + $defaultFilterChain->getPluginManager(), + 'Factory::getDefaultFilterChain() is not populated with the expected plugin manager' + ); + + $defaultValidatorChain = $factory->getDefaultValidatorChain(); + $this->assertSame( + $validatorManager, + $defaultValidatorChain->getPluginManager(), + 'Factory::getDefaultValidatorChain() is not populated with the expected plugin manager' + ); + } + + public function serviceProvider() + { + $inputFilterInterfaceMock = $this->createInputFilterInterfaceMock(); + $inputInterfaceMock = $this->createInputInterfaceMock(); + + // @formatter:off + return [ + // Description => [$serviceName, $service, $instanceOf] + 'InputFilterInterface' => ['inputFilterInterfaceService', $inputFilterInterfaceMock, InputFilterInterface::class], + 'InputInterface' => ['inputInterfaceService', $inputInterfaceMock, InputInterface::class], + ]; + // @formatter:on + } + + /** + * @dataProvider serviceProvider + */ + public function testGet($serviceName, $service) + { + $this->manager->setService($serviceName, $service); + + $this->assertSame($service, $this->manager->get($serviceName), 'get() value not match'); + } + /** - * @covers Zend\InputFilter\InputFilterPluginManager::validatePlugin + * @dataProvider serviceProvider */ - public function testAllowLoadingInstancesOfInputFilterInterface() + public function testServicesAreInitiatedIfImplementsInitializableInterface($serviceName, $service, $instanceOf) + { + $initializableProphecy = $this->prophesize($instanceOf)->willImplement(InitializableInterface::class); + $service = $initializableProphecy->reveal(); + + $this->manager->setService($serviceName, $service); + $this->assertSame($service, $this->manager->get($serviceName), 'get() value not match'); + + /** @noinspection PhpUndefinedMethodInspection */ + $initializableProphecy->init()->shouldBeCalled(); + } + + /** + * @return MockObject|InputFilterInterface + */ + protected function createInputFilterInterfaceMock() { /** @var InputFilterInterface|MockObject $inputFilter */ $inputFilter = $this->getMock(InputFilterInterface::class); - $this->assertNull($this->manager->validatePlugin($inputFilter)); + return $inputFilter; } /** - * @covers Zend\InputFilter\InputFilterPluginManager::validatePlugin + * @return MockObject|InputInterface */ - public function testAllowLoadingInstancesOfInputInterface() + protected function createInputInterfaceMock() { /** @var InputInterface|MockObject $input */ $input = $this->getMock(InputInterface::class); - $this->assertNull($this->manager->validatePlugin($input)); + return $input; + } + + /** + * @return MockObject|ServiceLocatorInterface + */ + protected function createServiceLocatorInterfaceMock() + { + /** @var ServiceLocatorInterface|MockObject $serviceLocator */ + $serviceLocator = $this->getMock(ServiceLocatorInterface::class); + + return $serviceLocator; } }