EntityEventTest.php in Hook Event Dispatcher 8
File
tests/src/Unit/Entity/EntityEventTest.php
View source
<?php
namespace Drupal\Tests\hook_event_dispatcher\Unit\Entity;
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 Drupal\Tests\UnitTestCase;
class EntityEventTest extends UnitTestCase {
private $manager;
public function setUp() {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
\Drupal::setContainer($builder);
}
public function testEntityCreateEvent() {
$entity = $this
->createMock(EntityInterface::class);
hook_event_dispatcher_entity_create($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_CREATE);
self::assertEquals($entity, $event
->getEntity());
}
public function testEntityDeleteEvent() {
$entity = $this
->createMock(EntityInterface::class);
hook_event_dispatcher_entity_delete($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_DELETE);
self::assertEquals($entity, $event
->getEntity());
}
public function testEntityInsertEvent() {
$entity = $this
->createMock(EntityInterface::class);
hook_event_dispatcher_entity_insert($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_INSERT);
self::assertEquals($entity, $event
->getEntity());
}
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);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_LOAD);
self::assertEquals($entities, $event
->getEntities());
self::assertEquals($entityTypeId, $event
->getEntityTypeId());
}
public function testEntityTranslationInsertEvent() {
$entity = $this
->createMock(EntityInterface::class);
hook_event_dispatcher_entity_translation_insert($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_TRANSLATION_INSERT);
self::assertEquals($entity, $event
->getEntity());
}
public function testEntityTranslationDeleteEvent() {
$entity = $this
->createMock(EntityInterface::class);
hook_event_dispatcher_entity_translation_delete($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_TRANSLATION_DELETE);
self::assertEquals($entity, $event
->getEntity());
}
public function testEntityPredeleteEvent() {
$entity = $this
->createMock(EntityInterface::class);
hook_event_dispatcher_entity_predelete($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_PRE_DELETE);
self::assertEquals($entity, $event
->getEntity());
}
public function testEntityPresaveEvent() {
$entity = $this
->createMock(EntityInterface::class);
$originalEntity = $this
->createMock(EntityInterface::class);
$entity->original = $originalEntity;
hook_event_dispatcher_entity_presave($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_PRE_SAVE);
self::assertEquals($entity, $event
->getEntity());
self::assertEquals($originalEntity, $event
->getOriginalEntity());
}
public function testEntityUpdateEvent() {
$entity = $this
->createMock(EntityInterface::class);
$originalEntity = $this
->createMock(EntityInterface::class);
$entity->original = $originalEntity;
hook_event_dispatcher_entity_update($entity);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_UPDATE);
self::assertEquals($entity, $event
->getEntity());
self::assertEquals($originalEntity, $event
->getOriginalEntity());
}
}