class ContextTest

Same name in this branch
  1. 10 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
  2. 9 core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest
  3. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
  4. 8.9.x core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest
  5. 11.x core/tests/Drupal/Tests/Core/Plugin/Context/ContextTest.php \Drupal\Tests\Core\Plugin\Context\ContextTest
  6. 11.x core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php \Drupal\Tests\Component\Plugin\Context\ContextTest

@coversDefaultClass \Drupal\Component\Plugin\Context\Context
@group Plugin

Hierarchy

  • class \Drupal\Tests\Component\Plugin\Context\ContextTest implements \PHPUnit\Framework\TestCase

Expanded class hierarchy of ContextTest

File

core/tests/Drupal/Tests/Component/Plugin/Context/ContextTest.php, line 14

Namespace

Drupal\Tests\Component\Plugin\Context
View source
class ContextTest extends TestCase {
  
  /**
   * Data provider for testGetContextValue.
   */
  public static function providerGetContextValue() {
    return [
      [
        'context_value',
        'context_value',
        FALSE,
        'data_type',
      ],
      [
        NULL,
        NULL,
        FALSE,
        'data_type',
      ],
      [
        'will throw exception',
        NULL,
        TRUE,
        'data_type',
      ],
    ];
  }
  
  /**
   * @covers ::getContextValue
   * @dataProvider providerGetContextValue
   */
  public function testGetContextValue($expected, $context_value, $is_required, $data_type) : void {
    // Mock a Context object.
    $mock_context = $this->getMockBuilder('Drupal\\Component\\Plugin\\Context\\Context')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getContextDefinition',
    ])
      ->getMock();
    // If the context value exists, getContextValue() behaves like a normal
    // getter.
    if ($context_value) {
      // Set visibility of contextValue.
      $ref_context_value = new \ReflectionProperty($mock_context, 'contextValue');
      // Set contextValue to a testable state.
      $ref_context_value->setValue($mock_context, $context_value);
      // Exercise getContextValue().
      $this->assertEquals($context_value, $mock_context->getContextValue());
    }
    else {
      // Create a mock definition.
      $mock_definition = $this->createMock('Drupal\\Component\\Plugin\\Context\\ContextDefinitionInterface');
      // Set expectation for isRequired().
      $mock_definition->expects($this->once())
        ->method('isRequired')
        ->willReturn($is_required);
      // Set expectation for getDataType().
      $mock_definition->expects($this->exactly($is_required ? 1 : 0))
        ->method('getDataType')
        ->willReturn($data_type);
      // Set expectation for getContextDefinition().
      $mock_context->expects($this->once())
        ->method('getContextDefinition')
        ->willReturn($mock_definition);
      // Set expectation for exception.
      if ($is_required) {
        $this->expectException('Drupal\\Component\\Plugin\\Exception\\ContextException');
        $this->expectExceptionMessage(sprintf("The %s context is required and not present.", $data_type));
      }
      // Exercise getContextValue().
      $this->assertEquals($context_value, $mock_context->getContextValue());
    }
  }
  
  /**
   * Data provider for testHasContextValue.
   */
  public static function providerHasContextValue() {
    return [
      [
        TRUE,
        FALSE,
      ],
      [
        TRUE,
        0,
      ],
      [
        TRUE,
        -0,
      ],
      [
        TRUE,
        0.0,
      ],
      [
        TRUE,
        -0.0,
      ],
      [
        TRUE,
        '',
      ],
      [
        TRUE,
        '0',
      ],
      [
        TRUE,
        [],
      ],
      [
        FALSE,
        NULL,
      ],
    ];
  }
  
  /**
   * @covers ::hasContextValue
   * @dataProvider providerHasContextValue
   */
  public function testHasContextValue($has_context_value, $default_value) : void {
    $mock_definition = $this->createMock('Drupal\\Component\\Plugin\\Context\\ContextDefinitionInterface');
    $mock_definition->expects($this->atLeastOnce())
      ->method('getDefaultValue')
      ->willReturn($default_value);
    $context = new Context($mock_definition);
    $this->assertSame($has_context_value, $context->hasContextValue());
    $this->assertSame($default_value, $context->getContextValue());
  }
  
  /**
   * @covers ::getContextValue
   */
  public function testDefaultValue() : void {
    $mock_definition = $this->createMock('Drupal\\Component\\Plugin\\Context\\ContextDefinitionInterface');
    $mock_definition->expects($this->once())
      ->method('getDefaultValue')
      ->willReturn('test');
    $context = new Context($mock_definition);
    $this->assertEquals('test', $context->getContextValue());
  }

}

Members

Title Sort descending Modifiers Object type Summary
ContextTest::providerGetContextValue public static function Data provider for testGetContextValue.
ContextTest::providerHasContextValue public static function Data provider for testHasContextValue.
ContextTest::testDefaultValue public function @covers ::getContextValue[[api-linebreak]]
ContextTest::testGetContextValue public function @covers ::getContextValue[[api-linebreak]]
@dataProvider providerGetContextValue
ContextTest::testHasContextValue public function @covers ::hasContextValue[[api-linebreak]]
@dataProvider providerHasContextValue

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.