You are here

class ViewDataEventTest in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/views_event_dispatcher/tests/src/Unit/Views/ViewDataEventTest.php \Drupal\Tests\views_event_dispatcher\Unit\Views\ViewDataEventTest

Class ViewDataEventTest.

@group views_event_dispatcher

Hierarchy

  • class \Drupal\Tests\views_event_dispatcher\Unit\Views\ViewDataEventTest extends \PHPUnit\Framework\TestCase

Expanded class hierarchy of ViewDataEventTest

File

modules/views_event_dispatcher/tests/src/Unit/Views/ViewDataEventTest.php, line 20

Namespace

Drupal\Tests\views_event_dispatcher\Unit\Views
View source
class ViewDataEventTest extends TestCase {

  /**
   * The manager.
   *
   * @var \Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy
   */
  private $manager;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $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() : void {
    $data = [
      'test' => [
        'test_array_data',
      ],
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::VIEWS_DATA => static function (ViewsDataEvent $event) use ($data) {
        $event
          ->addData($data);
      },
    ]);
    $result = views_event_dispatcher_views_data();
    self::assertSame($data, $result);
  }

  /**
   * ViewsDataEvent multiple adds test.
   */
  public function testViewsDataEventMultipleAdds() : void {
    $data1 = [
      'test' => [
        'test_array_data',
      ],
    ];
    $data2 = [
      'test' => [
        'other_test_array_data',
      ],
    ];
    $data3 = [
      'some' => [
        'other_data',
      ],
    ];
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::VIEWS_DATA => static function (ViewsDataEvent $event) use ($data1, $data2, $data3) {
        $event
          ->addData($data1);
        $event
          ->addData($data2);
        $event
          ->addData($data3);
      },
    ]);
    $result = views_event_dispatcher_views_data();
    $expectedResult = [
      'test' => [
        'test_array_data',
        'other_test_array_data',
      ],
      'some' => [
        'other_data',
      ],
    ];
    self::assertSame($expectedResult, $result);
  }

  /**
   * ViewsDataAlterEvent test.
   */
  public function testViewsDataAlterEvent() : void {
    $this->manager
      ->setEventCallbacks([
      HookEventDispatcherInterface::VIEWS_DATA_ALTER => static function (ViewsDataAlterEvent $event) {
        $data =& $event
          ->getData();
        $data['test']['other_test'] = [
          'some_data',
        ];
      },
    ]);
    $data = $expectedData = [
      'test' => [
        'test' => 'test_array_data',
      ],
    ];
    views_event_dispatcher_views_data_alter($data);
    $expectedData['test']['other_test'] = [
      'some_data',
    ];
    self::assertSame($expectedData, $data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ViewDataEventTest::$manager private property The manager.
ViewDataEventTest::setUp public function
ViewDataEventTest::testViewsDataAlterEvent public function ViewsDataAlterEvent test.
ViewDataEventTest::testViewsDataEvent public function ViewsDataEvent test.
ViewDataEventTest::testViewsDataEventMultipleAdds public function ViewsDataEvent multiple adds test.