You are here

class ConfigSnapshotConfigEntityUnitTest in Config Snapshot 8

@coversDefaultClass \Drupal\config_snapshot\Entity\ConfigSnapshot @group config_snapshot

Hierarchy

Expanded class hierarchy of ConfigSnapshotConfigEntityUnitTest

File

tests/src/Unit/ConfigSnapshotConfigEntityUnitTest.php, line 16

Namespace

Drupal\Tests\config_snapshot\Unit
View source
class ConfigSnapshotConfigEntityUnitTest extends UnitTestCase {

  /**
   * The configuration snapshot entity.
   *
   * @var \Drupal\config_snapshot\Entity\ConfigSnapshot
   */
  protected $entity;

  /**
   * The entity type used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityType;

  /**
   * The entity manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityManager;

  /**
   * The entity type manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * The UUID generator used for testing.
   *
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $uuid;

  /**
   * The extension name used for testing.
   *
   * @var string
   */
  protected $extensionName;

  /**
   * {@inheritdoc}
   */
  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');
  }

  /**
   * @covers ::calculateDependencies
   */
  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']);
  }

  /**
   * @covers ::clearItem
   * @covers ::getItem
   * @covers ::getItems
   * @covers ::setItem
   */
  public function testItems() {
    $expected_item = [
      'collection' => StorageInterface::DEFAULT_COLLECTION,
      'name' => 'example',
      'data' => [
        'something' => 'some_value',
      ],
    ];

    // Set a new item.
    $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);

    // Reset an existing 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']);

    // Clear an item.
    $this->entity
      ->clearItem($expected_item['collection'], $expected_item['name']);
    $item = $this->entity
      ->getItem($expected_item['collection'], $expected_item['name']);
    $this
      ->assertNull($item);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSnapshotConfigEntityUnitTest::$entity protected property The configuration snapshot entity.
ConfigSnapshotConfigEntityUnitTest::$entityManager protected property The entity manager used for testing.
ConfigSnapshotConfigEntityUnitTest::$entityType protected property The entity type used for testing.
ConfigSnapshotConfigEntityUnitTest::$entityTypeManager protected property The entity type manager used for testing.
ConfigSnapshotConfigEntityUnitTest::$extensionName protected property The extension name used for testing.
ConfigSnapshotConfigEntityUnitTest::$uuid protected property The UUID generator used for testing.
ConfigSnapshotConfigEntityUnitTest::setUp protected function Overrides UnitTestCase::setUp
ConfigSnapshotConfigEntityUnitTest::testCalculateDependencies public function @covers ::calculateDependencies
ConfigSnapshotConfigEntityUnitTest::testItems public function @covers ::clearItem @covers ::getItem @covers ::getItems @covers ::setItem
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.