You are here

public function EntityOperationsTest::testEntityOperationAlter in Hook Event Dispatcher 8

EntityOperationAlterEvent test.

File

tests/src/Unit/Entity/EntityOperationsTest.php, line 74

Class

EntityOperationsTest
Class EntityOperationsTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Entity

Code

public function testEntityOperationAlter() {
  $entity = $this
    ->createMock(EntityInterface::class);
  $operations = [
    'my_opt' => [
      'title' => 'existing_ops',
    ],
  ];
  $extraOperation = [
    'title' => 'extra',
  ];
  $expectedOperations = $operations + [
    'extra' => $extraOperation,
  ];
  $expectedOperations['my_opt']['title'] = 'changed!';
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::ENTITY_OPERATION_ALTER => static function (EntityOperationAlterEvent $event) use ($extraOperation) {
      $operations =& $event
        ->getOperations();
      $operations['my_opt']['title'] = 'changed!';
      $operations['extra'] = $extraOperation;
    },
  ]);
  hook_event_dispatcher_entity_operation_alter($operations, $entity);

  /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityOperationAlterEvent $event */
  $event = $this->manager
    ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_OPERATION_ALTER);
  self::assertSame($entity, $event
    ->getEntity());
  self::assertSame($expectedOperations, $event
    ->getOperations());
  self::assertSame($expectedOperations, $operations);
}