EntityDisplayBaseTest.php

Same filename in this branch
  1. main core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php
Same filename and directory in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php
  2. 11.x core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php
  4. 10 core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php
  5. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php
  6. 9 core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php
  7. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayBaseTest.php
  8. 8.9.x core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php

Namespace

Drupal\Tests\Core\Config\Entity

File

core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayBaseTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Config\Entity;

use Drupal\Core\Entity\EntityDisplayBase;
use Drupal\Core\Entity\EntityType;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;

/**
 * Tests Drupal\Core\Entity\EntityDisplayBase.
 */
class EntityDisplayBaseTest extends UnitTestCase {
  
  /**
   * The mocked EntityDisplay object for testing.
   */
  protected EntityDisplayBaseMockableClass&MockObject $entityDisplay;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->entityDisplay = $this->getMockBuilder(EntityDisplayBaseMockableClass::class)
      ->disableOriginalConstructor()
      ->onlyMethods([])
      ->getMock();
  }
  
  /**
   * Tests get target entity type id.
   */
  public function testGetTargetEntityTypeId() : void {
    $reflection = new \ReflectionProperty($this->entityDisplay, 'targetEntityType');
    $reflection->setValue($this->entityDisplay, 'test');
    $this->assertEquals('test', $this->entityDisplay
      ->getTargetEntityTypeId());
  }
  
  /**
   * Tests get mode.
   */
  public function testGetMode() : void {
    $reflection = new \ReflectionProperty($this->entityDisplay, 'mode');
    $reflection->setValue($this->entityDisplay, 'test');
    $this->assertEquals('test', $this->entityDisplay
      ->getMode());
  }
  
  /**
   * Tests get original mode.
   */
  public function testGetOriginalMode() : void {
    $reflection = new \ReflectionProperty($this->entityDisplay, 'originalMode');
    $reflection->setValue($this->entityDisplay, 'test');
    $this->assertEquals('test', $this->entityDisplay
      ->getOriginalMode());
  }
  
  /**
   * Tests get target bundle.
   */
  public function testGetTargetBundle() : void {
    $reflection = new \ReflectionProperty($this->entityDisplay, 'bundle');
    $reflection->setValue($this->entityDisplay, 'test');
    $this->assertEquals('test', $this->entityDisplay
      ->getTargetBundle());
  }
  
  /**
   * Tests set target bundle.
   */
  public function testSetTargetBundle() : void {
    $reflection = new \ReflectionProperty($this->entityDisplay, 'bundle');
    $this->entityDisplay
      ->setTargetBundle('test');
    $this->assertEquals('test', $reflection->getValue($this->entityDisplay));
  }

}

/**
 * A class extending EntityDisplayBase for testing purposes.
 */
class EntityDisplayBaseMockableClass extends EntityDisplayBase {
  public function getPluginCollections() : array {
    return [];
  }
  public function getRenderer($field_name) : null {
    return NULL;
  }
  public function getEntityType() : EntityType {
    return new EntityType([
      'id' => 'entity_view_display',
      'entity_keys' => [
        'id' => 'id',
      ],
    ]);
  }

}

Classes

Title Deprecated Summary
EntityDisplayBaseMockableClass A class extending EntityDisplayBase for testing purposes.
EntityDisplayBaseTest Tests Drupal\Core\Entity\EntityDisplayBase.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.