final class EntityEventTest in Hook Event Dispatcher 8
Same name in this branch
- 8 tests/src/Unit/Entity/EntityEventTest.php \Drupal\Tests\hook_event_dispatcher\Unit\Entity\EntityEventTest
- 8 tests/src/Unit/Preprocess/EntityEventTest.php \Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\EntityEventTest
Class EntityEventTest.
@group hook_event_dispatcher
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\EntityEventTest
Expanded class hierarchy of EntityEventTest
File
- tests/
src/ Unit/ Preprocess/ EntityEventTest.php, line 22
Namespace
Drupal\Tests\hook_event_dispatcher\Unit\PreprocessView source
final class EntityEventTest extends UnitTestCase {
/**
* PreprocessEventService.
*
* @var \Drupal\hook_event_dispatcher\Service\PreprocessEventService
* PreprocessEventService.
*/
private $service;
/**
* SpyEventDispatcher.
*
* @var \Drupal\Tests\hook_event_dispatcher\Unit\Preprocess\Helpers\SpyEventDispatcher
* SpyEventDispatcher
*/
private $dispatcher;
/**
* Variables array.
*
* @var array
* Variables.
*/
private $variables;
/**
* {@inheritdoc}
*/
public function setUp() {
$loader = YamlDefinitionsLoader::getInstance();
$this->dispatcher = new SpyEventDispatcher();
$this->service = new PreprocessEventService($this->dispatcher, $loader
->getMapper());
$this->variables = [];
}
/**
* Test a BlockPreprocessEvent.
*/
public function testCommentEvent() {
$this->variables = [
'comment' => new EntityMock('comment', 'bundle', 'view_mode'),
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(CommentPreprocessEvent::class);
}
/**
* Test a EckEntityPreprocessEvent.
*/
public function testEckEntityEvent() {
$this->variables = [
'elements' => [
'#view_mode' => 'view_mode',
],
'eck_entity' => new EntityMock('eck_entity', 'bundle', 'view_mode'),
'theme_hook_original' => 'eck_entity',
'bundle' => 'bundle',
];
$this
->createAndAssertEntityEvent(EckEntityPreprocessEvent::class);
}
/**
* Test a NodePreprocessEvent.
*/
public function testNodeEvent() {
$this->variables = [
'node' => new EntityMock('node', 'bundle', 'view_mode'),
'theme_hook_original' => 'node',
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(NodePreprocessEvent::class);
}
/**
* Test a ParagraphPreprocessEvent.
*/
public function testParagraphEvent() {
$this->variables = [
'paragraph' => new EntityMock('paragraph', 'bundle', 'view_mode'),
'theme_hook_original' => 'paragraph',
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(ParagraphPreprocessEvent::class);
}
/**
* Test a TaxonomyTermPreprocessEvent.
*/
public function testTaxonomyTermEvent() {
$this->variables = [
'term' => new EntityMock('taxonomy_term', 'bundle', 'view_mode'),
'theme_hook_original' => 'taxonomy_term',
'view_mode' => 'view_mode',
];
$this
->createAndAssertEntityEvent(TaxonomyTermPreprocessEvent::class);
}
/**
* Create and assert the given entity event class.
*
* @param string $class
* Event class name.
*/
private function createAndAssertEntityEvent($class) {
$this->dispatcher
->setExpectedEventCount(3);
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\AbstractPreprocessEntityEvent $class */
$this->service
->createAndDispatchKnownEvents($class::getHook(), $this->variables);
/** @var \Drupal\hook_event_dispatcher\Event\Preprocess\AbstractPreprocessEntityEvent[] $events */
$events = $this->dispatcher
->getEvents();
$expectedName = $class::DISPATCH_NAME_PREFIX . $class::getHook();
$firstEvent = \reset($events);
$firstName = \key($events);
self::assertSame($expectedName, $firstName);
self::assertInstanceOf($class, $firstEvent);
self::assertInstanceOf(AbstractEventVariables::class, $firstEvent
->getVariables());
$secondEvent = \next($events);
$secondName = \key($events);
$bundle = $secondEvent
->getVariables()
->getEntityBundle();
self::assertNotNull($bundle);
self::assertInternalType('string', $bundle);
$expectedName .= '.' . $bundle;
self::assertSame($expectedName, $secondName);
self::assertInstanceOf($class, $secondEvent);
self::assertInstanceOf(AbstractEventVariables::class, $secondEvent
->getVariables());
$thirdEvent = \next($events);
$thirdName = \key($events);
$viewMode = $thirdEvent
->getVariables()
->getViewMode();
self::assertNotNull($viewMode);
self::assertInternalType('string', $viewMode);
$expectedName .= '.' . $viewMode;
self::assertSame($expectedName, $thirdName);
self::assertInstanceOf($class, $thirdEvent);
self::assertInstanceOf(AbstractEventVariables::class, $thirdEvent
->getVariables());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityEventTest:: |
private | property | SpyEventDispatcher. | |
EntityEventTest:: |
private | property | PreprocessEventService. | |
EntityEventTest:: |
private | property | Variables array. | |
EntityEventTest:: |
private | function | Create and assert the given entity event class. | |
EntityEventTest:: |
public | function |
Overrides UnitTestCase:: |
|
EntityEventTest:: |
public | function | Test a BlockPreprocessEvent. | |
EntityEventTest:: |
public | function | Test a EckEntityPreprocessEvent. | |
EntityEventTest:: |
public | function | Test a NodePreprocessEvent. | |
EntityEventTest:: |
public | function | Test a ParagraphPreprocessEvent. | |
EntityEventTest:: |
public | function | Test a TaxonomyTermPreprocessEvent. | |
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. |