diff --git a/build.xml b/build.xml index 89143cd..f27755c 100644 --- a/build.xml +++ b/build.xml @@ -1,8 +1,31 @@ + + + + + + + + + + + + + + + diff --git a/composer.json b/composer.json index f42c5fb..e007c52 100644 --- a/composer.json +++ b/composer.json @@ -5,8 +5,8 @@ "license": "MIT", "extra": { "branch-alias": { - "dev-master": "0.1-dev", - "dev-develop": "0.2-dev" + "dev-master": "0.2-dev", + "dev-develop": "0.3-dev" } }, "require": { @@ -16,7 +16,10 @@ }, "require-dev": { "phpunit/phpunit": "^5|^4.8", - "phing/phing": "^2" + "phing/phing": "^2", + "zendframework/zend-validator": "^2", + "squizlabs/php_codesniffer": "^2.5", + "jakub-onderka/php-parallel-lint": "^0.9.2" }, "autoload": { "psr-4": { diff --git a/src/EventsCapable/EventsCapableInitializer.php b/src/EventsCapable/EventsCapableInitializer.php index 3887e1b..d0cb616 100644 --- a/src/EventsCapable/EventsCapableInitializer.php +++ b/src/EventsCapable/EventsCapableInitializer.php @@ -2,8 +2,11 @@ namespace abacaphiliac\EventsCapable; +use abacaphiliac\EventsCapable\Exception\ListenerNotCreatedException; use Zend\EventManager\EventsCapableInterface; use Zend\EventManager\ListenerAggregateInterface; +use Zend\ServiceManager\AbstractPluginManager; +use Zend\ServiceManager\Exception\ExceptionInterface; use Zend\ServiceManager\InitializerInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -22,6 +25,10 @@ public function initialize($instance, ServiceLocatorInterface $serviceLocator) return; } + if ($serviceLocator instanceof AbstractPluginManager) { + $serviceLocator = $serviceLocator->getServiceLocator(); + } + $events = $instance->getEventManager(); if ($serviceLocator->has('config')) { @@ -42,11 +49,42 @@ public function initialize($instance, ServiceLocatorInterface $serviceLocator) $listeners = $options->getListeners($instance); - foreach ($listeners as $listenerName) { - $listener = $serviceLocator->get($listenerName); + foreach ($listeners as $listener) { + if (is_string($listener)) { + $listener = $this->getListener($serviceLocator, $listener); + } + if ($listener instanceof ListenerAggregateInterface) { $listener->attach($events); } + + // TODO Get listener spec from config (e.g. event-name, callable, and priority. } } + + /** + * @param ServiceLocatorInterface $serviceLocator + * @param mixed $listenerName + * @return object + */ + private function getListener(ServiceLocatorInterface $serviceLocator, $listenerName) + { + if ($serviceLocator->has($listenerName)) { + try { + return $serviceLocator->get($listenerName); + } catch (ExceptionInterface $e) { + throw new ListenerNotCreatedException($e->getMessage(), $e->getCode(), $e); + } + } + + // TODO Check for constructor params and throw an exception guiding the dev to register with service container. + + if (class_exists($listenerName)) { + return new $listenerName; + } + + throw new ListenerNotCreatedException( + 'Listener must be registered in service container, or an invokable class.' + ); + } } diff --git a/src/EventsCapable/Exception/ExceptionInterface.php b/src/EventsCapable/Exception/ExceptionInterface.php new file mode 100644 index 0000000..0f90905 --- /dev/null +++ b/src/EventsCapable/Exception/ExceptionInterface.php @@ -0,0 +1,8 @@ +assertNull($actual); } + + public function testInitializeFromPluginManager() + { + $events = new EventManager(); + + $instance = $this->getMock('\Zend\EventManager\EventsCapableInterface'); + $instance->method('getEventManager')->willReturn($events); + + $listener = $this->getMock('\Zend\EventManager\ListenerAggregateInterface'); + $listener->expects($this->once())->method('attach')->with($events); + + $services = new ServiceManager(); + $services->setService('MyListener', $listener); + $services->setService('config', array( + 'abacaphiliac/events-capable' => array( + 'eventsCapable' => array( + get_class($instance) => array( + 'MyListener', + ), + ), + ), + )); + + $validators = new ValidatorPluginManager(); + $validators->setServiceLocator($services); + + $this->sut->initialize($instance, $validators); + } + + public function testUseInstantiatedListenerProvidedByConfig() + { + $events = new EventManager(); + + $instance = $this->getMock('\Zend\EventManager\EventsCapableInterface'); + $instance->method('getEventManager')->willReturn($events); + + $listener = $this->getMock('\Zend\EventManager\ListenerAggregateInterface'); + $listener->expects($this->once())->method('attach')->with($events); + + $services = new ServiceManager(); + $services->setService('config', array( + 'abacaphiliac/events-capable' => array( + 'eventsCapable' => array( + get_class($instance) => array( + $listener, + ), + ), + ), + )); + + $this->sut->initialize($instance, $services); + } + + public function testInstantiateListener() + { + $events = new EventManager(); + + $instance = $this->getMock('\Zend\EventManager\EventsCapableInterface'); + $instance->method('getEventManager')->willReturn($events); + + $listener = $this->getMock('\Zend\EventManager\ListenerAggregateInterface'); + + $services = new ServiceManager(); + $services->setService('config', array( + 'abacaphiliac/events-capable' => array( + 'eventsCapable' => array( + get_class($instance) => array( + get_class($listener), + ), + ), + ), + )); + + $actual = $this->sut->initialize($instance, $services); + + // Since we're instantiating a new instance of the mock, I don't have anything to assert :( + $this->assertNull($actual); + } + + /** + * @expectedException \abacaphiliac\EventsCapable\Exception\ListenerNotCreatedException + */ + public function testUnexpectedListenerType() + { + $events = new EventManager(); + + $instance = $this->getMock('\Zend\EventManager\EventsCapableInterface'); + $instance->method('getEventManager')->willReturn($events); + + $services = new ServiceManager(); + $services->setService('config', array( + 'abacaphiliac/events-capable' => array( + 'eventsCapable' => array( + get_class($instance) => array( + 'ClassNameThatDoesNotExist', + ), + ), + ), + )); + + $this->sut->initialize($instance, $services); + } + + /** + * @expectedException \abacaphiliac\EventsCapable\Exception\ListenerNotCreatedException + */ + public function testServiceManagerFailsToCreateListener() + { + $events = new EventManager(); + + $instance = $this->getMock('\Zend\EventManager\EventsCapableInterface'); + $instance->method('getEventManager')->willReturn($events); + + $services = new ServiceManager(); + $services->setFactory('MyListener', function () { + throw new ServiceNotCreatedException(); + }); + $services->setService('config', array( + 'abacaphiliac/events-capable' => array( + 'eventsCapable' => array( + get_class($instance) => array( + 'MyListener', + ), + ), + ), + )); + + $this->sut->initialize($instance, $services); + } }