Skip to content

Commit

Permalink
add test for setLogErrorRenderer method
Browse files Browse the repository at this point in the history
  • Loading branch information
l0gicgate committed Aug 18, 2019
1 parent eea0503 commit 9efd5cb
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion tests/Handlers/ErrorHandlerTest.php
Expand Up @@ -11,13 +11,17 @@

use Psr\Http\Message\ResponseInterface;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use RuntimeException;
use Slim\Error\Renderers\HtmlErrorRenderer;
use Slim\Error\Renderers\JsonErrorRenderer;
use Slim\Error\Renderers\PlainTextErrorRenderer;
use Slim\Error\Renderers\XmlErrorRenderer;
use Slim\Exception\HttpMethodNotAllowedException;
use Slim\Exception\HttpNotFoundException;
use Slim\Handlers\ErrorHandler;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Tests\Mocks\MockCustomException;
use Slim\Tests\TestCase;

Expand Down Expand Up @@ -322,12 +326,37 @@ public function testDefaultErrorRenderer()
->withHeader('Accept', 'application/unknown');

$handler = new ErrorHandler($this->getCallableResolver(), $this->getResponseFactory());
$exception = new \RuntimeException();
$exception = new RuntimeException();

/** @var ResponseInterface $res */
$res = $handler->__invoke($request, $exception, true, true, true);

$this->assertTrue($res->hasHeader('Content-Type'));
$this->assertEquals('text/html', $res->getHeaderLine('Content-Type'));
}

public function testLogErrorRenderer()
{
$renderer = function () {
return 'error';
};

$callableResolverProphecy = $this->prophesize(CallableResolverInterface::class);
$callableResolverProphecy
->resolve('logErrorRenderer')
->willReturn($renderer)
->shouldBeCalledOnce();

$handler = new ErrorHandler($callableResolverProphecy->reveal(), $this->getResponseFactory());
$handler->setLogErrorRenderer('logErrorRenderer');

$exception = new RuntimeException();
$exceptionProperty = new ReflectionProperty($handler, 'exception');
$exceptionProperty->setAccessible(true);
$exceptionProperty->setValue($handler, $exception);

$writeToErrorLogMethod = new ReflectionMethod($handler, 'writeToErrorLog');
$writeToErrorLogMethod->setAccessible(true);
$writeToErrorLogMethod->invoke($handler);
}
}

0 comments on commit 9efd5cb

Please sign in to comment.