class FieldTargetEntityTest in Twig Field Value 8
Same name and namespace in other branches
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\twig_field_value\Unit\FieldValue\FieldTargetEntityTest
Expanded class hierarchy of FieldTargetEntityTest
File
- tests/
src/ Unit/ FieldValue/ FieldTargetEntityTest.php, line 12
Namespace
Drupal\Tests\twig_field_value\Unit\FieldValueView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FieldTargetEntityTest:: |
protected | property | The Twig extension under test. | |
FieldTargetEntityTest:: |
protected | function | Returns a mock Content Entity object. | |
FieldTargetEntityTest:: |
public | function | Provides data and expected results for the test method. | |
FieldTargetEntityTest:: |
protected | function |
Overrides UnitTestCase:: |
|
FieldTargetEntityTest:: |
public | function | Asserts the twig field_target_entity filter. | |
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. | |
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. |