Skip to content

Commit

Permalink
Merge pull request #10 from abacaphiliac/develop
Browse files Browse the repository at this point in the history
prepare 0.2.0
  • Loading branch information
abacaphiliac committed Jan 20, 2016
2 parents 1be300e + 05527d1 commit 6eecdf1
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 7 deletions.
23 changes: 23 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
<project name="abacaphiliac/events-capable" default="develop" basedir=".">

<target name="develop">
<phingcall target="lint"/>
<phingcall target="tests"/>
</target>

<target name="lint">
<phingcall target="php-lint"/>
<phingcall target="phpcs"/>
</target>

<target name="php-lint">
<exec command="vendor/bin/parallel-lint src tests"
passthru="true"
output="/dev/stdout"
error="/dev/stdout"
checkreturn="true"/>
</target>

<target name="phpcs">
<exec command="vendor/bin/phpcs --standard=PSR2 --extensions=php --error-severity=1 --warning-severity=8 src tests"
passthru="true"
output="/dev/stdout"
error="/dev/stdout"
checkreturn="true"/>
<echo msg="Congratulations! No violations were found."/>
</target>

<target name="tests">
<phingcall target="unit-tests"/>
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
42 changes: 40 additions & 2 deletions src/EventsCapable/EventsCapableInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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')) {
Expand All @@ -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.'
);
}
}
8 changes: 8 additions & 0 deletions src/EventsCapable/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace abacaphiliac\EventsCapable\Exception;

interface ExceptionInterface
{

}
8 changes: 8 additions & 0 deletions src/EventsCapable/Exception/ListenerNotCreatedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace abacaphiliac\EventsCapable\Exception;

class ListenerNotCreatedException extends \RuntimeException implements ExceptionInterface
{

}
133 changes: 131 additions & 2 deletions tests/EventsCapable/EventsCapableInitializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use abacaphiliac\EventsCapable\EventsCapableInitializer;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventsCapableInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\ServiceManager;
use Zend\Validator\ValidatorPluginManager;

class EventsCapableInitializerTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -63,4 +63,133 @@ public function testInitializeInvalidInstance()

$this->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);
}
}

0 comments on commit 6eecdf1

Please sign in to comment.