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

Commit cd9d49f

Browse files
committed
Updates to zend-stratigility 3.0.0alpha4
The major change is that the stratigility `ErrorHandler` now expects a callable response factory as the first argument, and not a response _instance_. This patch updates the `ErrorHandlerFactory` to use the `ResponseInterface` service to seed the first argument, as that service resolves to a callable factory. Tests are also updated to demosntrate that the response service is required, and that it cannot return an actual response.
1 parent 61ea007 commit cd9d49f

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"zendframework/zend-expressive-router": "^3.0.0alpha3",
3232
"zendframework/zend-expressive-template": "^2.0.0alpha1",
3333
"zendframework/zend-httphandlerrunner": "^1.0.1",
34-
"zendframework/zend-stratigility": "3.0.0alpha3"
34+
"zendframework/zend-stratigility": "3.0.0alpha4"
3535
},
3636
"require-dev": {
3737
"filp/whoops": "^1.1.10 || ^2.1.13",

composer.lock

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Container/ErrorHandlerFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Zend\Expressive\Container;
1111

1212
use Psr\Container\ContainerInterface;
13-
use Zend\Diactoros\Response;
13+
use Psr\Http\Message\ResponseInterface;
1414
use Zend\Expressive\Middleware\ErrorResponseGenerator;
1515
use Zend\Stratigility\Middleware\ErrorHandler;
1616

@@ -22,6 +22,6 @@ public function __invoke(ContainerInterface $container) : ErrorHandler
2222
? $container->get(ErrorResponseGenerator::class)
2323
: null;
2424

25-
return new ErrorHandler(new Response(), $generator);
25+
return new ErrorHandler($container->get(ResponseInterface::class), $generator);
2626
}
2727
}

test/Container/ErrorHandlerFactoryTest.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99

1010
namespace ZendTest\Expressive\Container;
1111

12+
use Closure;
1213
use PHPUnit\Framework\TestCase;
1314
use Prophecy\Prophecy\ObjectProphecy;
1415
use Psr\Container\ContainerInterface;
1516
use Psr\Http\Message\ResponseInterface;
17+
use RuntimeException;
18+
use TypeError;
1619
use Zend\Expressive\Container\ErrorHandlerFactory;
1720
use Zend\Expressive\Middleware\ErrorResponseGenerator;
1821
use Zend\Stratigility\Middleware\ErrorHandler;
@@ -28,15 +31,45 @@ public function setUp()
2831
$this->container = $this->prophesize(ContainerInterface::class);
2932
}
3033

34+
public function testFactoryFailsIfResponseServiceIsMissing()
35+
{
36+
$exception = new RuntimeException();
37+
$this->container->has(ErrorResponseGenerator::class)->willReturn(false);
38+
$this->container->get(ErrorResponseGenerator::class)->shouldNotBeCalled();
39+
$this->container->get(ResponseInterface::class)->willThrow($exception);
40+
41+
$factory = new ErrorHandlerFactory();
42+
43+
$this->expectException(RuntimeException::class);
44+
$factory($this->container->reveal());
45+
}
46+
47+
public function testFactoryFailsIfResponseServiceReturnsResponse()
48+
{
49+
$response = $this->prophesize(ResponseInterface::class)->reveal();
50+
$this->container->has(ErrorResponseGenerator::class)->willReturn(false);
51+
$this->container->get(ErrorResponseGenerator::class)->shouldNotBeCalled();
52+
$this->container->get(ResponseInterface::class)->willReturn($response);
53+
54+
$factory = new ErrorHandlerFactory();
55+
56+
$this->expectException(TypeError::class);
57+
$factory($this->container->reveal());
58+
}
59+
3160
public function testFactoryCreatesHandlerWithStratigilityGeneratorIfNoGeneratorServiceAvailable()
3261
{
3362
$this->container->has(ErrorResponseGenerator::class)->willReturn(false);
63+
$this->container->get(ErrorResponseGenerator::class)->shouldNotBeCalled();
64+
65+
$this->container->get(ResponseInterface::class)->willReturn(function () {
66+
});
3467

3568
$factory = new ErrorHandlerFactory();
3669
$handler = $factory($this->container->reveal());
3770

3871
$this->assertInstanceOf(ErrorHandler::class, $handler);
39-
$this->assertAttributeInstanceOf(ResponseInterface::class, 'responsePrototype', $handler);
72+
$this->assertAttributeInstanceOf(Closure::class, 'responseFactory', $handler);
4073
$this->assertAttributeInstanceOf(StratigilityGenerator::class, 'responseGenerator', $handler);
4174
}
4275

@@ -46,11 +79,14 @@ public function testFactoryCreatesHandlerWithGeneratorIfGeneratorServiceAvailabl
4679
$this->container->has(ErrorResponseGenerator::class)->willReturn(true);
4780
$this->container->get(ErrorResponseGenerator::class)->willReturn($generator);
4881

82+
$this->container->get(ResponseInterface::class)->willReturn(function () {
83+
});
84+
4985
$factory = new ErrorHandlerFactory();
5086
$handler = $factory($this->container->reveal());
5187

5288
$this->assertInstanceOf(ErrorHandler::class, $handler);
53-
$this->assertAttributeInstanceOf(ResponseInterface::class, 'responsePrototype', $handler);
89+
$this->assertAttributeInstanceOf(Closure::class, 'responseFactory', $handler);
5490
$this->assertAttributeSame($generator, 'responseGenerator', $handler);
5591
}
5692
}

0 commit comments

Comments
 (0)