Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Exception/CommonProblemDetailsExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Zend\ProblemDetails\Exception;

use function array_merge;

/**
* Common functionality for ProblemDetailsExceptionInterface implementations.
*
Expand Down
8 changes: 7 additions & 1 deletion src/ProblemDetailsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;

use function array_walk;
use function error_reporting;
use function in_array;
use function restore_error_handler;
use function set_error_handler;

/**
* Middleware that ensures a Problem Details response is returned
* for all errors and Exceptions/Throwables.
Expand Down Expand Up @@ -76,7 +82,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
*/
public function attachListener(callable $listener) : void
{
if (\in_array($listener, $this->listeners, true)) {
if (in_array($listener, $this->listeners, true)) {
return;
}

Expand Down
2 changes: 2 additions & 0 deletions src/ProblemDetailsNotFoundHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function sprintf;

class ProblemDetailsNotFoundHandler implements MiddlewareInterface
{
/**
Expand Down
20 changes: 19 additions & 1 deletion src/ProblemDetailsResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,28 @@
use Negotiation\Negotiator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Spatie\ArrayToXml\ArrayToXml;
use Throwable;

use function array_merge;
use function array_walk_recursive;
use function get_class;
use function get_resource_type;
use function is_array;
use function is_int;
use function is_resource;
use function json_decode;
use function json_encode;
use function preg_replace;
use function print_r;
use function sprintf;
use function strpos;

use const JSON_PRESERVE_ZERO_FRACTION;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;

/**
* Create a Problem Details response.
*
Expand Down
3 changes: 3 additions & 0 deletions test/Exception/ProblemDetailsExceptionInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Zend\ProblemDetails\Exception\CommonProblemDetailsExceptionTrait;
use Zend\ProblemDetails\Exception\ProblemDetailsExceptionInterface;

use function json_decode;
use function json_encode;

class ProblemDetailsExceptionTest extends TestCase
{
protected $status = 403;
Expand Down
9 changes: 8 additions & 1 deletion test/ProblemDetailsAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@
use PHPUnit\Framework\Assert;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Throwable;

use function array_walk_recursive;
use function get_class;
use function json_decode;
use function json_encode;
use function simplexml_load_string;
use function sprintf;
use function var_export;

trait ProblemDetailsAssertionsTrait
{
public function assertProblemDetails(array $expected, array $details) : void
Expand Down
9 changes: 6 additions & 3 deletions test/ProblemDetailsMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
use Psr\Http\Server\RequestHandlerInterface;
use Zend\ProblemDetails\ProblemDetailsMiddleware;
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
use ZendTest\ProblemDetails\TestAsset;

use function trigger_error;

use const E_USER_ERROR;

class ProblemDetailsMiddlewareTest extends TestCase
{
Expand Down Expand Up @@ -90,14 +93,14 @@ public function testMiddlewareRegistersErrorHandlerToConvertErrorsToProblemDetai
$handler
->handle(Argument::that([$this->request, 'reveal']))
->will(function () {
trigger_error('Triggered error!', \E_USER_ERROR);
trigger_error('Triggered error!', E_USER_ERROR);
});

$expected = $this->prophesize(ResponseInterface::class)->reveal();
$this->responseFactory
->createResponseFromThrowable($this->request->reveal(), Argument::that(function ($e) {
$this->assertInstanceOf(ErrorException::class, $e);
$this->assertEquals(\E_USER_ERROR, $e->getSeverity());
$this->assertEquals(E_USER_ERROR, $e->getSeverity());
$this->assertEquals('Triggered error!', $e->getMessage());
return true;
}))
Expand Down
5 changes: 5 additions & 0 deletions test/ProblemDetailsResponseFactoryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
use Zend\ProblemDetails\ProblemDetailsResponseFactoryFactory;

use const JSON_PRESERVE_ZERO_FRACTION;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;

class ProblemDetailsResponseFactoryFactoryTest extends TestCase
{
protected function setUp() : void
Expand Down
6 changes: 5 additions & 1 deletion test/ProblemDetailsResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use Zend\ProblemDetails\Exception\InvalidResponseBodyException;
use Zend\ProblemDetails\Exception\ProblemDetailsExceptionInterface;
use Zend\ProblemDetails\ProblemDetailsResponseFactory;

use function array_keys;
use function fclose;
use function fopen;
use function stripos;

class ProblemDetailsResponseFactoryTest extends TestCase
{
use ProblemDetailsAssertionsTrait;
Expand Down