You are here

class ViewDataEventTest in Hook Event Dispatcher 8

Class ViewDataEventTest.

@package Drupal\Tests\hook_event_dispatcher\Unit\Views

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of ViewDataEventTest

File

tests/src/Unit/Views/ViewDataEventTest.php, line 19

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Views
View 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

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
ViewDataEventTest::$manager private property The manager.
ViewDataEventTest::setUp public function Overrides UnitTestCase::setUp
ViewDataEventTest::testViewsDataAlterEventByReference public function ViewsDataAlterEvent by reference test.
ViewDataEventTest::testViewsDataAlterEventBySet public function ViewsDataAlterEvent by set test.
ViewDataEventTest::testViewsDataEvent public function ViewsDataEvent test.
ViewDataEventTest::testViewsDataEventMultipleAdds public function ViewsDataEvent multiple adds test.