Skip to content

Commit

Permalink
refactor Application::bootstrap() tests
Browse files Browse the repository at this point in the history
as explained in zendframework#7240, previous tests assertions were too specific and
prevented the introduction of additional default event listeners
  • Loading branch information
stefanotorresi committed Feb 23, 2015
1 parent ee3d114 commit 30bd6dc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 73 deletions.
1 change: 1 addition & 0 deletions library/Zend/Mvc/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Application implements
protected $defaultListeners = array(
'RouteListener',
'DispatchListener',
'HttpMethodListener',
'ViewManager',
'SendResponseListener',
);
Expand Down
122 changes: 49 additions & 73 deletions tests/ZendTest/Mvc/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,70 +137,68 @@ public function testEventsAreEmptyAtFirst()
$this->assertAttributeEquals(array(), 'identifiers', $sharedEvents);
}

public function testBootstrapRegistersRouteListener()
/**
* @param string $listenerServiceName
* @param string $event
* @param string $method
*
* @dataProvider bootstrapRegistersListenersProvider
*/
public function testBootstrapRegistersListeners($listenerServiceName, $event, $method, $isCustom = false)
{
$routeListener = $this->serviceManager->get('RouteListener');
$this->application->bootstrap();
$listenerService = $this->serviceManager->get($listenerServiceName);
$this->application->bootstrap($isCustom ? (array) $listenerServiceName : array());
$events = $this->application->getEventManager();
$listeners = $events->getListeners(MvcEvent::EVENT_ROUTE);
$this->assertEquals(1, count($listeners));
$listener = $listeners->top();
$callback = $listener->getCallback();
$this->assertSame(array($routeListener, 'onRoute'), $callback);
}
$listeners = $events->getListeners($event);

public function testBootstrapRegistersDispatchListener()
{
$dispatchListener = $this->serviceManager->get('DispatchListener');
$this->application->bootstrap();
$events = $this->application->getEventManager();
$listeners = $events->getListeners(MvcEvent::EVENT_DISPATCH);
$this->assertEquals(1, count($listeners));
$listener = $listeners->top();
$callback = $listener->getCallback();
$this->assertSame(array($dispatchListener, 'onDispatch'), $callback);
$foundListener = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
$foundListener = $callback === array($listenerService, $method);
if ($foundListener) {
break;
}
}
$this->assertTrue($foundListener);
}

public function testBootstrapRegistersSendResponseListener()
public function bootstrapRegistersListenersProvider()
{
$sendResponseListener = $this->serviceManager->get('SendResponseListener');
$this->application->bootstrap();
$events = $this->application->getEventManager();
$listeners = $events->getListeners(MvcEvent::EVENT_FINISH);
$this->assertEquals(1, count($listeners));
$listener = $listeners->top();
$callback = $listener->getCallback();
$this->assertSame(array($sendResponseListener, 'sendResponse'), $callback);
return array(
array('RouteListener', MvcEvent::EVENT_ROUTE, 'onRoute'),
array('DispatchListener', MvcEvent::EVENT_DISPATCH, 'onDispatch'),
array('SendResponseListener', MvcEvent::EVENT_FINISH, 'sendResponse'),
array('ViewManager', MvcEvent::EVENT_BOOTSTRAP, 'onBootstrap'),
array('HttpMethodListener', MvcEvent::EVENT_ROUTE, 'onRoute'),
array('BootstrapListener', MvcEvent::EVENT_BOOTSTRAP, 'onBootstrap', true),
);
}

public function testBootstrapRegistersViewManagerAsBootstrapListener()
public function testBootstrapAlwaysRegistersDefaultListeners()
{
$viewManager = $this->serviceManager->get('ViewManager');
$this->application->bootstrap();
$events = $this->application->getEventManager();
$listeners = $events->getListeners(MvcEvent::EVENT_BOOTSTRAP);
$this->assertEquals(1, count($listeners));
$listener = $listeners->top();
$callback = $listener->getCallback();
$this->assertSame(array($viewManager, 'onBootstrap'), $callback);
}
$refl = new \ReflectionProperty($this->application, 'defaultListeners');
$refl->setAccessible(true);
$defaultListenersNames = $refl->getValue($this->application);
$defaultListeners = array();
foreach ($defaultListenersNames as $defaultListenerName) {
$defaultListeners[] = $this->serviceManager->get($defaultListenerName);
}

public function testBootstrapCanRegisterHttpMethodListener()
{
$httpMethodListener = $this->serviceManager->get('HttpMethodListener');
$this->application->bootstrap(array('HttpMethodListener'));
$events = $this->application->getEventManager();
$listeners = $events->getListeners(MvcEvent::EVENT_ROUTE);
$foundListener = false;
foreach($listeners as $listener) {
$callback = $listener->getCallback();
$foundListener = $callback === array($httpMethodListener, 'onRoute');
if ($foundListener) {
break;
$this->application->bootstrap(array('BootstrapListener'));
$eventManager = $this->application->getEventManager();

$registeredListeners = array();
foreach ($eventManager->getEvents() as $event) {
$listeners = $eventManager->getListeners($event);
foreach ($listeners as $listener) {
$callback = $listener->getCallBack();
$registeredListeners[] = $callback[0];
}
}

$this->assertTrue($foundListener);
foreach ($defaultListeners as $defaultListener) {
$this->assertContains($defaultListener, $registeredListeners);
}
}

public function testBootstrapRegistersConfiguredMvcEvent()
Expand Down Expand Up @@ -648,28 +646,6 @@ public function testCompleteRequestShouldReturnApplicationInstance()
$this->assertSame($this->application, $result);
}


public function testCustomListener()
{
$this->application->bootstrap(array('BootstrapListener'));

// must contain custom bootstrap listeners
$bootstrapListener = $this->serviceManager->get('BootstrapListener');
$listeners = $this->application->getEventManager()->getListeners(MvcEvent::EVENT_BOOTSTRAP);
$bootstrapListeners = $bootstrapListener->getListeners();
$this->assertTrue($listeners->contains($bootstrapListeners[0]));

// must contain default listeners
$listeners = $this->application->getEventManager()->getListeners(MvcEvent::EVENT_DISPATCH);
$this->assertEquals(1, count($listeners));

$listeners = $this->application->getEventManager()->getListeners(MvcEvent::EVENT_ROUTE);
$this->assertEquals(1, count($listeners));

$listeners = $this->application->getEventManager()->getListeners(MvcEvent::EVENT_FINISH);
$this->assertEquals(1, count($listeners));
}

public function testFailedRoutingShouldBePreventable()
{
$this->application->bootstrap();
Expand Down

0 comments on commit 30bd6dc

Please sign in to comment.