function LinkFormatterTest::testFormatterLinkItemUrlUnexpectedException

Same name and namespace in other branches
  1. 9 core/modules/link/tests/src/Unit/LinkFormatterTest.php \Drupal\Tests\link\Unit\LinkFormatterTest::testFormatterLinkItemUrlUnexpectedException()
  2. 11.x core/modules/link/tests/src/Unit/LinkFormatterTest.php \Drupal\Tests\link\Unit\LinkFormatterTest::testFormatterLinkItemUrlUnexpectedException()

Tests when LinkItem::getUrl throws an unexpected exception.

File

core/modules/link/tests/src/Unit/LinkFormatterTest.php, line 73

Class

LinkFormatterTest
Tests the Field Formatter for the link field type.

Namespace

Drupal\Tests\link\Unit

Code

public function testFormatterLinkItemUrlUnexpectedException() : void {
  $exception = new \Exception('Unexpected!!!');
  $linkItem = $this->createMock(LinkItemInterface::class);
  $entity = $this->createMock(EntityInterface::class);
  $linkItem->expects($this->any())
    ->method('getParent')
    ->willReturn($entity);
  $linkItem->expects($this->once())
    ->method('getUrl')
    ->willThrowException($exception);
  $linkItem->expects($this->any())
    ->method('__get')
    ->with('options')
    ->willReturn([]);
  $fieldDefinition = $this->createMock(FieldDefinitionInterface::class);
  $fieldList = new FieldItemList($fieldDefinition, '', $linkItem);
  $fieldTypePluginManager = $this->createMock(FieldTypePluginManagerInterface::class);
  $fieldTypePluginManager->expects($this->once())
    ->method('createFieldItem')
    ->willReturn($linkItem);
  $container = new ContainerBuilder();
  $container->set('plugin.manager.field.field_type', $fieldTypePluginManager);
  \Drupal::setContainer($container);
  $fieldList->setValue([
    $linkItem,
  ]);
  $pathValidator = $this->createMock(PathValidatorInterface::class);
  $linkFormatter = new LinkFormatter('', [], $fieldDefinition, [], '', '', [], $pathValidator);
  $this->expectException(\Exception::class);
  $this->expectExceptionMessage('Unexpected!!!');
  $linkFormatter->viewElements($fieldList, 'fr');
}

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