EdgeExceptionSubscriberTest.php in Apigee Edge 8
File
tests/src/Unit/EventSubscriber/EdgeExceptionSubscriberTest.php
View source
<?php
namespace Drupal\Tests\apigee_edge\Unit\EventSubscriber;
use Apigee\Edge\Exception\ApiException;
use Drupal\apigee_edge\Controller\ErrorPageController;
use Drupal\apigee_edge\EventSubscriber\EdgeExceptionSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\MainContent\HtmlRenderer;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Config\Config;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
class EdgeExceptionSubscriberTest extends UnitTestCase {
protected $exception;
protected $logger;
protected $configFactory;
protected $messenger;
protected $classResolver;
protected $routeMatch;
protected $mainContentRenderers;
protected $getResponseForExceptionEvent;
protected function setUp() {
parent::setUp();
$this->exception = new ApiException("API response error message.");
$this->logger = $this
->prophesize(LoggerInterface::class);
$this->messenger = $this
->prophesize(MessengerInterface::class);
$response = $this
->prophesize(Response::class);
$this->mainContentRenderers = [
'html' => 'main_content_renderer.html',
];
$htmlRenderer = $this
->prophesize(HtmlRenderer::class);
$htmlRenderer
->renderResponse(Argument::cetera())
->willReturn($response
->reveal());
$errorPageController = $this
->prophesize(ErrorPageController::class);
$errorPageController
->render()
->willReturn([]);
$errorPageController
->getPageTitle()
->willReturn('');
$this->classResolver = $this
->prophesize(ClassResolverInterface::class);
$this->classResolver
->getInstanceFromDefinition(Argument::is($this->mainContentRenderers['html']))
->willReturn($htmlRenderer
->reveal());
$this->classResolver
->getInstanceFromDefinition(Argument::is(ErrorPageController::class))
->willReturn($errorPageController
->reveal());
$this->routeMatch = $this
->prophesize(RouteMatchInterface::class);
if (class_exists('\\Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent')) {
$this->getResponseForExceptionEvent = $this
->prophesize('\\Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent');
$this->getResponseForExceptionEvent
->getThrowable()
->willReturn($this->exception);
}
else {
$this->getResponseForExceptionEvent = $this
->prophesize(GetResponseForExceptionEvent::class);
$this->getResponseForExceptionEvent
->getException()
->willReturn($this->exception);
}
$this->getResponseForExceptionEvent
->getRequest()
->willReturn(new Request());
$this->getResponseForExceptionEvent
->setResponse(Argument::any())
->willReturn();
}
public function testOnExceptionErrorsOn() {
$config_error_page = $this
->prophesize(Config::class);
$config_error_page
->get(Argument::is('error_page_debug_messages'))
->shouldBeCalledTimes(1)
->willReturn(TRUE);
$this->configFactory = $this
->prophesize(ConfigFactoryInterface::class);
$this->configFactory
->get(Argument::is('apigee_edge.error_page'))
->willReturn($config_error_page
->reveal());
$this->messenger
->addError(Argument::is($this->exception
->getMessage()))
->shouldBeCalledTimes(1);
$edge_exception_subscriber = new EdgeExceptionSubscriber($this->logger
->reveal(), $this->configFactory
->reveal(), $this->messenger
->reveal(), $this->classResolver
->reveal(), $this->routeMatch
->reveal(), $this->mainContentRenderers);
$edge_exception_subscriber
->onException($this->getResponseForExceptionEvent
->reveal());
}
public function testOnExceptionErrorsOff() {
$config_error_page = $this
->prophesize(Config::class);
$config_error_page
->get(Argument::is('error_page_debug_messages'))
->shouldBeCalledTimes(1)
->willReturn(FALSE);
$this->configFactory = $this
->prophesize(ConfigFactoryInterface::class);
$this->configFactory
->get(Argument::is('apigee_edge.error_page'))
->willReturn($config_error_page
->reveal());
$this->messenger
->addError(Argument::type('string'))
->shouldNotBeCalled();
$edge_exception_subscriber = new EdgeExceptionSubscriber($this->logger
->reveal(), $this->configFactory
->reveal(), $this->messenger
->reveal(), $this->classResolver
->reveal(), $this->routeMatch
->reveal(), $this->mainContentRenderers);
$edge_exception_subscriber
->onException($this->getResponseForExceptionEvent
->reveal());
}
}