You are here

final class PageTest in Hook Event Dispatcher 8

Class PageTest.

@group hook_event_dispatcher

Hierarchy

Expanded class hierarchy of PageTest

File

tests/src/Unit/Preprocess/PageTest.php, line 15

Namespace

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

  /**
   * Mock node object.
   *
   * @var \Drupal\node\Entity\Node
   */
  protected $node;

  /**
   * Setup.
   */
  public function setUp() {
    $this->node = $this
      ->getMockBuilder(NodeInterface::class)
      ->disableOriginalClone()
      ->disableOriginalConstructor()
      ->getMock();
  }

  /**
   * Test the getter.
   */
  public function testGet() {
    $vars = [];
    $vars['page']['test'] = TRUE;
    $vars['page']['array'] = [
      'array key' => 1,
    ];
    $vars['page']['object'] = new stdClass();
    $page = new PageEventVariables($vars);
    self::assertTrue($page
      ->get('test'));
    self::assertArrayHasKey('array key', $page
      ->get('array'));
    self::assertInstanceOf(stdClass::class, $page
      ->get('object'));
    self::assertFalse($page
      ->get('doesNotExists', FALSE));
  }

  /**
   * Test the setter.
   */
  public function testSet() {
    $vars = [];
    $page = new PageEventVariables($vars);
    $page
      ->set('test', TRUE);
    $page
      ->set('array', [
      'array key' => 1,
    ]);
    $page
      ->set('object', new stdClass());
    self::assertTrue($page
      ->get('test'));
    self::assertArrayHasKey('array key', $page
      ->get('array'));
    self::assertInstanceOf(stdClass::class, $page
      ->get('object'));
    $page
      ->set('null');
    self::assertNull($page
      ->get('null'));
  }

  /**
   * The the vars by ref.
   */
  public function testVarByRef() {
    $vars = [];
    $page = new PageEventVariables($vars);
    $vars['page']['test'] = TRUE;
    $vars['page']['array'] = [
      'array key' => 1,
    ];
    $vars['page']['object'] = new stdClass();
    self::assertTrue($page
      ->get('test'));
    self::assertArrayHasKey('array key', $page
      ->get('array'));
    self::assertInstanceOf(stdClass::class, $page
      ->get('object'));
  }

  /**
   * Test is node page.
   */
  public function testIsNodePage() {
    $vars = [];
    $page = new PageEventVariables($vars);
    self::assertFalse($page
      ->isNodePage());
    $vars['node'] = new stdClass();
    self::assertFalse($page
      ->isNodePage());
    $vars['node'] = $this->node;
    self::assertTrue($page
      ->isNodePage());
  }

  /**
   * Test getNode() call.
   */
  public function testGetNode() {
    $vars = [];
    $page = new PageEventVariables($vars);
    self::assertEquals(NULL, $page
      ->getNode());
    $page
      ->set('node', new stdClass());
    self::assertEquals(NULL, $page
      ->getNode());
    $vars['node'] = $this->node;
    self::assertInstanceOf(NodeInterface::class, $page
      ->getNode());
  }

  /**
   * Test getting a var by ref and changing it.
   */
  public function testGetVarByRef() {
    $vars = [];
    $vars['page']['test'] = 'test';
    $page = new PageEventVariables($vars);
    $test =& $page
      ->getByReference('test');
    self::assertEquals('test', $test);
    $test = 'OtherTest';
    self::assertEquals('OtherTest', $page
      ->get('test'));
    self::assertEquals('OtherTest', $vars['page']['test']);
  }

  /**
   * Test getting root variables by reference.
   */
  public function testGetRootVariablesByReference() {
    $vars = [];
    $vars['test'] = 'something';
    $page = new PageEventVariables($vars);
    $retrievedVars =& $page
      ->getRootVariablesByReference();
    self::assertSame($vars, $retrievedVars);
    $retrievedVars['test2'] = 'other';
    self::assertSame($vars, $retrievedVars);
    self::assertSame($vars, $page
      ->getRootVariablesByReference());
  }

  /**
   * Test add cache context.
   */
  public function testAddCacheContext() {
    $vars = $expectedVars = [];
    $page = new PageEventVariables($vars);
    $page
      ->addCacheContext('url.path');
    $expectedVars['#cache']['contexts'][] = 'url.path';
    self::assertSame($expectedVars, $vars);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PageTest::$node protected property Mock node object.
PageTest::setUp public function Setup. Overrides UnitTestCase::setUp
PageTest::testAddCacheContext public function Test add cache context.
PageTest::testGet public function Test the getter.
PageTest::testGetNode public function Test getNode() call.
PageTest::testGetRootVariablesByReference public function Test getting root variables by reference.
PageTest::testGetVarByRef public function Test getting a var by ref and changing it.
PageTest::testIsNodePage public function Test is node page.
PageTest::testSet public function Test the setter.
PageTest::testVarByRef public function The the vars by ref.
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.