function ContextDefinitionTest::testGetDataDefinitionInvalidType

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php \Drupal\Tests\Core\Plugin\Context\ContextDefinitionTest::testGetDataDefinitionInvalidType()
  2. 10 core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php \Drupal\Tests\Core\Plugin\Context\ContextDefinitionTest::testGetDataDefinitionInvalidType()
  3. 11.x core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php \Drupal\Tests\Core\Plugin\Context\ContextDefinitionTest::testGetDataDefinitionInvalidType()

@dataProvider providerGetDataDefinition @covers ::getDataDefinition
@uses \Drupal

File

core/tests/Drupal/Tests/Core/Plugin/Context/ContextDefinitionTest.php, line 116

Class

ContextDefinitionTest
Tests the ContextDefinition class.

Namespace

Drupal\Tests\Core\Plugin\Context

Code

public function testGetDataDefinitionInvalidType($is_multiple) {
  // Since we're trying to make getDataDefinition() throw an exception in
  // isolation, we use a data type which is not valid.
  $data_type = 'not_valid';
  $mock_data_definition = $this->getMockBuilder('\\Drupal\\Core\\TypedData\\ListDataDefinitionInterface')
    ->getMockForAbstractClass();
  // Follow code paths for both multiple and non-multiple definitions.
  $create_definition_method = 'createDataDefinition';
  if ($is_multiple) {
    $create_definition_method = 'createListDataDefinition';
  }
  $mock_data_manager = $this->createMock(TypedDataManagerInterface::class);
  // Our mocked data manager will return NULL for a non-valid data type. This
  // will eventually cause getDataDefinition() to throw an exception.
  $mock_data_manager->expects($this->once())
    ->method($create_definition_method)
    ->willReturnMap([
    [
      'not_valid',
      NULL,
    ],
    [
      'valid',
      $mock_data_definition,
    ],
  ]);
  // Mock a ContextDefinition object with expectations for only the methods
  // that will be called before the expected exception.
  $mock_context_definition = $this->getMockBuilder('Drupal\\Core\\Plugin\\Context\\ContextDefinition')
    ->disableOriginalConstructor()
    ->onlyMethods([
    'isMultiple',
    'getTypedDataManager',
    'getDataType',
  ])
    ->getMock();
  $mock_context_definition->expects($this->once())
    ->method('isMultiple')
    ->willReturn($is_multiple);
  $mock_context_definition->expects($this->once())
    ->method('getTypedDataManager')
    ->willReturn($mock_data_manager);
  $mock_context_definition->method('getDataType')
    ->willReturn($data_type);
  $this->expectException(\Exception::class);
  $mock_context_definition->getDataDefinition();
}

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