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

Commit

Permalink
Merge pull request #195 from Maks3w/optimize-tests
Browse files Browse the repository at this point in the history
[test] Add helpers for deal with ContainerInterface mock
  • Loading branch information
weierophinney committed Nov 24, 2015
2 parents 106f2f6 + 0a82f3f commit 5d1a32a
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 615 deletions.
10 changes: 5 additions & 5 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ public function pipe($path, $middleware = null)
{
// Lazy-load middleware from the container when possible
$container = $this->container;
if (null === $middleware && is_string($path) && $container && $container->has($path, true)) {
if (null === $middleware && is_string($path) && $container && $container->has($path)) {
$middleware = $this->marshalLazyMiddlewareService($path, $container);
$path = '/';
} elseif (is_string($middleware)
&& ! is_callable($middleware)
&& $container
&& $container->has($middleware, true)
&& $container->has($middleware)
) {
$middleware = $this->marshalLazyMiddlewareService($middleware, $container);
} elseif (null === $middleware && is_callable($path)) {
Expand Down Expand Up @@ -263,13 +263,13 @@ public function pipeErrorHandler($path, $middleware = null)
{
// Lazy-load middleware from the container
$container = $this->container;
if (null === $middleware && is_string($path) && $container && $container->has($path, true)) {
if (null === $middleware && is_string($path) && $container && $container->has($path)) {
$middleware = $this->marshalLazyErrorMiddlewareService($path, $container);
$path = '/';
} elseif (is_string($middleware)
&& ! is_callable($middleware)
&& $container
&& $container->has($middleware, true)
&& $container->has($middleware)
) {
$middleware = $this->marshalLazyErrorMiddlewareService($middleware, $container);
} elseif (null === $middleware && is_callable($path)) {
Expand Down Expand Up @@ -542,7 +542,7 @@ private function checkForDuplicateRoute($path, $methods = null)
private function marshalMiddlewareFromContainer($middleware)
{
$container = $this->container;
if (! $container || ! $container->has($middleware, true)) {
if (! $container || ! $container->has($middleware)) {
return $middleware;
}

Expand Down
28 changes: 12 additions & 16 deletions test/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace ZendTest\Expressive;

use Interop\Container\ContainerInterface;
use PHPUnit_Framework_TestCase as TestCase;
use Prophecy\Argument;
use ReflectionProperty;
Expand All @@ -26,6 +25,8 @@
*/
class ApplicationTest extends TestCase
{
use ContainerTrait;

public function setUp()
{
$this->noopMiddleware = function ($req, $res, $next) {
Expand Down Expand Up @@ -377,9 +378,8 @@ public function testInvocationWillPipeRoutingMiddlewareIfNotAlreadyPiped()

$this->router->match($request)->willReturn(RouteResult::fromRouteMatch('foo', 'foo', []));

$container = $this->prophesize(ContainerInterface::class);
$container->has('foo')->willReturn(true);
$container->get('foo')->willReturn($middleware);
$container = $this->mockContainerInterface();
$this->injectServiceInContainer($container, 'foo', $middleware);

$app = new Application($this->router->reveal(), $container->reveal());

Expand Down Expand Up @@ -417,9 +417,8 @@ public function testPipingAllowsPassingMiddlewareServiceNameAsSoleArgument()
return 'invoked';
};

$container = $this->prophesize(ContainerInterface::class);
$container->has('foo', true)->willReturn(true);
$container->get('foo')->willReturn($middleware);
$container = $this->mockContainerInterface();
$this->injectServiceInContainer($container, 'foo', $middleware);

$app = new Application($this->router->reveal(), $container->reveal());
$app->pipe('foo');
Expand All @@ -444,9 +443,8 @@ public function testAllowsPipingErrorMiddlewareUsingServiceNameAsSoleArgument()
return 'invoked';
};

$container = $this->prophesize(ContainerInterface::class);
$container->has('foo', true)->willReturn(true);
$container->get('foo')->willReturn($middleware);
$container = $this->mockContainerInterface();
$this->injectServiceInContainer($container, 'foo', $middleware);

$app = new Application($this->router->reveal(), $container->reveal());
$app->pipeErrorHandler('foo');
Expand All @@ -471,9 +469,8 @@ public function testAllowsPipingMiddlewareAsServiceNameWithPath()
return 'invoked';
};

$container = $this->prophesize(ContainerInterface::class);
$container->has('foo', true)->willReturn(true);
$container->get('foo')->willReturn($middleware);
$container = $this->mockContainerInterface();
$this->injectServiceInContainer($container, 'foo', $middleware);

$app = new Application($this->router->reveal(), $container->reveal());
$app->pipe('/foo', 'foo');
Expand All @@ -498,9 +495,8 @@ public function testAllowsPipingErrorMiddlewareAsServiceNameWithPath()
return 'invoked';
};

$container = $this->prophesize(ContainerInterface::class);
$container->has('foo', true)->willReturn(true);
$container->get('foo')->willReturn($middleware);
$container = $this->mockContainerInterface();
$this->injectServiceInContainer($container, 'foo', $middleware);

$app = new Application($this->router->reveal(), $container->reveal());
$app->pipeErrorHandler('/foo', 'foo');
Expand Down
Loading

0 comments on commit 5d1a32a

Please sign in to comment.