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

Commit f394488

Browse files
committed
Merge branch 'hotfix/response-factory'
Close #32
2 parents ebf5fb5 + 36831e8 commit f394488

3 files changed

+58
-3
lines changed

CHANGELOG.md

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,31 @@ All notable changes to this project will be documented in this file, in reverse
44

55
Versions 0.3.0 and prior were released as "weierophinney/problem-details".
66

7+
## 0.5.3 - 2018-03-12
8+
9+
### Added
10+
11+
- Nothing.
12+
13+
### Changed
14+
15+
- [#32](https://github.com/zendframework/zend-problem-details/pull/32) updates
16+
the `ProblemDetailsResponseFactoryFactory` to allow the `ResponseInterface`
17+
service to either return an instance, or a factory capable of generating an
18+
instance.
19+
20+
### Deprecated
21+
22+
- Nothing.
23+
24+
### Removed
25+
26+
- Nothing.
27+
28+
### Fixed
29+
30+
- Nothing.
31+
732
## 0.5.2 - 2018-01-10
833

934
### Added

src/ProblemDetailsResponseFactoryFactory.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public function __invoke(ContainerInterface $container) : ProblemDetailsResponse
2020
$problemDetailsConfig = $config['problem-details'] ?? [];
2121
$jsonFlags = $problemDetailsConfig['json_flags'] ?? null;
2222

23-
$responsePrototype = $container->has(ResponseInterface::class)
24-
? $container->get(ResponseInterface::class)
25-
: null;
23+
$responsePrototype = $this->getResponsePrototype($container);
2624

2725
$streamFactory = $container->has('Zend\ProblemDetails\StreamFactory')
2826
? $container->get('Zend\ProblemDetails\StreamFactory')
@@ -36,4 +34,17 @@ public function __invoke(ContainerInterface $container) : ProblemDetailsResponse
3634
$includeThrowableDetail
3735
);
3836
}
37+
38+
/**
39+
* @return null|ResponseInterface
40+
*/
41+
private function getResponsePrototype(ContainerInterface $container)
42+
{
43+
if (! $container->has(ResponseInterface::class)) {
44+
return null;
45+
}
46+
47+
$response = $container->get(ResponseInterface::class);
48+
return is_callable($response) ? $response() : $response;
49+
}
3950
}

test/ProblemDetailsResponseFactoryFactoryTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,25 @@ public function testUsesResponseServiceFromContainerWhenPresent() : void
9090
$this->assertAttributeSame($response, 'response', $factory);
9191
}
9292

93+
public function testUsesResponseServiceAsFactoryFromContainerWhenPresent() : void
94+
{
95+
$response = $this->prophesize(ResponseInterface::class)->reveal();
96+
$responseFactory = function () use ($response) {
97+
return $response;
98+
};
99+
100+
$this->container->has('config')->willReturn(false);
101+
$this->container->has(ResponseInterface::class)->willReturn(true);
102+
$this->container->get(ResponseInterface::class)->willReturn($responseFactory);
103+
$this->container->has('Zend\ProblemDetails\StreamFactory')->willReturn(false);
104+
105+
$factoryFactory = new ProblemDetailsResponseFactoryFactory();
106+
$factory = $factoryFactory($this->container->reveal());
107+
108+
$this->assertInstanceOf(ProblemDetailsResponseFactory::class, $factory);
109+
$this->assertAttributeSame($response, 'response', $factory);
110+
}
111+
93112
public function testUsesStreamFactoryServiceFromContainerWhenPresent() : void
94113
{
95114
// @codingStandardsIgnoreStart

0 commit comments

Comments
 (0)