class EdgeExceptionSubscriberTest in Apigee Edge 8
Test EdgeExceptionSubscriber.
@group apigee_edge
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\apigee_edge\Unit\EventSubscriber\EdgeExceptionSubscriberTest
Expanded class hierarchy of EdgeExceptionSubscriberTest
File
- tests/
src/ Unit/ EventSubscriber/ EdgeExceptionSubscriberTest.php, line 43
Namespace
Drupal\Tests\apigee_edge\Unit\EventSubscriberView source
class EdgeExceptionSubscriberTest extends UnitTestCase {
/**
* The API Exception class.
*
* @var \Apigee\Edge\Exception\ApiException
*/
protected $exception;
/**
* The logger mock.
*
* @var \Prophecy\Prophecy\ObjectProphecy
*/
protected $logger;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\Config
*/
protected $configFactory;
/**
* The messenger mock.
*
* @var \Prophecy\Prophecy\ObjectProphecy
*/
protected $messenger;
/**
* Class Resolver service.
*
* @var \Drupal\Core\DependencyInjection\ClassResolverInterface
*/
protected $classResolver;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The available main content renderer services, keyed per format.
*
* @var array
*/
protected $mainContentRenderers;
/**
* The getResponseForException mock.
*
* @var \Prophecy\Prophecy\ObjectProphecy
*/
protected $getResponseForExceptionEvent;
/**
* {@inheritdoc}
*/
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);
// Drupal 9 / Symfony 4.x and up.
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();
}
/**
* Test OnException method will show errors.
*
* When error_page_debug_messages config is set, the exception message
* should be displayed.
*/
public function testOnExceptionErrorsOn() {
// Config will return true when checked.
$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());
// Error should be displayed since show debug messages config is on.
$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());
}
/**
* Test OnExceptionErrors method will not show errors.
*
* When error_page_debug_messages config is FALSE, the exception message
* should be not be displayed.
*/
public function testOnExceptionErrorsOff() {
// Config will return false when checked.
$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());
// Messenger should not be adding error since show debug messages is false.
$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());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EdgeExceptionSubscriberTest:: |
protected | property | Class Resolver service. | |
EdgeExceptionSubscriberTest:: |
protected | property | The configuration factory. | |
EdgeExceptionSubscriberTest:: |
protected | property | The API Exception class. | |
EdgeExceptionSubscriberTest:: |
protected | property | The getResponseForException mock. | |
EdgeExceptionSubscriberTest:: |
protected | property | The logger mock. | |
EdgeExceptionSubscriberTest:: |
protected | property | The available main content renderer services, keyed per format. | |
EdgeExceptionSubscriberTest:: |
protected | property | The messenger mock. | |
EdgeExceptionSubscriberTest:: |
protected | property | The current route match. | |
EdgeExceptionSubscriberTest:: |
protected | function |
Overrides UnitTestCase:: |
|
EdgeExceptionSubscriberTest:: |
public | function | Test OnExceptionErrors method will not show errors. | |
EdgeExceptionSubscriberTest:: |
public | function | Test OnException method will show errors. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |