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

Commit 5e4c1e8

Browse files
committed
Merge branch 'feature/type-safety'
Close #2
2 parents c8bd6fa + c13c528 commit 5e4c1e8

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 1.0.1 - TBD
5+
## 1.0.1 - 2018-02-21
66

77
### Added
88

99
- Nothing.
1010

1111
### Changed
1212

13-
- Nothing.
13+
- [#2](https://github.com/zendframework/zend-httphandlerrunner/pull/2) modifies
14+
how the request and error response factories are composed with the
15+
`RequestHandlerRunner` class. In both cases, they are now encapsulated in a
16+
closure which also defines a return type hint, ensuring that if the factories
17+
produce an invalid return type, a PHP `TypeError` will be raised.
1418

1519
### Deprecated
1620

src/RequestHandlerRunner.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Zend\HttpHandlerRunner;
1111

12+
use Psr\Http\Message\ResponseInterface;
1213
use Psr\Http\Message\ServerRequestInterface;
1314
use Psr\Http\Server\RequestHandlerInterface;
1415
use Throwable;
@@ -66,8 +67,16 @@ public function __construct(
6667
) {
6768
$this->handler = $handler;
6869
$this->emitter = $emitter;
69-
$this->serverRequestFactory = $serverRequestFactory;
70-
$this->serverRequestErrorResponseGenerator = $serverRequestErrorResponseGenerator;
70+
71+
// Factories are cast as Closures to ensure return type safety.
72+
$this->serverRequestFactory = function () use ($serverRequestFactory) : ServerRequestInterface {
73+
return $serverRequestFactory();
74+
};
75+
76+
$this->serverRequestErrorResponseGenerator =
77+
function (Throwable $exception) use ($serverRequestErrorResponseGenerator) : ResponseInterface {
78+
return $serverRequestErrorResponseGenerator($exception);
79+
};
7180
}
7281

7382
/**

test/RequestHandlerRunnerTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use PHPUnit\Framework\Assert;
1414
use PHPUnit\Framework\TestCase;
1515
use Prophecy\Argument;
16+
use Throwable;
17+
use TypeError;
1618
use Psr\Http\Message\ResponseInterface;
1719
use Psr\Http\Message\ServerRequestInterface;
1820
use Psr\Http\Server\RequestHandlerInterface;
@@ -80,4 +82,60 @@ public function testRunPassesRequestGeneratedByRequestFactoryToHandleWhenNoReque
8082

8183
$this->assertNull($runner->run());
8284
}
85+
86+
public function testRaisesTypeErrorIfServerRequestFactoryDoesNotReturnARequestInstance()
87+
{
88+
$serverRequestFactory = function () {
89+
return null;
90+
};
91+
92+
$response = $this->prophesize(ResponseInterface::class)->reveal();
93+
$errorResponseGenerator = function (Throwable $e) use ($response) {
94+
Assert::assertInstanceOf(TypeError::class, $e);
95+
return $response;
96+
};
97+
98+
$handler = $this->prophesize(RequestHandlerInterface::class);
99+
$handler->handle(Argument::any())->shouldNotBeCalled();
100+
101+
$emitter = $this->prophesize(EmitterInterface::class);
102+
$emitter->emit($response)->shouldBeCalled();
103+
104+
$runner = new RequestHandlerRunner(
105+
$handler->reveal(),
106+
$emitter->reveal(),
107+
$serverRequestFactory,
108+
$errorResponseGenerator
109+
);
110+
111+
$this->assertNull($runner->run());
112+
}
113+
114+
public function testRaisesTypeErrorIfServerErrorResponseGeneratorFactoryDoesNotReturnAResponse()
115+
{
116+
$serverRequestFactory = function () {
117+
return null;
118+
};
119+
120+
$errorResponseGenerator = function (Throwable $e) {
121+
Assert::assertInstanceOf(TypeError::class, $e);
122+
return null;
123+
};
124+
125+
$handler = $this->prophesize(RequestHandlerInterface::class);
126+
$handler->handle(Argument::any())->shouldNotBeCalled();
127+
128+
$emitter = $this->prophesize(EmitterInterface::class);
129+
$emitter->emit(Argument::any())->shouldNotBeCalled();
130+
131+
$runner = new RequestHandlerRunner(
132+
$handler->reveal(),
133+
$emitter->reveal(),
134+
$serverRequestFactory,
135+
$errorResponseGenerator
136+
);
137+
138+
$this->expectException(TypeError::class);
139+
$runner->run();
140+
}
83141
}

0 commit comments

Comments
 (0)