View source
<?php
namespace Drupal\Tests\config_snapshot\Unit;
use Drupal\config_snapshot\Entity\ConfigSnapshot;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
class ConfigSnapshotConfigEntityUnitTest extends UnitTestCase {
protected $entity;
protected $entityType;
protected $entityManager;
protected $entityTypeManager;
protected $uuid;
protected $extensionName;
protected function setUp() {
$this->entityType = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$this->entityType
->expects($this
->any())
->method('getProvider')
->will($this
->returnValue('entity'));
$this->entityManager = new EntityManager();
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->uuid = $this
->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
$container = new ContainerBuilder();
$container
->set('entity.manager', $this->entityManager);
$container
->set('entity_type.manager', $this->entityTypeManager);
$container
->set('uuid', $this->uuid);
$this->entityManager
->setContainer($container);
\Drupal::setContainer($container);
$snapshot_set = $this
->randomMachineName(8);
$extension_type = 'module';
$this->extensionName = $this
->randomMachineName(8);
$values = [
'snapshotSet' => $snapshot_set,
'extensionType' => $extension_type,
'extensionName' => $this->extensionName,
];
$this->entity = new ConfigSnapshot($values, 'config_snapshot.snapshot');
}
public function testCalculateDependencies() {
$this->entityTypeManager
->expects($this
->any())
->method('getDefinition')
->will($this
->returnValue($this->entityType));
$dependencies = $this->entity
->calculateDependencies()
->getDependencies();
$this
->assertArrayNotHasKey('config', $dependencies);
$this
->assertContains($this->extensionName, $dependencies['module']);
}
public function testItems() {
$expected_item = [
'collection' => StorageInterface::DEFAULT_COLLECTION,
'name' => 'example',
'data' => [
'something' => 'some_value',
],
];
$this->entity
->setItem($expected_item['collection'], $expected_item['name'], $expected_item['data']);
$items = $this->entity
->getItems();
$this
->assertEquals(1, count($items));
$this
->assertEquals($expected_item, $items[0]);
$item = $this->entity
->getItem($expected_item['collection'], $expected_item['name']);
$this
->assertEquals($expected_item, $item);
$expected_new_data = [
'something_else' => 'some_new_value',
];
$this->entity
->setItem($expected_item['collection'], $expected_item['name'], $expected_new_data);
$item = $this->entity
->getItem($expected_item['collection'], $expected_item['name']);
$this
->assertEquals($expected_new_data, $item['data']);
$this->entity
->clearItem($expected_item['collection'], $expected_item['name']);
$item = $this->entity
->getItem($expected_item['collection'], $expected_item['name']);
$this
->assertNull($item);
}
}