Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Use a factory to create the SendResponseListener #198

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Service/SendResponseListenerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* @link http://github.com/zendframework/zend-mvc for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Mvc\Service;

use Interop\Container\ContainerInterface;
use Zend\Mvc\SendResponseListener;

class SendResponseListenerFactory
{
/**
* @param ContainerInterface $container
* @return SendResponseListener
*/
public function __invoke(ContainerInterface $container)
{
$listener = new SendResponseListener();
$listener->setEventManager($container->get('EventManager'));
return $listener;
}
}
2 changes: 1 addition & 1 deletion src/Service/ServiceListenerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ServiceListenerFactory implements FactoryInterface
'ViewPrefixPathStackResolver' => 'Zend\Mvc\Service\ViewPrefixPathStackResolverFactory',
'Zend\Mvc\MiddlewareListener' => InvokableFactory::class,
'Zend\Mvc\RouteListener' => InvokableFactory::class,
'Zend\Mvc\SendResponseListener' => InvokableFactory::class,
'Zend\Mvc\SendResponseListener' => SendResponseListenerFactory::class,
'Zend\View\Renderer\FeedRenderer' => InvokableFactory::class,
'Zend\View\Renderer\JsonRenderer' => InvokableFactory::class,
'Zend\View\Renderer\PhpRenderer' => ViewPhpRendererFactory::class,
Expand Down
55 changes: 55 additions & 0 deletions test/Service/SendResponseFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @link http://github.com/zendframework/zend-mvc for the canonical source repository
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Mvc\Service;

use Interop\Container\ContainerInterface;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\SharedEventManagerInterface;
use Zend\Mvc\ResponseSender\HttpResponseSender;
use Zend\Mvc\ResponseSender\PhpEnvironmentResponseSender;
use Zend\Mvc\ResponseSender\SendResponseEvent;
use Zend\Mvc\ResponseSender\SimpleStreamResponseSender;
use Zend\Mvc\SendResponseListener;
use Zend\Mvc\Service\SendResponseListenerFactory;

class SendResponseFactoryTest extends TestCase
{
public function testFactoryReturnsListenerWithEventManagerFromContainer()
{
$sharedEvents = $this->prophesize(SharedEventManagerInterface::class);
$events = $this->prophesize(EventManagerInterface::class);
$events->getSharedManager()->will([$sharedEvents, 'reveal']);

$events->setIdentifiers([SendResponseListener::class, SendResponseListener::class])->shouldBeCalled();
$events->attach(
SendResponseEvent::EVENT_SEND_RESPONSE,
Argument::type(PhpEnvironmentResponseSender::class),
-1000
)->shouldBeCalled();
$events->attach(
SendResponseEvent::EVENT_SEND_RESPONSE,
Argument::type(SimpleStreamResponseSender::class),
-3000
)->shouldBeCalled();
$events->attach(
SendResponseEvent::EVENT_SEND_RESPONSE,
Argument::type(HttpResponseSender::class),
-4000
)->shouldBeCalled();

$container = $this->prophesize(ContainerInterface::class);
$container->get('EventManager')->will([$events, 'reveal']);

$factory = new SendResponseListenerFactory();
$listener = $factory($container->reveal());
$this->assertInstanceOf(SendResponseListener::class, $listener);
$this->assertSame($events->reveal(), $listener->getEventManager());
}
}