You are here

class PageEventTest in Hook Event Dispatcher 3.x

Same name and namespace in other branches
  1. 8.2 modules/core_event_dispatcher/tests/src/Unit/Theme/PageEventTest.php \Drupal\Tests\hook_event_dispatcher\Unit\Page\PageEventTest

Class PageEventTest.

@group hook_event_dispatcher

Hierarchy

  • class \Drupal\Tests\hook_event_dispatcher\Unit\Page\PageEventTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of PageEventTest

File

modules/core_event_dispatcher/tests/src/Unit/Theme/PageEventTest.php, line 22

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Page
View source
class PageEventTest extends TestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * Sets up the test.
   */
  public function setUp() : void {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    Drupal::setContainer($builder);
  }

  /**
   * Test the PageAttachmentsEvent.
   */
  public function testPageAttachments() : void {
    $currentAttachments = [];
    $currentAttachments['current']['#attached']['library'] = [
      'current/current',
    ];
    $testAttachment = [];
    $testAttachment['#attached']['library'] = [
      'test/test',
    ];
    $expectedAttachments = $currentAttachments;
    $expectedAttachments['new'] = $testAttachment;
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::PAGE_ATTACHMENTS => static function (PageAttachmentsEvent $event) use ($testAttachment) {
        $eventAttachments =& $event
          ->getAttachments();
        $eventAttachments['new'] = $testAttachment;
      },
    ]);
    core_event_dispatcher_page_attachments($currentAttachments);

    /** @var \Drupal\core_event_dispatcher\Event\Theme\PageAttachmentsEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::PAGE_ATTACHMENTS);
    self::assertSame($expectedAttachments, $event
      ->getAttachments());
    self::assertSame($expectedAttachments, $currentAttachments);
  }

  /**
   * Test the PageBottomEvent.
   */
  public function testPageBottomEvent() : void {
    $pageBottom = [];
    $renderArray = [
      '#markup' => 'Bottom!',
    ];
    $expectedBuild = [
      'new' => $renderArray,
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::PAGE_BOTTOM => static function (PageBottomEvent $event) use ($renderArray) {
        $build =& $event
          ->getBuild();
        $build['new'] = $renderArray;
      },
    ]);
    core_event_dispatcher_page_bottom($pageBottom);

    /** @var \Drupal\core_event_dispatcher\Event\Theme\PageBottomEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::PAGE_BOTTOM);
    self::assertSame($expectedBuild, $pageBottom);
    self::assertSame($expectedBuild, $event
      ->getBuild());
  }

  /**
   * Test the PageTopEvent.
   */
  public function testPageTopEvent() : void {
    $pageTop = [];
    $renderArray = [
      '#markup' => 'Top!',
    ];
    $expectedBuild = [
      'new' => $renderArray,
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::PAGE_TOP => static function (PageTopEvent $event) use ($renderArray) {
        $build =& $event
          ->getBuild();
        $build['new'] = $renderArray;
      },
    ]);
    core_event_dispatcher_page_top($pageTop);

    /** @var \Drupal\core_event_dispatcher\Event\Theme\PageTopEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::PAGE_TOP);
    self::assertSame($expectedBuild, $pageTop);
    self::assertSame($expectedBuild, $event
      ->getBuild());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PageEventTest::$manager private property The manager.
PageEventTest::setUp public function Sets up the test.
PageEventTest::testPageAttachments public function Test the PageAttachmentsEvent.
PageEventTest::testPageBottomEvent public function Test the PageBottomEvent.
PageEventTest::testPageTopEvent public function Test the PageTopEvent.