function LinkFormatterTest::testFormatterLinkItemUrlUnexpectedException
Tests when LinkItem::getUrl throws an unexpected exception.
File
- 
              core/modules/ link/ tests/ src/ Unit/ LinkFormatterTest.php, line 71 
Class
- LinkFormatterTest
- Tests the Field Formatter for the link field type.
Namespace
Drupal\Tests\link\UnitCode
public function testFormatterLinkItemUrlUnexpectedException() {
  $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')
    ->will($this->returnValue($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.
