FormatterTestBase.php in Address 8
File
tests/src/Kernel/Formatter/FormatterTestBase.php
View source
<?php
namespace Drupal\Tests\address\Kernel\Formatter;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
abstract class FormatterTestBase extends KernelTestBase {
protected static $modules = [
'system',
'field',
'language',
'text',
'entity_test',
'user',
'address',
];
protected $fieldName;
protected $display;
protected function setUp() : void {
parent::setUp();
if (\Drupal::entityTypeManager()
->hasDefinition('path_alias')) {
$this
->installEntitySchema('path_alias');
}
$this
->installConfig([
'system',
]);
$this
->installConfig([
'field',
]);
$this
->installConfig([
'text',
]);
$this
->installConfig([
'address',
]);
$this
->installEntitySchema('entity_test');
$this->fieldName = mb_strtolower($this
->randomMachineName());
}
protected function createField($field_type, $formatter_id) {
$field_storage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => $field_type,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
'label' => $this
->randomMachineName(),
]);
$field
->save();
$this->display = \Drupal::service('entity_display.repository')
->getViewDisplay('entity_test', 'entity_test', 'default');
$this->display
->setComponent($this->fieldName, [
'type' => $formatter_id,
'settings' => [],
]);
$this->display
->save();
}
protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
$content = $display
->build($entity);
$content = $this
->render($content);
return $content;
}
}