You are here

class EntityTypeBundleInfoTest in Entity Construction Kit (ECK) 8

Tests the form element implementation.

@group eck

Hierarchy

Expanded class hierarchy of EntityTypeBundleInfoTest

File

tests/src/Unit/EntityTypeBundleInfoTest.php, line 20

Namespace

Drupal\Tests\eck\Unit
View source
class EntityTypeBundleInfoTest extends UnitTestBase {

  /**
   * The entity type manager mock.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManagerMock;

  /**
   * The language manager mock.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManagerMock;

  /**
   * The module handler mock.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandlerMock;

  /**
   * The typed data manager mock.
   *
   * @var \Drupal\Core\TypedData\TypedDataManagerInterface
   */
  protected $typedDataManagerMock;

  /**
   * The cache backend mock.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheBackendMock;

  /**
   * Tests that entityTypeHasBundles returns false on non-existing entity type.
   *
   * @test
   */
  public function returnsFalseWhenNonExistingEntityTypeIsPassed() {
    $sut = $this
      ->createNewTestSubject();
    $this
      ->assertFalse($sut
      ->entityTypeHasBundles('does not exist'));
  }

  /**
   * Tests that entityTypeHasBundles returns false if it has no bundles.
   *
   * @test
   */
  public function returnsFalseWhenEntityTypeHasNoBundles() {
    $sut = $this
      ->createNewTestSubjectWithEntityType();
    $this
      ->assertFalse($sut
      ->entityTypeHasBundles('existing_entity_type'));
  }

  /**
   * Tests that entityTypeHasBundles returns true if it has bundles.
   *
   * @test
   */
  public function returnsTrueWhenEntityTypeHasAtLeastOneBundle() {
    $sut = $this
      ->createNewTestSubjectWithEntityTypeAndBundles();
    $this
      ->assertTrue($sut
      ->entityTypeHasBundles('existing_entity_type'));
  }

  /**
   * Tests caching of data.
   *
   * @test
   */
  public function entityTypeHasBundlesMethodCachesData() {
    $this->cacheBackendMock = $this
      ->getMockForAbstractClass(CacheBackendInterface::class);
    $this->cacheBackendMock
      ->expects($this
      ->once())
      ->method('set');
    $sut = $this
      ->createNewTestSubject();
    $sut
      ->entityTypeHasBundles('test');
  }

  /**
   * Tests that cached data is used if available.
   *
   * @test
   */
  public function usesCachedDataWhenAvailable() {
    $this->cacheBackendMock = $this->cacheBackendMock = $this
      ->getMockForAbstractClass(CacheBackendInterface::class);
    $this->cacheBackendMock
      ->expects($this
      ->once())
      ->method('get')
      ->willReturn((object) [
      'data' => 'obviously not normal bundle info',
    ]);
    $sut = $this
      ->createNewTestSubject();
    $this
      ->assertSame('obviously not normal bundle info', $sut
      ->getAllBundleInfo());
  }

  /**
   * Tests that no machine names are returned if the entity type doesn't exist.
   *
   * @test
   */
  public function returnsNoMachineNamesIfEntityTypeDoesNotExist() {
    $sut = $this
      ->createNewTestSubject();
    $this
      ->assertEmpty($sut
      ->getEntityTypeBundleMachineNames('non_existing_entity_type'));
  }

  /**
   * Tests that no machine names are returned if there are no bundles.
   *
   * @test
   */
  public function returnsNoMachineNamesIfEntityTypeHasNoBundles() {
    $sut = $this
      ->createNewTestSubjectWithEntityType();
    $this
      ->assertEmpty($sut
      ->getEntityTypeBundleMachineNames('existing_entity_type'));
  }

  /**
   * Tests that machine names are returned if there are bundles.
   *
   * @test
   */
  public function returnsMachineNamesIfEntityTypeHasBundles() {
    $sut = $this
      ->createNewTestSubjectWithEntityTypeAndBundles();
    $this
      ->assertNotEmpty($sut
      ->getEntityTypeBundleMachineNames('existing_entity_type'));
  }

  /**
   * Tests that zero is returned if the entity type doesn't exist.
   *
   * @test
   */
  public function returnsZeroIfEntityTypeDoesNotExist() {
    $sut = $this
      ->createNewTestSubject();
    $this
      ->assertEquals(0, $sut
      ->entityTypeBundleCount('non_existing_entity_type'));
  }

  /**
   * Tests that zero is returned if the entity type has no bundles.
   *
   * @test
   */
  public function returnsZeroIfEntityTypeHasNoBundles() {
    $sut = $this
      ->createNewTestSubjectWithEntityType();
    $this
      ->assertEquals(0, $sut
      ->entityTypeBundleCount('existing_entity_type'));
  }

  /**
   * Tests that the number of entity type bundles is counted correctly.
   *
   * @test
   */
  public function correctlyCountsEntityTypeBundles() {
    for ($i = 1; $i <= 10; $i++) {
      $sut = $this
        ->createNewTestSubjectWithEntityTypeAndBundles($i);
      $this
        ->assertEquals($i, $sut
        ->entityTypeBundleCount('existing_entity_type'));
    }
  }

  /**
   * Creates a new test subject.
   *
   * @return \Drupal\eck\EckEntityTypeBundleInfo
   *   The newly created test subject.
   */
  protected function createNewTestSubject() {
    if (NULL === $this->entityTypeManagerMock) {
      $this->entityTypeManagerMock = $this
        ->getMockForAbstractClass(EntityTypeManagerInterface::class);
      $this->entityTypeManagerMock
        ->method('getDefinitions')
        ->willReturn([]);
    }
    if (NULL === $this->languageManagerMock) {
      $this->languageManagerMock = $this
        ->createLanguageManagerMock();
    }
    if (NULL === $this->moduleHandlerMock) {
      $this->moduleHandlerMock = $this
        ->getMockForAbstractClass(ModuleHandlerInterface::class);
    }
    if (NULL === $this->typedDataManagerMock) {
      $this->typedDataManagerMock = $this
        ->getMockForAbstractClass(TypedDataManagerInterface::class);
    }
    if (NULL === $this->cacheBackendMock) {
      $this->cacheBackendMock = $this
        ->getMockForAbstractClass(CacheBackendInterface::class);
    }
    return new EckEntityTypeBundleInfo($this->entityTypeManagerMock, $this->languageManagerMock, $this->moduleHandlerMock, $this->typedDataManagerMock, $this->cacheBackendMock);
  }

  /**
   * Creates a new test subject with an entity type.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entityTypeMock
   *   The entity type mock.
   * @param \Drupal\Core\Entity\EntityStorageInterface $entityStorageMock
   *   The entity storage mock.
   *
   * @return \Drupal\eck\EckEntityTypeBundleInfo
   *   The test subject.
   */
  protected function createNewTestSubjectWithEntityType(EntityTypeInterface $entityTypeMock = NULL, EntityStorageInterface $entityStorageMock = NULL) {
    if (NULL === $entityTypeMock) {
      $entityTypeMock = $this
        ->getMockForAbstractClass(EntityTypeInterface::class);
      $entityTypeMock
        ->method('getBundleEntityType')
        ->willReturn('eck_entity_bundle');
    }
    if (NULL === $entityStorageMock) {
      $entityStorageMock = $this
        ->getMockForAbstractClass(EntityStorageInterface::class);
      $entityStorageMock
        ->method('loadMultiple')
        ->willReturn([]);
    }
    $this->entityTypeManagerMock = $this
      ->getMockForAbstractClass(EntityTypeManagerInterface::class);
    $this->entityTypeManagerMock
      ->method('getDefinitions')
      ->willReturn([
      'existing_entity_type' => $entityTypeMock,
    ]);
    $this->entityTypeManagerMock
      ->method('getStorage')
      ->willReturn($entityStorageMock);
    return $this
      ->createNewTestSubject();
  }

  /**
   * Creates a new test subject with entity type and bundles.
   *
   * @param int $numberOfBundlesToCreate
   *   The number of bundles to create.
   *
   * @return \Drupal\eck\EckEntityTypeBundleInfo
   *   The bundle info to test.
   */
  protected function createNewTestSubjectWithEntityTypeAndBundles($numberOfBundlesToCreate = 1) {
    $bundles = [];
    for ($i = 0; $i < $numberOfBundlesToCreate; $i++) {
      $machineName = $this
        ->randomMachineName();
      $bundleMock = $this
        ->getMockForAbstractClass(EntityInterface::class);
      $bundleMock
        ->method('id')
        ->willReturn(strtolower($machineName));
      $bundleMock
        ->method('label')
        ->willReturn($machineName);
      $bundles[strtolower($machineName)] = $bundleMock;
    }
    $entityStorageMock = $this
      ->getMockForAbstractClass(EntityStorageInterface::class);
    $entityStorageMock
      ->method('loadMultiple')
      ->willReturn($bundles);
    return $this
      ->createNewTestSubjectWithEntityType(NULL, $entityStorageMock);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityTypeBundleInfoTest::$cacheBackendMock protected property The cache backend mock.
EntityTypeBundleInfoTest::$entityTypeManagerMock protected property The entity type manager mock.
EntityTypeBundleInfoTest::$languageManagerMock protected property The language manager mock.
EntityTypeBundleInfoTest::$moduleHandlerMock protected property The module handler mock.
EntityTypeBundleInfoTest::$typedDataManagerMock protected property The typed data manager mock.
EntityTypeBundleInfoTest::correctlyCountsEntityTypeBundles public function Tests that the number of entity type bundles is counted correctly.
EntityTypeBundleInfoTest::createNewTestSubject protected function Creates a new test subject.
EntityTypeBundleInfoTest::createNewTestSubjectWithEntityType protected function Creates a new test subject with an entity type.
EntityTypeBundleInfoTest::createNewTestSubjectWithEntityTypeAndBundles protected function Creates a new test subject with entity type and bundles.
EntityTypeBundleInfoTest::entityTypeHasBundlesMethodCachesData public function Tests caching of data.
EntityTypeBundleInfoTest::returnsFalseWhenEntityTypeHasNoBundles public function Tests that entityTypeHasBundles returns false if it has no bundles.
EntityTypeBundleInfoTest::returnsFalseWhenNonExistingEntityTypeIsPassed public function Tests that entityTypeHasBundles returns false on non-existing entity type.
EntityTypeBundleInfoTest::returnsMachineNamesIfEntityTypeHasBundles public function Tests that machine names are returned if there are bundles.
EntityTypeBundleInfoTest::returnsNoMachineNamesIfEntityTypeDoesNotExist public function Tests that no machine names are returned if the entity type doesn't exist.
EntityTypeBundleInfoTest::returnsNoMachineNamesIfEntityTypeHasNoBundles public function Tests that no machine names are returned if there are no bundles.
EntityTypeBundleInfoTest::returnsTrueWhenEntityTypeHasAtLeastOneBundle public function Tests that entityTypeHasBundles returns true if it has bundles.
EntityTypeBundleInfoTest::returnsZeroIfEntityTypeDoesNotExist public function Tests that zero is returned if the entity type doesn't exist.
EntityTypeBundleInfoTest::returnsZeroIfEntityTypeHasNoBundles public function Tests that zero is returned if the entity type has no bundles.
EntityTypeBundleInfoTest::usesCachedDataWhenAvailable public function Tests that cached data is used if available.
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.
UnitTestBase::$entities protected property The entities.
UnitTestBase::$services private property The services.
UnitTestBase::addEntityToStorage protected function Adds an entity to the mock storage.
UnitTestBase::assertArrayKeysEqual protected function Asserts that the array keys of an array equal the expected keys.
UnitTestBase::containerMockGetServiceCallback public function Callback for the get method on the mocked service container.
UnitTestBase::createEckEntityType protected function Creates a test entity type.
UnitTestBase::createLanguageManagerMock protected function Creates the language manager mock.
UnitTestBase::entityStorageLoadMultiple public function Callback for entity storage load multiple.
UnitTestBase::getEntityManagerMock private function Retrieves the entity manager mock.
UnitTestBase::getEntityStorageMock private function Retrieves the entity storage mock.
UnitTestBase::getEntityTypeManagerMock private function Retrieves the entity type manager mock.
UnitTestBase::getEntityTypeRepositoryMock private function Retrieves the entity type repository mock.
UnitTestBase::getNewUserMock private function Creates and returns a mocked user.
UnitTestBase::prepareContainer private function Prepares a mocked service container.
UnitTestBase::registerServiceWithContainerMock protected function Registers a (mocked) service with the mocked service container.
UnitTestBase::setUp protected function Overrides UnitTestCase::setUp 1
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.