You are here

public function EntityOperationsTest::testEntityOperation in Hook Event Dispatcher 8

EntityOperationEvent test.

File

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

Class

EntityOperationsTest
Class EntityOperationsTest.

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Entity

Code

public function testEntityOperation() {
  $entity = $this
    ->createMock(EntityInterface::class);
  $operations = [
    'test' => [
      'title' => 'new',
    ],
  ];
  $extraOperation = [
    'title' => 'extra',
  ];
  $expectedOperations = $operations + [
    'extra' => $extraOperation,
  ];
  $this->manager
    ->setEventCallbacks([
    HookEventDispatcherInterface::ENTITY_OPERATION => static function (EntityOperationEvent $event) use ($operations, $extraOperation) {
      $event
        ->setOperations($operations);
      $event
        ->addOperation('extra', $extraOperation);
    },
  ]);
  $result = hook_event_dispatcher_entity_operation($entity);

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