class ViewEventTest in Hook Event Dispatcher 8
Class ViewEventTest.
@package Drupal\Tests\hook_event_dispatcher\Unit\Views
@group hook_event_dispatcher
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\hook_event_dispatcher\Unit\Views\ViewEventTest
Expanded class hierarchy of ViewEventTest
File
- tests/
src/ Unit/ Views/ ViewEventTest.php, line 23
Namespace
Drupal\Tests\hook_event_dispatcher\Unit\ViewsView source
class ViewEventTest 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);
}
/**
* Pre view event.
*/
public function testPreViewEvent() {
/** @var \Drupal\views\ViewExecutable $view */
$view = $this
->createMock(ViewExecutable::class);
$displayId = 'test';
$arguments = [
'test',
];
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::VIEWS_PRE_VIEW => function (ViewsPreViewEvent $event) {
$arguments =& $event
->getArguments();
$arguments[0] = 'test2';
},
]);
hook_event_dispatcher_views_pre_view($view, $displayId, $arguments);
self::assertEquals('test2', $arguments[0]);
}
/**
* Pre build event.
*/
public function testPreBuildEvent() {
/** @var \Drupal\views\ViewExecutable $view */
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_pre_build($view);
/** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_PRE_BUILD);
self::assertEquals($view, $event
->getView());
}
/**
* Post build event.
*/
public function testPostBuildEvent() {
/** @var \Drupal\views\ViewExecutable $view */
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_post_build($view);
/** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_POST_BUILD);
self::assertEquals($view, $event
->getView());
}
/**
* Pre execute event.
*/
public function testPreExecuteEvent() {
/** @var \Drupal\views\ViewExecutable $view */
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_pre_execute($view);
/** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_PRE_EXECUTE);
self::assertEquals($view, $event
->getView());
}
/**
* Post execute event.
*/
public function testPostExecuteEvent() {
/** @var \Drupal\views\ViewExecutable $view */
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_post_execute($view);
/** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_POST_EXECUTE);
self::assertEquals($view, $event
->getView());
}
/**
* Pre render event.
*/
public function testPreRender() {
/** @var \Drupal\views\ViewExecutable $view */
$view = $this
->createMock(ViewExecutable::class);
hook_event_dispatcher_views_pre_render($view);
/** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_PRE_RENDER);
self::assertEquals($view, $event
->getView());
}
/**
* Post render event.
*/
public function testPostRenderEvent() {
/** @var \Drupal\views\ViewExecutable $view */
$view = $this
->createMock(ViewExecutable::class);
$output = "<h1>test</h1>";
/** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
$cache = $this
->createMock(CachePluginBase::class);
$cache->options['results_lifespan'] = 0;
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::VIEWS_POST_RENDER => function (ViewsPostRenderEvent $event) {
$output =& $event
->getOutput();
$output = "<h2>Test</h2>";
$cache = $event
->getCache();
$cache->options['results_lifespan'] = 10;
},
]);
hook_event_dispatcher_views_post_render($view, $output, $cache);
self::assertEquals("<h2>Test</h2>", $output);
self::assertEquals(10, $cache->options['results_lifespan']);
}
/**
* Query alter event.
*/
public function testQueryAlterEvent() {
/** @var \Drupal\views\ViewExecutable $view */
$view = $this
->createMock(ViewExecutable::class);
/** @var \Drupal\views\Plugin\views\query\QueryPluginBase $query */
$query = $this
->createMock(QueryPluginBase::class);
hook_event_dispatcher_views_query_alter($view, $query);
/** @var \Drupal\hook_event_dispatcher\Event\Views\ViewsQueryAlterEvent $event */
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::VIEWS_QUERY_ALTER);
self::assertSame($view, $event
->getView());
self::assertSame($query, $event
->getQuery());
}
/**
* Query substitutions event.
*/
public function testQuerySubstitions() {
/** @var \Drupal\views\ViewExecutable $view */
$view = $this
->createMock(ViewExecutable::class);
$expected = [
"test" => 1,
];
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::VIEWS_QUERY_SUBSTITUTIONS => function (ViewsQuerySubstitutionsEvent $event) use ($expected) {
$event
->setSubstitutions($expected);
},
]);
$result = hook_event_dispatcher_views_query_substitutions($view);
self::assertSame($expected, $result);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
ViewEventTest:: |
private | property | The manager. | |
ViewEventTest:: |
public | function |
Overrides UnitTestCase:: |
|
ViewEventTest:: |
public | function | Post build event. | |
ViewEventTest:: |
public | function | Post execute event. | |
ViewEventTest:: |
public | function | Post render event. | |
ViewEventTest:: |
public | function | Pre build event. | |
ViewEventTest:: |
public | function | Pre execute event. | |
ViewEventTest:: |
public | function | Pre render event. | |
ViewEventTest:: |
public | function | Pre view event. | |
ViewEventTest:: |
public | function | Query alter event. | |
ViewEventTest:: |
public | function | Query substitutions event. |