class ViewDataEventTest in Hook Event Dispatcher 8
Class ViewDataEventTest.
@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\ViewDataEventTest
Expanded class hierarchy of ViewDataEventTest
File
- tests/
src/ Unit/ Views/ ViewDataEventTest.php, line 19
Namespace
Drupal\Tests\hook_event_dispatcher\Unit\ViewsView source
class ViewDataEventTest 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);
}
/**
* ViewsDataEvent test.
*/
public function testViewsDataEvent() {
$data = [
'test' => [
'test_array_data',
],
];
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::VIEWS_DATA => function (ViewsDataEvent $event) use ($data) {
$event
->addData($data);
},
]);
$result = hook_event_dispatcher_views_data();
self::assertSame($data, $result);
}
/**
* ViewsDataEvent multiple adds test.
*/
public function testViewsDataEventMultipleAdds() {
$data1 = [
'test' => [
'test_array_data',
],
];
$data2 = [
'test' => [
'other_test_array_data',
],
];
$data3 = [
'some' => [
'other_data',
],
];
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::VIEWS_DATA => function (ViewsDataEvent $event) use ($data1, $data2, $data3) {
$event
->addData($data1);
$event
->addData($data2);
$event
->addData($data3);
},
]);
$result = hook_event_dispatcher_views_data();
$expectedResult = [
'test' => [
'test_array_data',
'other_test_array_data',
],
'some' => [
'other_data',
],
];
self::assertSame($expectedResult, $result);
}
/**
* ViewsDataAlterEvent by reference test.
*/
public function testViewsDataAlterEventByReference() {
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::VIEWS_DATA_ALTER => function (ViewsDataAlterEvent $event) {
$data =& $event
->getData();
$data['test']['other_test'] = [
'some_data',
];
},
]);
$data = $expectedData = [
'test' => [
'test' => 'test_array_data',
],
];
hook_event_dispatcher_views_data_alter($data);
$expectedData['test']['other_test'] = [
'some_data',
];
self::assertSame($expectedData, $data);
}
/**
* ViewsDataAlterEvent by set test.
*/
public function testViewsDataAlterEventBySet() {
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::VIEWS_DATA_ALTER => function (ViewsDataAlterEvent $event) {
$data = $event
->getData();
$data['other'] = [
'other_data',
];
$event
->setData($data);
},
]);
$data = $expectedData = [
'test' => [
'test' => 'test_array_data',
],
];
hook_event_dispatcher_views_data_alter($data);
$expectedData['other'] = [
'other_data',
];
self::assertSame($expectedData, $data);
}
}
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. | |
ViewDataEventTest:: |
private | property | The manager. | |
ViewDataEventTest:: |
public | function |
Overrides UnitTestCase:: |
|
ViewDataEventTest:: |
public | function | ViewsDataAlterEvent by reference test. | |
ViewDataEventTest:: |
public | function | ViewsDataAlterEvent by set test. | |
ViewDataEventTest:: |
public | function | ViewsDataEvent test. | |
ViewDataEventTest:: |
public | function | ViewsDataEvent multiple adds test. |