Namespace
Drupal\ctools\Testing
File
-
src/Testing/EntityCreationTrait.php
View source
<?php
namespace Drupal\ctools\Testing;
use Drupal\Component\Render\FormattableMarkup;
trait EntityCreationTrait {
protected $entityTypeManager;
protected function createEntity($entity_type, array $values = []) {
$storage = $this->getEntityTypeManager()
->getStorage($entity_type);
$entity = $storage->create($values);
$status = $entity->save();
\Drupal::service('router.builder')->rebuild();
if ($this instanceof \PHPUnit\Framework\TestCase) {
$this->assertSame(SAVED_NEW, $status, (new FormattableMarkup('Created entity %id of type %type.', [
'%id' => $entity->id(),
'%type' => $entity_type,
]))
->__toString());
}
else {
$this->assertEquals(SAVED_NEW, $status, (new FormattableMarkup('Created entity %id of type %type.', [
'%id' => $entity->id(),
'%type' => $entity_type,
]))
->__toString());
}
return $entity;
}
protected function getEntityTypeManager() {
if (!isset($this->entityTypeManager)) {
$this->entityTypeManager = $this->container
->get('entity_type.manager');
}
return $this->entityTypeManager;
}
}
Traits