You are here

final class OtherEventVariablesTest in Hook Event Dispatcher 8

Class OtherEventVariablesTest.

@group hook_event_dispatcher

Testing the other events gives expected PHPMD warnings.

Plugin annotation


@SuppressWarnings(PHPMD.CouplingBetweenObjects)
@SuppressWarnings(PHPMD.TooManyPublicMethods)

Hierarchy

Expanded class hierarchy of OtherEventVariablesTest

File

tests/src/Unit/Preprocess/OtherEventVariablesTest.php, line 42

Namespace

Drupal\Tests\hook_event_dispatcher\Unit\Preprocess
View source
final class OtherEventVariablesTest extends UnitTestCase {

  /**
   * Factory mapper.
   *
   * @var \Drupal\hook_event_dispatcher\Service\PreprocessEventFactoryMapper
   */
  private $mapper;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->mapper = YamlDefinitionsLoader::getInstance()
      ->getMapper();
  }

  /**
   * Test a BlockPreprocessEvent.
   */
  public function testBlockEvent() {
    $variablesArray = $this
      ->createVariablesArray();
    $variablesArray['block'] = 'block';
    $variablesArray['elements']['#id'] = 22;
    $variablesArray['content']['test'] = [
      'success2',
    ];

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\BlockEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(BlockPreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(BlockEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
    self::assertSame('block', $variables
      ->getBlock());
    self::assertEquals(22, $variables
      ->getId());
    self::assertEquals([
      'success2',
    ], $variables
      ->getContentChild('test'));
    self::assertEquals([], $variables
      ->getContentChild('none-existing'));
  }

  /**
   * Test a FieldPreprocessEvent.
   */
  public function testFieldEvent() {
    $variablesArray = $this
      ->createVariablesArray();
    $variablesArray['element'] = [
      'element array',
    ];
    $variablesArray['items'] = [
      'items array',
    ];

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\FieldEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(FieldPreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(FieldEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
    self::assertEquals([
      'element array',
    ], $variables
      ->getElement());
    self::assertEquals([
      'items array',
    ], $variables
      ->getItems());
  }

  /**
   * Test FormPreprocessEvent.
   */
  public function testFormEvent() {
    $variablesArray = $this
      ->createVariablesArray();
    $variablesArray['element'] = [
      'element array',
    ];

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\FormEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(FormPreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(FormEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
    self::assertEquals([
      'element array',
    ], $variables
      ->getElement());
  }

  /**
   * Test a HtmlPreprocessEvent.
   */
  public function testHtmlEvent() {
    $variablesArray = $this
      ->createVariablesArray();

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\HtmlEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(HtmlPreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(HtmlEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
  }

  /**
   * Test a ImagePreprocessEvent.
   */
  public function testImageEvent() {
    $variablesArray = $this
      ->createVariablesArray();

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\ImageEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(ImagePreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(ImageEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
  }

  /**
   * Test a PagePreprocessEvent.
   */
  public function testPageEvent() {
    $variablesArray = [];
    $variablesArray['page'] = $this
      ->createVariablesArray();

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\PageEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(PagePreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(PageEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
  }

  /**
   * Test a UsernamePreprocessEvent.
   */
  public function testUsernameEvent() {
    $variablesArray = $this
      ->createVariablesArray();
    $accountMock = $this
      ->getMockBuilder(UserInterface::class)
      ->disableOriginalClone()
      ->disableOriginalConstructor()
      ->setMethods([
      'isAnonymous',
    ])
      ->getMock();
    $accountMock
      ->expects($this
      ->once())
      ->method('isAnonymous')
      ->with()
      ->willReturn(TRUE);
    $variablesArray['account'] = $accountMock;

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\UsernameEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(UsernamePreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(UsernameEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
    self::assertEquals($accountMock, $variables
      ->getAccount());
    self::assertTrue($variables
      ->userIsAnonymous());
  }

  /**
   * Test a ViewFieldPreprocessEvent.
   */
  public function testViewFieldEvent() {
    $variablesArray = $this
      ->createVariablesArray();
    $variablesArray['field'] = 'field';
    $variablesArray['output'] = 'output';
    $variablesArray['row'] = 'row';
    $variablesArray['view'] = 'view';

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\ViewFieldEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(ViewFieldPreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(ViewFieldEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
    self::assertEquals('field', $variables
      ->getField());
    self::assertEquals('output', $variables
      ->getOutput());
    self::assertEquals('row', $variables
      ->getRow());
    self::assertEquals('view', $variables
      ->getView());
  }

  /**
   * Test a ViewTablePreprocessEvent.
   */
  public function testViewTableEvent() {
    $variablesArray = $this
      ->createVariablesArray();
    $variablesArray['rows'] = 'rows';
    $variablesArray['view'] = 'view';

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\ViewTableEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(ViewTablePreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(ViewTableEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
    self::assertSame('rows', $variables
      ->getRows());
    self::assertSame('view', $variables
      ->getView());
  }

  /**
   * Test a ViewPreprocessEvent.
   */
  public function testViewEvent() {
    $variablesArray = $this
      ->createVariablesArray();
    $variablesArray['rows'][0]['#rows'] = [
      'rows',
    ];
    $variablesArray['view'] = 'view';

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\ViewEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(ViewPreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(ViewEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
    self::assertEquals([
      'rows',
    ], $variables
      ->getRows());
    self::assertEquals('view', $variables
      ->getView());
  }

  /**
   * Test a StatusMessagesPreprocessEvent.
   */
  public function testStatusMessagesEvent() {
    $variablesArray = $this
      ->createVariablesArray();

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\StatusMessagesEventVariables $variables */
    $variables = $this
      ->getVariablesFromCreatedEvent(StatusMessagesPreprocessEvent::class, $variablesArray);
    self::assertInstanceOf(StatusMessagesEventVariables::class, $variables);
    $this
      ->assertAbstractEventVariables($variables);
  }

  /**
   * Test the default event variable functions.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\AbstractEventVariables $variables
   *   Variables object.
   */
  private function assertAbstractEventVariables(AbstractEventVariables $variables) {
    self::assertEquals('success', $variables
      ->get('test'));
    self::assertEquals('default', $variables
      ->get('test2', 'default'));
    $reference =& $variables
      ->getByReference('reference');
    self::assertEquals('first', $reference);
    $reference = 'second';
    self::assertEquals('second', $variables
      ->get('reference'));
    $variables
      ->set('test3', 'new set');
    self::assertEquals('new set', $variables
      ->get('test3'));
    $variables
      ->remove('test');
    self::assertNull($variables
      ->get('test'));
  }

  /**
   * Get the variables from the created event.
   *
   * @param string $class
   *   Event class name.
   * @param array $variablesArray
   *   Variables array.
   *
   * @return \Drupal\hook_event_dispatcher\Event\Preprocess\Variables\AbstractEventVariables
   *   Variables object.
   */
  private function getVariablesFromCreatedEvent($class, array $variablesArray) {

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\PreprocessEventInterface $class */
    $hook = $class::getHook();
    self::assertEquals(AbstractPreprocessEvent::DISPATCH_NAME_PREFIX . $hook, $class::name());
    $factory = $this->mapper
      ->getFactory($hook);
    self::assertEquals($hook, $factory
      ->getEventHook());

    /** @var \Drupal\hook_event_dispatcher\Event\Preprocess\PreprocessEventInterface $event*/
    $event = $factory
      ->createEvent($variablesArray);
    self::assertInstanceOf($class, $event);
    return $event
      ->getVariables();
  }

  /**
   * Create the variables array.
   *
   * @return array
   *   Variables array.
   */
  private function createVariablesArray() {
    return [
      'test' => 'success',
      'reference' => 'first',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OtherEventVariablesTest::$mapper private property Factory mapper.
OtherEventVariablesTest::assertAbstractEventVariables private function Test the default event variable functions.
OtherEventVariablesTest::createVariablesArray private function Create the variables array.
OtherEventVariablesTest::getVariablesFromCreatedEvent private function Get the variables from the created event.
OtherEventVariablesTest::setUp public function Overrides UnitTestCase::setUp
OtherEventVariablesTest::testBlockEvent public function Test a BlockPreprocessEvent.
OtherEventVariablesTest::testFieldEvent public function Test a FieldPreprocessEvent.
OtherEventVariablesTest::testFormEvent public function Test FormPreprocessEvent.
OtherEventVariablesTest::testHtmlEvent public function Test a HtmlPreprocessEvent.
OtherEventVariablesTest::testImageEvent public function Test a ImagePreprocessEvent.
OtherEventVariablesTest::testPageEvent public function Test a PagePreprocessEvent.
OtherEventVariablesTest::testStatusMessagesEvent public function Test a StatusMessagesPreprocessEvent.
OtherEventVariablesTest::testUsernameEvent public function Test a UsernamePreprocessEvent.
OtherEventVariablesTest::testViewEvent public function Test a ViewPreprocessEvent.
OtherEventVariablesTest::testViewFieldEvent public function Test a ViewFieldPreprocessEvent.
OtherEventVariablesTest::testViewTableEvent public function Test a ViewTablePreprocessEvent.
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.