You are here

class FieldTargetEntityTest in Twig Field Value 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/FieldValue/FieldTargetEntityTest.php \Drupal\Tests\twig_field_value\Unit\FieldValue\FieldTargetEntityTest

@coversDefaultClass \Drupal\twig_field_value\Twig\Extension\FieldValueExtension @group twig_field_value

Hierarchy

Expanded class hierarchy of FieldTargetEntityTest

File

tests/src/Unit/FieldValue/FieldTargetEntityTest.php, line 12

Namespace

Drupal\Tests\twig_field_value\Unit\FieldValue
View source
class FieldTargetEntityTest extends UnitTestCase {

  /**
   * The Twig extension under test.
   *
   * @var \Drupal\twig_field_value\Twig\Extension\FieldValueExtension
   */
  protected $extension;
  protected function setUp() {
    $this->extension = new FieldValueExtension();
  }

  /**
   * Returns a mock Content Entity object.
   *
   * @param array $referenced_entities
   *
   * @return \Drupal\Core\Field\FieldItemBase
   *   The entity object.
   */
  protected function mockContentEntity(array $referenced_entities) {
    $entities = [];

    // Build the 'entity' objects with a property 'entity' that contains the
    // referenced entity.
    foreach ($referenced_entities as $referenced_entity) {
      $entity = new \stdClass();
      $entity->entity = $referenced_entity;
      $entities[] = $entity;
    }
    $field_item = $this
      ->getMockBuilder('Drupal\\Core\\Entity\\ContentEntityBase')
      ->disableOriginalConstructor()
      ->getMock();
    $field_item
      ->expects($this
      ->any())
      ->method('get')
      ->will($this
      ->returnValue($entities));
    return $field_item;
  }

  /**
   * Asserts the twig field_target_entity filter.
   *
   * @dataProvider providerTestTargetEntity
   * @covers ::getTargetEntity
   *
   * @param $expected_result
   * @param $render_array
   */
  public function testTargetEntity($expected_result, $render_array) {
    $result = $this->extension
      ->getTargetEntity($render_array);
    $this
      ->assertSame($expected_result, $result);
  }

  /**
   * Provides data and expected results for the test method.
   *
   * @return array
   *   Data and expected results.
   */
  public function providerTestTargetEntity() {
    return [
      // Invalid render arrays.
      [
        NULL,
        NULL,
      ],
      [
        NULL,
        [],
      ],
      [
        NULL,
        [
          '#theme' => 'field',
          '#no_field_name' => [],
        ],
      ],
      [
        NULL,
        [
          '#theme' => 'field',
          '#field_name' => [
            'reference_field',
          ],
        ],
      ],
      [
        'foo',
        [
          '#theme' => 'field',
          '#field_name' => [
            'reference_field',
          ],
          '#object' => $this
            ->mockContentEntity([
            'foo',
          ]),
        ],
      ],
      [
        [
          'entity_1',
          'entity_2',
          'entity_3',
        ],
        [
          '#theme' => 'field',
          '#field_name' => [
            'reference_field',
          ],
          '#object' => $this
            ->mockContentEntity([
            'entity_1',
            'entity_2',
            'entity_3',
          ]),
        ],
      ],
      [
        [
          'entity_1',
          'entity_2',
        ],
        [
          '#theme' => 'field',
          '#field_name' => [
            'reference_field',
          ],
          '#field_collection_item' => $this
            ->mockContentEntity([
            'entity_1',
            'entity_2',
          ]),
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldTargetEntityTest::$extension protected property The Twig extension under test.
FieldTargetEntityTest::mockContentEntity protected function Returns a mock Content Entity object.
FieldTargetEntityTest::providerTestTargetEntity public function Provides data and expected results for the test method.
FieldTargetEntityTest::setUp protected function Overrides UnitTestCase::setUp
FieldTargetEntityTest::testTargetEntity public function Asserts the twig field_target_entity filter.
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.