You are here

class EntityEventTest in Hook Event Dispatcher 8

Same name in this branch
  1. 8 tests/src/Unit/Entity/EntityEventTest.php \Drupal\Tests\hook_event_dispatcher\Unit\Entity\EntityEventTest
  2. 8 tests/src/Unit/Preprocess/EntityEventTest.php \Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\EntityEventTest

Class EntityEventTest.

@package Drupal\Tests\hook_event_dispatcher\Unit\Entity

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of EntityEventTest

File

tests/src/Unit/Entity/EntityEventTest.php, line 18

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Entity
View source
class EntityEventTest extends UnitTestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $builder = new ContainerBuilder();
    $this->manager = new HookEventDispatcherManagerSpy();
    $builder
      ->set('hook_event_dispatcher.manager', $this->manager);
    $builder
      ->compile();
    \Drupal::setContainer($builder);
  }

  /**
   * Test EntityCreateEvent.
   */
  public function testEntityCreateEvent() {
    $entity = $this
      ->createMock(EntityInterface::class);
    hook_event_dispatcher_entity_create($entity);

    /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityCreateEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_CREATE);
    self::assertEquals($entity, $event
      ->getEntity());
  }

  /**
   * Test EntityDeleteEvent.
   */
  public function testEntityDeleteEvent() {
    $entity = $this
      ->createMock(EntityInterface::class);
    hook_event_dispatcher_entity_delete($entity);

    /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityDeleteEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_DELETE);
    self::assertEquals($entity, $event
      ->getEntity());
  }

  /**
   * Test EntityInsertEvent.
   */
  public function testEntityInsertEvent() {
    $entity = $this
      ->createMock(EntityInterface::class);
    hook_event_dispatcher_entity_insert($entity);

    /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityInsertEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_INSERT);
    self::assertEquals($entity, $event
      ->getEntity());
  }

  /**
   * Test EntityLoadEvent.
   */
  public function testEntityLoadEvent() {
    $entities = [
      $this
        ->createMock(EntityInterface::class),
      $this
        ->createMock(EntityInterface::class),
      $this
        ->createMock(EntityInterface::class),
    ];
    $entityTypeId = 'test';
    hook_event_dispatcher_entity_load($entities, $entityTypeId);

    /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityLoadEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_LOAD);
    self::assertEquals($entities, $event
      ->getEntities());
    self::assertEquals($entityTypeId, $event
      ->getEntityTypeId());
  }

  /**
   * Test EntityTranslationInsertEvent.
   */
  public function testEntityTranslationInsertEvent() {
    $entity = $this
      ->createMock(EntityInterface::class);
    hook_event_dispatcher_entity_translation_insert($entity);

    /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityTranslationInsertEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_TRANSLATION_INSERT);
    self::assertEquals($entity, $event
      ->getEntity());
  }

  /**
   * Test EntityTranslationDeleteEvent.
   */
  public function testEntityTranslationDeleteEvent() {
    $entity = $this
      ->createMock(EntityInterface::class);
    hook_event_dispatcher_entity_translation_delete($entity);

    /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityTranslationDeleteEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_TRANSLATION_DELETE);
    self::assertEquals($entity, $event
      ->getEntity());
  }

  /**
   * Test EntityPredeleteEvent.
   */
  public function testEntityPredeleteEvent() {
    $entity = $this
      ->createMock(EntityInterface::class);
    hook_event_dispatcher_entity_predelete($entity);

    /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityPredeleteEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_PRE_DELETE);
    self::assertEquals($entity, $event
      ->getEntity());
  }

  /**
   * Test EntityPresaveEvent.
   */
  public function testEntityPresaveEvent() {
    $entity = $this
      ->createMock(EntityInterface::class);
    $originalEntity = $this
      ->createMock(EntityInterface::class);
    $entity->original = $originalEntity;
    hook_event_dispatcher_entity_presave($entity);

    /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityPresaveEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_PRE_SAVE);
    self::assertEquals($entity, $event
      ->getEntity());
    self::assertEquals($originalEntity, $event
      ->getOriginalEntity());
  }

  /**
   * Test EntityUpdateEvent.
   */
  public function testEntityUpdateEvent() {
    $entity = $this
      ->createMock(EntityInterface::class);
    $originalEntity = $this
      ->createMock(EntityInterface::class);
    $entity->original = $originalEntity;
    hook_event_dispatcher_entity_update($entity);

    /** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityUpdateEvent $event */
    $event = $this->manager
      ->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_UPDATE);
    self::assertEquals($entity, $event
      ->getEntity());
    self::assertEquals($originalEntity, $event
      ->getOriginalEntity());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityEventTest::$manager private property The manager.
EntityEventTest::setUp public function Overrides UnitTestCase::setUp
EntityEventTest::testEntityCreateEvent public function Test EntityCreateEvent.
EntityEventTest::testEntityDeleteEvent public function Test EntityDeleteEvent.
EntityEventTest::testEntityInsertEvent public function Test EntityInsertEvent.
EntityEventTest::testEntityLoadEvent public function Test EntityLoadEvent.
EntityEventTest::testEntityPredeleteEvent public function Test EntityPredeleteEvent.
EntityEventTest::testEntityPresaveEvent public function Test EntityPresaveEvent.
EntityEventTest::testEntityTranslationDeleteEvent public function Test EntityTranslationDeleteEvent.
EntityEventTest::testEntityTranslationInsertEvent public function Test EntityTranslationInsertEvent.
EntityEventTest::testEntityUpdateEvent public function Test EntityUpdateEvent.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.