class EntityTypeBundleInfoTest in Entity Construction Kit (ECK) 8
Tests the form element implementation.
@group eck
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\eck\Unit\UnitTestBase
- class \Drupal\Tests\eck\Unit\EntityTypeBundleInfoTest
- class \Drupal\Tests\eck\Unit\UnitTestBase
Expanded class hierarchy of EntityTypeBundleInfoTest
File
- tests/
src/ Unit/ EntityTypeBundleInfoTest.php, line 20
Namespace
Drupal\Tests\eck\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityTypeBundleInfoTest:: |
protected | property | The cache backend mock. | |
EntityTypeBundleInfoTest:: |
protected | property | The entity type manager mock. | |
EntityTypeBundleInfoTest:: |
protected | property | The language manager mock. | |
EntityTypeBundleInfoTest:: |
protected | property | The module handler mock. | |
EntityTypeBundleInfoTest:: |
protected | property | The typed data manager mock. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that the number of entity type bundles is counted correctly. | |
EntityTypeBundleInfoTest:: |
protected | function | Creates a new test subject. | |
EntityTypeBundleInfoTest:: |
protected | function | Creates a new test subject with an entity type. | |
EntityTypeBundleInfoTest:: |
protected | function | Creates a new test subject with entity type and bundles. | |
EntityTypeBundleInfoTest:: |
public | function | Tests caching of data. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that entityTypeHasBundles returns false if it has no bundles. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that entityTypeHasBundles returns false on non-existing entity type. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that machine names are returned if there are bundles. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that no machine names are returned if the entity type doesn't exist. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that no machine names are returned if there are no bundles. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that entityTypeHasBundles returns true if it has bundles. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that zero is returned if the entity type doesn't exist. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that zero is returned if the entity type has no bundles. | |
EntityTypeBundleInfoTest:: |
public | function | Tests that cached data is used if available. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestBase:: |
protected | property | The entities. | |
UnitTestBase:: |
private | property | The services. | |
UnitTestBase:: |
protected | function | Adds an entity to the mock storage. | |
UnitTestBase:: |
protected | function | Asserts that the array keys of an array equal the expected keys. | |
UnitTestBase:: |
public | function | Callback for the get method on the mocked service container. | |
UnitTestBase:: |
protected | function | Creates a test entity type. | |
UnitTestBase:: |
protected | function | Creates the language manager mock. | |
UnitTestBase:: |
public | function | Callback for entity storage load multiple. | |
UnitTestBase:: |
private | function | Retrieves the entity manager mock. | |
UnitTestBase:: |
private | function | Retrieves the entity storage mock. | |
UnitTestBase:: |
private | function | Retrieves the entity type manager mock. | |
UnitTestBase:: |
private | function | Retrieves the entity type repository mock. | |
UnitTestBase:: |
private | function | Creates and returns a mocked user. | |
UnitTestBase:: |
private | function | Prepares a mocked service container. | |
UnitTestBase:: |
protected | function | Registers a (mocked) service with the mocked service container. | |
UnitTestBase:: |
protected | function |
Overrides UnitTestCase:: |
1 |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |