You are here

public function EntityOperationsTest::testEntityOperation in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/core_event_dispatcher/tests/src/Unit/Entity/EntityOperationsTest.php \Drupal\Tests\core_event_dispatcher\Unit\Entity\EntityOperationsTest::testEntityOperation()

EntityOperationEvent test.

File

modules/core_event_dispatcher/tests/src/Unit/Entity/EntityOperationsTest.php, line 44

Class

EntityOperationsTest
Class EntityOperationsTest.

Namespace

Drupal\Tests\core_event_dispatcher\Unit\Entity

Code

public function testEntityOperation() : void {
  $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 = core_event_dispatcher_entity_operation($entity);

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