You are here

class EntityDisplayModeBaseUnitTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php \Drupal\Tests\Core\Config\Entity\EntityDisplayModeBaseUnitTest

@coversDefaultClass \Drupal\Core\Entity\EntityDisplayModeBase @group Config

Hierarchy

Expanded class hierarchy of EntityDisplayModeBaseUnitTest

File

core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php, line 13

Namespace

Drupal\Tests\Core\Config\Entity
View 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() {
    $this->entityType = $this
      ->randomMachineName();
    $this->entityInfo = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $this->entityInfo
      ->expects($this
      ->any())
      ->method('getProvider')
      ->will($this
      ->returnValue('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);
  }

  /**
   * @covers ::calculateDependencies
   */
  public function testCalculateDependencies() {
    $target_entity_type_id = $this
      ->randomMachineName(16);
    $target_entity_type = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
    $target_entity_type
      ->expects($this
      ->any())
      ->method('getProvider')
      ->will($this
      ->returnValue('test_module'));
    $values = [
      'targetEntityType' => $target_entity_type_id,
    ];
    $this->entityTypeManager
      ->expects($this
      ->at(0))
      ->method('getDefinition')
      ->with($target_entity_type_id)
      ->will($this
      ->returnValue($target_entity_type));
    $this->entityTypeManager
      ->expects($this
      ->at(1))
      ->method('getDefinition')
      ->with($this->entityType)
      ->will($this
      ->returnValue($this->entityInfo));
    $this->entity = $this
      ->getMockBuilder('\\Drupal\\Core\\Entity\\EntityDisplayModeBase')
      ->setConstructorArgs([
      $values,
      $this->entityType,
    ])
      ->setMethods([
      'getFilterFormat',
    ])
      ->getMock();
    $dependencies = $this->entity
      ->calculateDependencies()
      ->getDependencies();
    $this
      ->assertContains('test_module', $dependencies['module']);
  }

  /**
   * @covers ::setTargetType
   */
  public function testSetTargetType() {

    // Generate mock.
    $mock = $this
      ->getMockBuilder('Drupal\\Core\\Entity\\EntityDisplayModeBase')
      ->setMethods(NULL)
      ->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');
    $property
      ->setAccessible(TRUE);

    // 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));
  }

  /**
   * @covers ::getTargetType
   */
  public function testGetTargetType() {

    // Generate mock.
    $mock = $this
      ->getMockBuilder('Drupal\\Core\\Entity\\EntityDisplayModeBase')
      ->setMethods(NULL)
      ->setConstructorArgs([
      [
        'something' => 'nothing',
      ],
      'test_type',
    ])
      ->getMock();

    // A test value.
    $target = 'test_target_type';

    // Gain access to the protected property.
    $property = new \ReflectionProperty($mock, 'targetEntityType');
    $property
      ->setAccessible(TRUE);

    // 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

Namesort descending Modifiers Type Description Overrides
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 @covers ::calculateDependencies
EntityDisplayModeBaseUnitTest::testGetTargetType public function @covers ::getTargetType
EntityDisplayModeBaseUnitTest::testSetTargetType public function @covers ::setTargetType
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.