EntityEventTest.php in Hook Event Dispatcher 3.x
File
modules/core_event_dispatcher/tests/src/Unit/Entity/EntityEventTest.php
View source
<?php
namespace Drupal\Tests\core_event_dispatcher\Unit\Entity;
use Drupal;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use PHPUnit\Framework\TestCase;
use function core_event_dispatcher_entity_create;
use function core_event_dispatcher_entity_delete;
use function core_event_dispatcher_entity_insert;
use function core_event_dispatcher_entity_load;
use function core_event_dispatcher_entity_predelete;
use function core_event_dispatcher_entity_presave;
use function core_event_dispatcher_entity_translation_delete;
use function core_event_dispatcher_entity_translation_insert;
use function core_event_dispatcher_entity_update;
class EntityEventTest extends TestCase {
private $manager;
public function setUp() : void {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
Drupal::setContainer($builder);
}
public function testEntityCreateEvent() : void {
$entity = $this
->createMock(EntityInterface::class);
core_event_dispatcher_entity_create($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_CREATE);
self::assertSame($entity, $event
->getEntity());
}
public function testEntityDeleteEvent() : void {
$entity = $this
->createMock(EntityInterface::class);
core_event_dispatcher_entity_delete($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_DELETE);
self::assertSame($entity, $event
->getEntity());
}
public function testEntityInsertEvent() : void {
$entity = $this
->createMock(EntityInterface::class);
core_event_dispatcher_entity_insert($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_INSERT);
self::assertSame($entity, $event
->getEntity());
}
public function testEntityLoadEvent() : void {
$entities = [
$this
->createMock(EntityInterface::class),
$this
->createMock(EntityInterface::class),
$this
->createMock(EntityInterface::class),
];
$entityTypeId = 'test';
core_event_dispatcher_entity_load($entities, $entityTypeId);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_LOAD);
self::assertSame($entities, $event
->getEntities());
self::assertSame($entityTypeId, $event
->getEntityTypeId());
}
public function testEntityTranslationInsertEvent() : void {
$entity = $this
->createMock(EntityInterface::class);
core_event_dispatcher_entity_translation_insert($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_TRANSLATION_INSERT);
self::assertSame($entity, $event
->getEntity());
}
public function testEntityTranslationDeleteEvent() : void {
$entity = $this
->createMock(EntityInterface::class);
core_event_dispatcher_entity_translation_delete($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_TRANSLATION_DELETE);
self::assertSame($entity, $event
->getEntity());
}
public function testEntityPredeleteEvent() : void {
$entity = $this
->createMock(EntityInterface::class);
core_event_dispatcher_entity_predelete($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_PRE_DELETE);
self::assertSame($entity, $event
->getEntity());
}
public function testEntityPresaveEvent() : void {
$entity = $this
->createMock(EntityInterface::class);
$originalEntity = $this
->createMock(EntityInterface::class);
$entity->original = $originalEntity;
core_event_dispatcher_entity_presave($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_PRE_SAVE);
self::assertSame($entity, $event
->getEntity());
self::assertSame($originalEntity, $event
->getOriginalEntity());
}
public function testEntityPresaveEventWithoutOriginal() : void {
$entity = $this
->createMock(EntityInterface::class);
core_event_dispatcher_entity_presave($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_PRE_SAVE);
self::assertSame($entity, $event
->getEntity());
self::assertNull($event
->getOriginalEntity());
}
public function testEntityUpdateEvent() : void {
$entity = $this
->createMock(EntityInterface::class);
$originalEntity = $this
->createMock(EntityInterface::class);
$entity->original = $originalEntity;
core_event_dispatcher_entity_update($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_UPDATE);
self::assertSame($entity, $event
->getEntity());
self::assertSame($originalEntity, $event
->getOriginalEntity());
}
}