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

Commit

Permalink
Merge c448c28 into ebf5fb5
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 12, 2018
2 parents ebf5fb5 + c448c28 commit e2d4663
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/ProblemDetailsResponseFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public function __invoke(ContainerInterface $container) : ProblemDetailsResponse
$problemDetailsConfig = $config['problem-details'] ?? [];
$jsonFlags = $problemDetailsConfig['json_flags'] ?? null;

$responsePrototype = $container->has(ResponseInterface::class)
? $container->get(ResponseInterface::class)
: null;
$responsePrototype = $this->getResponsePrototype($container);

$streamFactory = $container->has('Zend\ProblemDetails\StreamFactory')
? $container->get('Zend\ProblemDetails\StreamFactory')
Expand All @@ -36,4 +34,17 @@ public function __invoke(ContainerInterface $container) : ProblemDetailsResponse
$includeThrowableDetail
);
}

/**
* @return null|ResponseInterface
*/
private function getResponsePrototype(ContainerInterface $container)
{
if (! $container->has(ResponseInterface::class)) {
return null;
}

$response = $container->get(ResponseInterface::class);
return is_callable($response) ? $response() : $response;
}
}
19 changes: 19 additions & 0 deletions test/ProblemDetailsResponseFactoryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ public function testUsesResponseServiceFromContainerWhenPresent() : void
$this->assertAttributeSame($response, 'response', $factory);
}

public function testUsesResponseServiceAsFactoryFromContainerWhenPresent() : void
{
$response = $this->prophesize(ResponseInterface::class)->reveal();
$responseFactory = function () use ($response) {
return $response;
};

$this->container->has('config')->willReturn(false);
$this->container->has(ResponseInterface::class)->willReturn(true);
$this->container->get(ResponseInterface::class)->willReturn($responseFactory);
$this->container->has('Zend\ProblemDetails\StreamFactory')->willReturn(false);

$factoryFactory = new ProblemDetailsResponseFactoryFactory();
$factory = $factoryFactory($this->container->reveal());

$this->assertInstanceOf(ProblemDetailsResponseFactory::class, $factory);
$this->assertAttributeSame($response, 'response', $factory);
}

public function testUsesStreamFactoryServiceFromContainerWhenPresent() : void
{
// @codingStandardsIgnoreStart
Expand Down

0 comments on commit e2d4663

Please sign in to comment.