You are here

public function PageAttachmentsEventTest::testPageAttachmentsBySet in Hook Event Dispatcher 8

Test the PageAttachmentsEvent by set.

File

tests/src/Unit/Page/PageAttachmentsEventTest.php, line 67

Class

PageAttachmentsEventTest
Class PageAttachmentsEventTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Page

Code

public function testPageAttachmentsBySet() {
  $currentAttachments = $testAttachment = [];
  $currentAttachments['current']['#attached']['library'] = [
    'current/current',
  ];
  $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;
      $event
        ->setAttachments($eventAttachments);
    },
  ]);
  hook_event_dispatcher_page_attachments($currentAttachments);

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