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

Commit 984eeb4

Browse files
committed
Merge branch 'hotfix/dispatch-abstract-services' into develop
Close #38
2 parents 367045a + 0dab8bb commit 984eeb4

File tree

6 files changed

+113
-4
lines changed

6 files changed

+113
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ All notable changes to this project will be documented in this file, in reverse
8585
`Zend\ModuleManager\Listener\ServiceListener` instance before retrieving and
8686
bootstrapping the `Application` instance; this ensure it is fully
8787
configured at that time.
88+
- [#38](https://github.com/zendframework/zend-mvc/pull/38) Ensure middleware
89+
tests against abstract factories
90+
- zend-servicemanager v3 modified the behavior of has() to not search abstract
91+
factories by default. You can force it to do so by passing an optional
92+
second argument, a boolean flag, with a value of boolean true.
8893

8994
## 2.6.1 - TBD
9095

src/MiddlewareListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function onDispatch(MvcEvent $event)
4848
$serviceManager = $application->getServiceManager();
4949
$middlewareName = is_string($middleware) ? $middleware : get_class($middleware);
5050

51-
if (is_string($middleware) && $serviceManager->has($middleware)) {
51+
if (is_string($middleware) && $serviceManager->has($middleware, true)) {
5252
$middleware = $serviceManager->get($middleware);
5353
}
5454
if (! is_callable($middleware)) {

test/DispatchListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function createMvcEvent($controllerMatched)
4343
return $event;
4444
}
4545

46-
public function testControllerLoaderComposedOfAbstractFactory()
46+
public function testControllerManagerUsingAbstractFactory()
4747
{
4848
$controllerManager = new ControllerManager(new ServiceManager(), ['abstract_factories' => [
4949
Controller\TestAsset\ControllerLoaderAbstractFactory::class,
@@ -64,7 +64,7 @@ public function testControllerLoaderComposedOfAbstractFactory()
6464
$this->assertSame(200, $return->getStatusCode());
6565
}
6666

67-
public function testUnlocatableControllerLoaderComposedOfAbstractFactory()
67+
public function testUnlocatableControllerViaAbstractFactory()
6868
{
6969
$controllerManager = new ControllerManager(new ServiceManager(), ['abstract_factories' => [
7070
Controller\TestAsset\UnlocatableControllerLoaderAbstractFactory::class,

test/MiddlewareListenerTest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Zend\Mvc\MiddlewareListener;
2121
use Zend\Mvc\MvcEvent;
2222
use Zend\Mvc\Router\RouteMatch;
23+
use Zend\ServiceManager\ServiceManager;
2324

2425
class MiddlewareListenerTest extends TestCase
2526
{
@@ -39,7 +40,7 @@ public function createMvcEvent($middlewareMatched, $middleware = null)
3940
$eventManager = new EventManager();
4041

4142
$serviceManager = $this->prophesize(ContainerInterface::class);
42-
$serviceManager->has($middlewareMatched)->willReturn(true);
43+
$serviceManager->has($middlewareMatched, true)->willReturn(true);
4344
$serviceManager->get($middlewareMatched)->willReturn($middleware);
4445

4546
$application = $this->prophesize(Application::class);
@@ -115,4 +116,49 @@ public function testTriggersErrorForExceptionRaisedInMiddleware()
115116
$return = $listener->onDispatch($event);
116117
$this->assertEquals('FAILED', $return);
117118
}
119+
120+
/**
121+
* Ensure that the listener tests for services in abstract factories.
122+
*
123+
* has() omits abstract factories by default; you must pass an optional
124+
* second parameter, a boolean flag, to indicate it should also search
125+
* those.
126+
*
127+
* This test ensures that.
128+
*/
129+
public function testCanLoadFromAbstractFactory()
130+
{
131+
$response = new Response();
132+
$routeMatch = $this->prophesize(RouteMatch::class);
133+
$routeMatch->getParam('middleware', false)->willReturn('test');
134+
135+
$eventManager = new EventManager();
136+
137+
$serviceManager = new ServiceManager(['abstract_factories' => [
138+
TestAsset\MiddlewareAbstractFactory::class,
139+
]]);
140+
141+
$application = $this->prophesize(Application::class);
142+
$application->getEventManager()->willReturn($eventManager);
143+
$application->getServiceManager()->willReturn($serviceManager);
144+
$application->getResponse()->willReturn($response);
145+
146+
$event = new MvcEvent();
147+
$event->setRequest(new Request());
148+
$event->setResponse($response);
149+
$event->setApplication($application->reveal());
150+
$event->setRouteMatch($routeMatch->reveal());
151+
152+
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) {
153+
$this->fail(sprintf('dispatch.error triggered when it should not be: %s', var_export($e->getError(), 1)));
154+
});
155+
156+
$listener = new MiddlewareListener();
157+
$return = $listener->onDispatch($event);
158+
$this->assertInstanceOf(Response::class, $return);
159+
160+
$this->assertInstanceOf('Zend\Http\Response', $return);
161+
$this->assertSame(200, $return->getStatusCode());
162+
$this->assertEquals(TestAsset\Middleware::class, $return->getBody());
163+
}
118164
}

test/TestAsset/Middleware.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Mvc\TestAsset;
11+
12+
use Psr\Http\Message\ServerRequestInterface;
13+
use Psr\Http\Message\ResponseInterface;
14+
15+
class Middleware
16+
{
17+
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null)
18+
{
19+
$response->getBody()->write(__CLASS__);
20+
return $response;
21+
}
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Mvc\TestAsset;
11+
12+
use Interop\Container\ContainerInterface;
13+
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
14+
15+
class MiddlewareAbstractFactory implements AbstractFactoryInterface
16+
{
17+
public $classmap = array(
18+
'test' => 'ZendTest\Mvc\TestAsset\Middleware',
19+
);
20+
21+
public function canCreateServiceWithName(ContainerInterface $container, $name)
22+
{
23+
if (! isset($this->classmap[$name])) {
24+
return false;
25+
}
26+
27+
$classname = $this->classmap[$name];
28+
return class_exists($classname);
29+
}
30+
31+
public function __invoke(ContainerInterface $container, $name, array $options = null)
32+
{
33+
$classname = $this->classmap[$name];
34+
return new $classname;
35+
}
36+
}

0 commit comments

Comments
 (0)