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

Commit

Permalink
Merge branch 'hotfix/response-factory'
Browse files Browse the repository at this point in the history
Close #32
  • Loading branch information
weierophinney committed Mar 12, 2018
2 parents 948285c + ac776c1 commit 3892fc3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ All notable changes to this project will be documented in this file, in reverse

Versions prior to 0.4.0 were released as the package "weierophinney/hal".

## 0.6.3 - 2018-03-12

### Added

- Nothing.

### Changed

- [#32](https://github.com/zendframework/zend-expressive-hal/pull/32) modifies
`HalResponseFactoryFactory` to test if a `ResponseInterface` service instance
is `callable` before returning it; if it is, it calls it first. This allows
the `ResponseInterface` service to return a response _factory_ instead of an
instance.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 0.6.2 - 2018-01-03

### Added
Expand Down
3 changes: 2 additions & 1 deletion src/HalResponseFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function __invoke(ContainerInterface $container) : HalResponseFactory
private function getResponseInstance(ContainerInterface $container) : ResponseInterface
{
if ($container->has(ResponseInterface::class)) {
return $container->get(ResponseInterface::class);
$response = $container->get(ResponseInterface::class);
return is_callable($response) ? $response() : $response;
}

if (class_exists(Response::class)) {
Expand Down
33 changes: 33 additions & 0 deletions test/HalResponseFactoryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,37 @@ public function testReturnsHalResponseFactoryInstanceWithoutConfiguredDependenci
self::assertAttributeInstanceOf(ResponseInterface::class, 'responsePrototype', $instance);
self::assertAttributeInstanceOf(Closure::class, 'streamFactory', $instance);
}

public function testReturnsHalResponseFactoryInstanceWhenResponseInterfaceReturnsFactory()
{
$jsonRenderer = $this->prophesize(Renderer\JsonRenderer::class)->reveal();
$xmlRenderer = $this->prophesize(Renderer\XmlRenderer::class)->reveal();
$response = $this->prophesize(ResponseInterface::class)->reveal();
$responseFactory = function () use ($response) {
return $response;
};
$stream = new class()
{
public function __invoke()
{
}
};

$container = $this->prophesize(ContainerInterface::class);
$container->has(Renderer\JsonRenderer::class)->willReturn(true);
$container->get(Renderer\JsonRenderer::class)->willReturn($jsonRenderer);
$container->has(Renderer\XmlRenderer::class)->willReturn(true);
$container->get(Renderer\XmlRenderer::class)->willReturn($xmlRenderer);
$container->has(ResponseInterface::class)->willReturn(true);
$container->get(ResponseInterface::class)->willReturn($responseFactory);
$container->has(StreamInterface::class)->willReturn(true);
$container->get(StreamInterface::class)->willReturn($stream);

$instance = (new HalResponseFactoryFactory())($container->reveal());
self::assertInstanceOf(HalResponseFactory::class, $instance);
self::assertAttributeSame($jsonRenderer, 'jsonRenderer', $instance);
self::assertAttributeSame($xmlRenderer, 'xmlRenderer', $instance);
self::assertAttributeSame($response, 'responsePrototype', $instance);
self::assertAttributeSame($stream, 'streamFactory', $instance);
}
}

0 comments on commit 3892fc3

Please sign in to comment.