You are here

public function PageAttachmentsEventTest::testPageAttachmentsByReference in Hook Event Dispatcher 8

Test the PageAttachmentsEvent by reference.

File

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

Class

PageAttachmentsEventTest
Class PageAttachmentsEventTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Page

Code

public function testPageAttachmentsByReference() {
  $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;
    },
  ]);
  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);
}