class FieldRawValueTest in Twig Field Value 8
Same name and namespace in other branches
- 2.0.x tests/src/Unit/FieldValue/FieldRawValueTest.php \Drupal\Tests\twig_field_value\Unit\FieldValue\FieldRawValueTest
@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\FieldRawValueTest
Expanded class hierarchy of FieldRawValueTest
File
- tests/
src/ Unit/ FieldValue/ FieldRawValueTest.php, line 12
Namespace
Drupal\Tests\twig_field_value\Unit\FieldValueView source
class FieldRawValueTest extends UnitTestCase {
/**
* The Twig extension under test.
*
* @var \Drupal\twig_field_value\Twig\Extension\FieldValueExtension
*/
protected $extension;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->extension = new FieldValueExtension();
}
/**
* Returns a mock FieldItem.
*
* @param mixed $values
*
* @return \Drupal\Core\Field\FieldItemBase
*/
protected function mockFieldItem($values) {
$field_item = $this
->getMockBuilder('Drupal\\Core\\Field\\FieldItemBase')
->disableOriginalConstructor()
->getMock();
$field_item
->expects($this
->any())
->method('getValue')
->will($this
->returnValue($values));
return $field_item;
}
/**
* Asserts the twig field_raw filter.
*
* @dataProvider providerTestRawValues
* @covers ::getRawValues
*
* @param $expected_result
* @param $render_array
* @param $key
*/
public function testRawValues($expected_result, $render_array, $key) {
$result = $this->extension
->getRawValues($render_array, $key);
$this
->assertSame($expected_result, $result);
}
/**
* Provides data and expected results for the test method.
*
* @return array
* Data and expected results.
*/
public function providerTestRawValues() {
return [
// Invalid render arrays.
[
NULL,
NULL,
'',
],
[
NULL,
[],
'',
],
[
NULL,
[
'#theme' => 'field',
'#no_items' => [],
],
'',
],
[
NULL,
[
'#theme' => 'field',
'#items' => [],
],
'',
],
[
NULL,
[
'#theme' => 'field',
'#items' => $this
->mockFieldItem(NULL),
],
'',
],
// Request all values, field with single value.
[
[
'value' => 'text_value',
],
[
'#theme' => 'field',
'#items' => $this
->mockFieldItem([
[
'value' => 'text_value',
],
]),
],
'',
],
// Request all values, field with multiple values.
[
[
'alt' => 'alt_value',
'title' => 'title_value',
],
[
'#theme' => 'field',
'#items' => $this
->mockFieldItem([
[
'alt' => 'alt_value',
'title' => 'title_value',
],
]),
],
'',
],
// Request 'foo', but value not exist.
[
NULL,
[
'#theme' => 'field',
'#items' => $this
->mockFieldItem([
[
'alt' => 'alt_value',
'title' => 'title_value',
],
]),
],
'foo',
],
// Request 'alt' value, field cardinality = 1.
[
'alt_value',
[
'#theme' => 'field',
'#items' => $this
->mockFieldItem([
[
'alt' => 'alt_value',
'title' => 'title_value',
],
]),
],
'alt',
],
// Request all values, field cardinality = 2.
[
[
[
'alt' => 'alt_value_1',
'title' => 'title_value_1',
],
[
'alt' => 'alt_value_2',
'title' => 'title_value_2',
],
],
[
'#theme' => 'field',
'#items' => $this
->mockFieldItem([
[
'alt' => 'alt_value_1',
'title' => 'title_value_1',
],
[
'alt' => 'alt_value_2',
'title' => 'title_value_2',
],
]),
],
'',
],
// Request 'alt' value, field cardinality = 2.
[
[
'alt_value_1',
'alt_value_2',
],
[
'#theme' => 'field',
'#items' => $this
->mockFieldItem([
[
'alt' => 'alt_value_1',
'title' => 'title_value_1',
],
[
'alt' => 'alt_value_2',
'title' => 'title_value_2',
],
]),
],
'alt',
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FieldRawValueTest:: |
protected | property | The Twig extension under test. | |
FieldRawValueTest:: |
protected | function | Returns a mock FieldItem. | |
FieldRawValueTest:: |
public | function | Provides data and expected results for the test method. | |
FieldRawValueTest:: |
protected | function |
Overrides UnitTestCase:: |
|
FieldRawValueTest:: |
public | function | Asserts the twig field_raw 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. |