diff --git a/Slim/DeferredCallable.php b/Slim/DeferredCallable.php deleted file mode 100644 index 1fb899d03..000000000 --- a/Slim/DeferredCallable.php +++ /dev/null @@ -1,46 +0,0 @@ -callable = $callable; - $this->callableResolver = $resolver; - } - - public function __invoke(...$args) - { - /** @var callable $callable */ - $callable = $this->callable; - if ($this->callableResolver) { - $callable = $this->callableResolver->resolve($callable); - } - - return $callable(...$args); - } -} diff --git a/Slim/Handlers/ErrorHandler.php b/Slim/Handlers/ErrorHandler.php index f1975b533..8ac1755df 100644 --- a/Slim/Handlers/ErrorHandler.php +++ b/Slim/Handlers/ErrorHandler.php @@ -42,6 +42,11 @@ class ErrorHandler implements ErrorHandlerInterface */ protected $defaultErrorRenderer = HtmlErrorRenderer::class; + /** + * @var ErrorRendererInterface|string|callable + */ + protected $logErrorRenderer = PlainTextErrorRenderer::class; + /** * @var array */ @@ -259,6 +264,16 @@ public function setDefaultErrorRenderer(string $contentType, $errorRenderer): vo $this->defaultErrorRenderer = $errorRenderer; } + /** + * Set the renderer for the error logger + * + * @param ErrorRendererInterface|string|callable $logErrorRenderer + */ + public function setLogErrorRenderer($logErrorRenderer): void + { + $this->logErrorRenderer = $logErrorRenderer; + } + /** * Write to the error log if $logErrors has been set to true * @@ -266,8 +281,8 @@ public function setDefaultErrorRenderer(string $contentType, $errorRenderer): vo */ protected function writeToErrorLog(): void { - $renderer = new PlainTextErrorRenderer(); - $error = $renderer->__invoke($this->exception, $this->logErrorDetails); + $renderer = $this->callableResolver->resolve($this->logErrorRenderer); + $error = $renderer($this->exception, $this->logErrorDetails); $error .= "\nView in rendered output by enabling the \"displayErrorDetails\" setting.\n"; $this->logError($error); } diff --git a/tests/DeferredCallableTest.php b/tests/DeferredCallableTest.php deleted file mode 100644 index e0bd4d0d6..000000000 --- a/tests/DeferredCallableTest.php +++ /dev/null @@ -1,71 +0,0 @@ -prophesize(ContainerInterface::class); - $containerProphecy->has('CallableTest')->willReturn(true); - $containerProphecy->get('CallableTest')->willReturn(new CallableTest()); - $resolver = new CallableResolver($containerProphecy->reveal()); - - $deferred = new DeferredCallable('CallableTest:toCall', $resolver); - $deferred(); - - $this->assertEquals(1, CallableTest::$CalledCount); - } - - public function testItBindsClosuresToContainer() - { - $containerProphecy = $this->prophesize(ContainerInterface::class); - $resolver = new CallableResolver($containerProphecy->reveal()); - - $assertCalled = $this->getMockBuilder('StdClass')->setMethods(['foo'])->getMock(); - $assertCalled - ->expects($this->once()) - ->method('foo'); - - $test = $this; - $closure = function () use ($containerProphecy, $test, $assertCalled) { - $assertCalled->foo(); - $test->assertSame($containerProphecy->reveal(), $this); - }; - - $deferred = new DeferredCallable($closure, $resolver); - $deferred(); - } - - public function testItReturnsInvokedCallableResponse() - { - $containerProphecy = $this->prophesize(ContainerInterface::class); - $resolver = new CallableResolver($containerProphecy->reveal()); - - $test = $this; - $foo = 'foo'; - $bar = 'bar'; - - $closure = function ($param) use ($test, $foo, $bar) { - $test->assertEquals($foo, $param); - return $bar; - }; - - $deferred = new DeferredCallable($closure, $resolver); - - $response = $deferred($foo); - $this->assertEquals($bar, $response); - } -} diff --git a/tests/Handlers/ErrorHandlerTest.php b/tests/Handlers/ErrorHandlerTest.php index df5d897df..8993cc3ab 100644 --- a/tests/Handlers/ErrorHandlerTest.php +++ b/tests/Handlers/ErrorHandlerTest.php @@ -11,6 +11,9 @@ 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; @@ -18,6 +21,7 @@ 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; @@ -322,7 +326,7 @@ 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); @@ -330,4 +334,29 @@ public function testDefaultErrorRenderer() $this->assertTrue($res->hasHeader('Content-Type')); $this->assertEquals('text/html', $res->getHeaderLine('Content-Type')); } + + public function testLogErrorRenderer() + { + $renderer = function () { + return ''; + }; + + $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); + } } diff --git a/tests/Routing/RouteTest.php b/tests/Routing/RouteTest.php index 301b51625..a672b44a2 100644 --- a/tests/Routing/RouteTest.php +++ b/tests/Routing/RouteTest.php @@ -17,10 +17,8 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; -use Psr\Http\Message\UriInterface; use Psr\Http\Server\RequestHandlerInterface; use Slim\CallableResolver; -use Slim\DeferredCallable; use Slim\Handlers\Strategies\RequestHandler; use Slim\Handlers\Strategies\RequestResponse; use Slim\Interfaces\CallableResolverInterface; @@ -390,7 +388,7 @@ public function testControllerMethodAsStringResolvesWithoutContainer() $callableResolver = new CallableResolver(); $responseFactory = $this->getResponseFactory(); - $deferred = new DeferredCallable('\Slim\Tests\Mocks\CallableTest:toCall', $callableResolver); + $deferred = $callableResolver->resolve('\Slim\Tests\Mocks\CallableTest:toCall'); $route = new Route(['GET'], '/', $deferred, $responseFactory, $callableResolver); CallableTest::$CalledCount = 0; @@ -410,7 +408,6 @@ public function testControllerMethodAsStringResolvesWithContainer() $callableResolverProphecy = $this->prophesize(CallableResolverInterface::class); $callable = 'CallableTest:toCall'; - $deferred = new DeferredCallable($callable, $callableResolverProphecy->reveal()); $callableResolverProphecy ->resolve($callable) @@ -426,6 +423,7 @@ public function testControllerMethodAsStringResolvesWithContainer() }) ->shouldBeCalledOnce(); + $deferred = $callableResolverProphecy->reveal()->resolve($callable); $callableResolverProphecy ->resolve($deferred) ->willReturn($deferred)