class EntityDisplayModeBaseUnitTest
Tests Drupal\Core\Entity\EntityDisplayModeBase.
Attributes
#[CoversClass(EntityDisplayModeBase::class)]
#[Group('Config')]
  Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase- class \Drupal\Tests\Core\Config\Entity\EntityDisplayModeBaseUnitTest extends \Drupal\Tests\UnitTestCase
 
Expanded class hierarchy of EntityDisplayModeBaseUnitTest
File
- 
              core/tests/ Drupal/ Tests/ Core/ Config/ Entity/ EntityDisplayModeBaseUnitTest.php, line 17 
Namespace
Drupal\Tests\Core\Config\EntityView source
class EntityDisplayModeBaseUnitTest extends UnitTestCase {
  
  /**
   * The entity under test.
   *
   * @var \Drupal\Core\Entity\EntityDisplayModeBase|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entity;
  
  /**
   * The entity type used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityInfo;
  
  /**
   * The entity type manager used for testing.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $entityTypeManager;
  
  /**
   * The ID of the type of the entity under test.
   *
   * @var string
   */
  protected $entityType;
  
  /**
   * The UUID generator used for testing.
   *
   * @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $uuid;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entityType = $this->randomMachineName();
    $this->entityInfo = $this->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $this->entityInfo
      ->expects($this->any())
      ->method('getProvider')
      ->willReturn('entity');
    $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
    $this->uuid = $this->createMock('\\Drupal\\Component\\Uuid\\UuidInterface');
    $container = new ContainerBuilder();
    $container->set('entity_type.manager', $this->entityTypeManager);
    $container->set('uuid', $this->uuid);
    \Drupal::setContainer($container);
  }
  
  /**
   * Tests calculate dependencies.
   *
   * @legacy-covers ::calculateDependencies
   */
  public function testCalculateDependencies() : void {
    $target_entity_type_id = $this->randomMachineName(16);
    $target_entity_type = $this->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $target_entity_type->expects($this->any())
      ->method('getProvider')
      ->willReturn('test_module');
    $values = [
      'targetEntityType' => $target_entity_type_id,
    ];
    $this->entityTypeManager
      ->expects($this->exactly(2))
      ->method('getDefinition')
      ->willReturnMap([
      [
        $target_entity_type_id,
        TRUE,
        $target_entity_type,
      ],
      [
        $this->entityType,
        TRUE,
        $this->entityInfo,
      ],
    ]);
    $this->entity = new EntityDisplayModeBaseTestableClass($values, $this->entityType);
    $dependencies = $this->entity
      ->calculateDependencies()
      ->getDependencies();
    $this->assertContains('test_module', $dependencies['module']);
  }
  
  /**
   * Tests set target type.
   *
   * @legacy-covers ::setTargetType
   */
  public function testSetTargetType() : void {
    // Generate mock.
    $mock = $this->getMockBuilder('Drupal\\Core\\Entity\\EntityDisplayModeBase')
      ->onlyMethods([])
      ->setConstructorArgs([
      [
        'something' => 'nothing',
      ],
      'test_type',
    ])
      ->getMock();
    // Some test values.
    $bad_target = 'uninitialized';
    $target = 'test_target_type';
    // Gain access to the protected property.
    $property = new \ReflectionProperty($mock, 'targetEntityType');
    // Set the property to a known state.
    $property->setValue($mock, $bad_target);
    // Set the target type.
    $mock->setTargetType($target);
    // Test the outcome.
    $this->assertNotEquals($bad_target, $property->getValue($mock));
    $this->assertEquals($target, $property->getValue($mock));
  }
  
  /**
   * Tests get target type.
   *
   * @legacy-covers ::getTargetType
   */
  public function testGetTargetType() : void {
    // Generate mock.
    $mock = $this->getMockBuilder('Drupal\\Core\\Entity\\EntityDisplayModeBase')
      ->onlyMethods([])
      ->setConstructorArgs([
      [
        'something' => 'nothing',
      ],
      'test_type',
    ])
      ->getMock();
    // A test value.
    $target = 'test_target_type';
    // Gain access to the protected property.
    $property = new \ReflectionProperty($mock, 'targetEntityType');
    // Set the property to a known state.
    $property->setValue($mock, $target);
    // Get the target type.
    $value = $mock->getTargetType($target);
    // Test the outcome.
    $this->assertEquals($value, $property->getValue($mock));
  }
}Members
| Title Sort descending | Modifiers | Object type | Summary | Overriden Title | 
|---|---|---|---|---|
| EntityDisplayModeBaseUnitTest::$entity | protected | property | The entity under test. | |
| EntityDisplayModeBaseUnitTest::$entityInfo | protected | property | The entity type used for testing. | |
| EntityDisplayModeBaseUnitTest::$entityType | protected | property | The ID of the type of the entity under test. | |
| EntityDisplayModeBaseUnitTest::$entityTypeManager | protected | property | The entity type manager used for testing. | |
| EntityDisplayModeBaseUnitTest::$uuid | protected | property | The UUID generator used for testing. | |
| EntityDisplayModeBaseUnitTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
| EntityDisplayModeBaseUnitTest::testCalculateDependencies | public | function | Tests calculate dependencies. | |
| EntityDisplayModeBaseUnitTest::testGetTargetType | public | function | Tests get target type. | |
| EntityDisplayModeBaseUnitTest::testSetTargetType | public | function | Tests set target type. | |
| ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
| ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
| UnitTestCase::$root | protected | property | The app root. | |
| 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::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
