final class EntityViewEventTest in Hook Event Dispatcher 8
Class EntityViewEventTest.
@package Drupal\Tests\hook_event_dispatcher\Unit\Entity
@group hook_event_dispatcher
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\hook_event_dispatcher\Unit\Entity\EntityViewEventTest
Expanded class hierarchy of EntityViewEventTest
File
- tests/
src/ Unit/ Entity/ EntityViewEventTest.php, line 25
Namespace
Drupal\Tests\hook_event_dispatcher\Unit\EntityView source
final class EntityViewEventTest 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 EntityViewEvent by reference.
*/
public function testEntityViewEventByReference() {
$build = $expectedBuild = [
'testBuild' => [
'someBuild',
],
];
$entity = $this
->createMock(EntityInterface::class);
$display = $this
->createMock(EntityViewDisplayInterface::class);
$viewMode = 'testViewMode';
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_VIEW => static function (EntityViewEvent $event) {
$event
->getBuild()['otherBuild'] = [
'aBuild',
];
},
]);
$expectedBuild['otherBuild'] = [
'aBuild',
];
hook_event_dispatcher_entity_view($build, $entity, $display, $viewMode);
/** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityViewEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_VIEW);
self::assertSame($build, $event
->getBuild());
self::assertSame($expectedBuild, $event
->getBuild());
self::assertSame($entity, $event
->getEntity());
self::assertSame($display, $event
->getDisplay());
self::assertSame($viewMode, $event
->getViewMode());
}
/**
* Test EntityViewEvent by set.
*/
public function testEntityViewEventBySet() {
$build = [
'testBuild' => [
'someBuild',
],
];
$otherBuild = [
'otherBuild' => [
'lalala',
],
];
$entity = $this
->createMock(EntityInterface::class);
$display = $this
->createMock(EntityViewDisplayInterface::class);
$viewMode = 'testViewMode';
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_VIEW => static function (EntityViewEvent $event) use ($otherBuild) {
$event
->setBuild($otherBuild);
},
]);
hook_event_dispatcher_entity_view($build, $entity, $display, $viewMode);
/** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityViewEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_VIEW);
self::assertSame($build, $event
->getBuild());
self::assertSame($otherBuild, $event
->getBuild());
self::assertSame($entity, $event
->getEntity());
self::assertSame($display, $event
->getDisplay());
self::assertSame($viewMode, $event
->getViewMode());
}
/**
* Test EntityViewAlterEvent.
*/
public function testEntityViewAlterEvent() {
$build = $expectedBuild = [
'testBuild' => [
'someBuild',
],
];
$entity = $this
->createMock(EntityInterface::class);
$display = $this
->createMock(EntityViewDisplayInterface::class);
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_VIEW_ALTER => static function (EntityViewAlterEvent $event) {
$event
->getBuild()['otherBuild'] = [
'aBuild',
];
},
]);
$expectedBuild['otherBuild'] = [
'aBuild',
];
hook_event_dispatcher_entity_view_alter($build, $entity, $display);
/** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityViewAlterEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_VIEW_ALTER);
self::assertSame($build, $event
->getBuild());
self::assertSame($expectedBuild, $event
->getBuild());
self::assertSame($entity, $event
->getEntity());
self::assertSame($display, $event
->getDisplay());
}
/**
* Test EntityBuildDefaultsAlter.
*/
public function testEntityBuildDefaultsAlter() {
$build = $expectedBuild = [
'testBuild' => [
'someBuild',
],
];
$entity = $this
->createMock(EntityInterface::class);
$viewMode = 'entity_view_mode';
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::ENTITY_BUILD_DEFAULTS_ALTER => static function (EntityBuildDefaultsAlterEvent $event) {
$event
->getBuild()['otherBuild'] = [
'aBuild',
];
},
]);
$expectedBuild['otherBuild'] = [
'aBuild',
];
hook_event_dispatcher_entity_build_defaults_alter($build, $entity, $viewMode);
/** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityBuildDefaultsAlterEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_BUILD_DEFAULTS_ALTER);
self::assertSame($build, $event
->getBuild());
self::assertSame($expectedBuild, $event
->getBuild());
self::assertSame($entity, $event
->getEntity());
self::assertSame($viewMode, $event
->getViewMode());
}
/**
* Test EntityBuildDefaultsAlter.
*/
public function testEntityBuildDefaultsAlterWithNullViewMode() {
$build = [
'testBuild' => [
'someBuild',
],
];
$entity = $this
->createMock(EntityInterface::class);
$viewMode = NULL;
hook_event_dispatcher_entity_build_defaults_alter($build, $entity, $viewMode);
/** @var \Drupal\hook_event_dispatcher\Event\Entity\EntityBuildDefaultsAlterEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::ENTITY_BUILD_DEFAULTS_ALTER);
self::assertSame($build, $event
->getBuild());
self::assertSame($entity, $event
->getEntity());
self::assertSame((string) $viewMode, $event
->getViewMode());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityViewEventTest:: |
private | property | The manager. | |
EntityViewEventTest:: |
public | function |
Overrides UnitTestCase:: |
|
EntityViewEventTest:: |
public | function | Test EntityBuildDefaultsAlter. | |
EntityViewEventTest:: |
public | function | Test EntityBuildDefaultsAlter. | |
EntityViewEventTest:: |
public | function | Test EntityViewAlterEvent. | |
EntityViewEventTest:: |
public | function | Test EntityViewEvent by reference. | |
EntityViewEventTest:: |
public | function | Test EntityViewEvent by set. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |