You are here

class EdgeExceptionSubscriberTest in Apigee Edge 8

Test EdgeExceptionSubscriber.

@group apigee_edge

Hierarchy

Expanded class hierarchy of EdgeExceptionSubscriberTest

File

tests/src/Unit/EventSubscriber/EdgeExceptionSubscriberTest.php, line 43

Namespace

Drupal\Tests\apigee_edge\Unit\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
EdgeExceptionSubscriberTest::$classResolver protected property Class Resolver service.
EdgeExceptionSubscriberTest::$configFactory protected property The configuration factory.
EdgeExceptionSubscriberTest::$exception protected property The API Exception class.
EdgeExceptionSubscriberTest::$getResponseForExceptionEvent protected property The getResponseForException mock.
EdgeExceptionSubscriberTest::$logger protected property The logger mock.
EdgeExceptionSubscriberTest::$mainContentRenderers protected property The available main content renderer services, keyed per format.
EdgeExceptionSubscriberTest::$messenger protected property The messenger mock.
EdgeExceptionSubscriberTest::$routeMatch protected property The current route match.
EdgeExceptionSubscriberTest::setUp protected function Overrides UnitTestCase::setUp
EdgeExceptionSubscriberTest::testOnExceptionErrorsOff public function Test OnExceptionErrors method will not show errors.
EdgeExceptionSubscriberTest::testOnExceptionErrorsOn public function Test OnException method will show errors.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.