class TwigExtensionFiltersTest in Components! 8.2
Same name and namespace in other branches
- 3.x tests/src/Unit/TwigExtensionFiltersTest.php \Drupal\Tests\components\Unit\TwigExtensionFiltersTest
@coversDefaultClass \Drupal\components\Template\TwigExtension @group components
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\components\Unit\TwigExtensionFiltersTest
Expanded class hierarchy of TwigExtensionFiltersTest
File
- tests/
src/ Unit/ TwigExtensionFiltersTest.php, line 13
Namespace
Drupal\Tests\components\UnitView source
class TwigExtensionFiltersTest extends UnitTestCase {
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $renderer;
/**
* The system under test.
*
* @var \Drupal\components\Template\TwigExtension
*/
protected $systemUnderTest;
/**
* The Twig CoreExtension.
*
* @var \Twig\Extension\CoreExtension
*/
protected $coreExtension;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->renderer = $this
->createMock('\\Drupal\\Core\\Render\\RendererInterface');
$this->systemUnderTest = new TwigExtension();
// Load the Twig CoreExtension as its file contains static functions used by
// TwigExtension.
$this->coreExtension = new CoreExtension();
}
/**
* Tests exceptions during recursive_merge filter.
*
* @covers ::recursiveMergeFilter
*/
public function testRecursiveMergeFilterException() {
try {
TwigExtension::recursiveMergeFilter('not-an-array', [
'key' => 'value',
]);
$exception = FALSE;
} catch (\Exception $e) {
$this
->assertStringContainsString('The recursive_merge filter only works on arrays or "Traversable" objects, got "string".', $e
->getMessage());
$exception = TRUE;
}
if (!$exception) {
$this
->fail('Expected Exception, none was thrown.');
}
}
/**
* Tests the recursive_merge filter.
*
* @param array $element
* The element to alter.
* @param array $value
* The value to set.
* @param array $expected
* The expected result.
*
* @covers ::recursiveMergeFilter
*
* @dataProvider providerTestRecursiveMergeFilter
*/
public function testRecursiveMergeFilter(array $element, array $value, array $expected) {
$result = NULL;
try {
$result = TwigExtension::recursiveMergeFilter($element, $value);
} catch (\Exception $e) {
$this
->fail('No Exception expected; "' . $e
->getMessage() . '" thrown during: ' . $this
->getName());
}
$this
->assertEquals($expected, $result, $this
->getName());
$this
->assertEquals(array_replace_recursive($element, $value), $result, $this
->getName());
}
/**
* Data provider for testRecursiveMergeFilter().
*
* @see testRecursiveMergeFilter()
*/
public function providerTestRecursiveMergeFilter() : array {
return [
'Recursively sets values' => [
'element' => [
'existing' => 'value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
],
'id' => 'element',
],
],
],
'value' => [
'extra' => 'extra-value',
'element' => [
'#attributes' => [
'class' => [
'new-value',
],
'placeholder' => 'Label',
],
],
],
'expected' => [
'existing' => 'value',
'extra' => 'extra-value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'new-value',
'old-value-2',
],
'id' => 'element',
'placeholder' => 'Label',
],
],
],
],
];
}
/**
* Tests exceptions during set filter.
*
* @covers ::setFilter
*/
public function testSetFilterException() {
try {
TwigExtension::setFilter('not-an-array', 'key', 'value');
$exception = FALSE;
} catch (\Exception $e) {
$needle = 'The set filter only works on arrays or "Traversable" objects, got "string".';
if (method_exists($this, 'assertStringContainsString')) {
$this
->assertStringContainsString($needle, $e
->getMessage());
}
else {
$this
->assertContains($needle, $e
->getMessage());
}
$exception = TRUE;
}
if (!$exception) {
$this
->fail('Expected Exception, none was thrown.');
}
}
/**
* Tests exceptions during set filter.
*
* @covers ::setFilter
*/
public function testSetFilterMissingArgumentException() {
try {
TwigExtension::setFilter([
'an-array',
], NULL, 'value');
$exception = FALSE;
} catch (\Exception $e) {
$this
->assertStringContainsString('Value for argument "at" is required for filter "set".', $e
->getMessage());
$exception = TRUE;
}
if (!$exception) {
$this
->fail('Expected Exception, none was thrown.');
}
}
/**
* Tests the set filter.
*
* @param array $element
* The element to alter.
* @param string|array $at
* The dotted-path to the deeply nested element to set. (Or an array value
* to merge, if using the backwards-compatible 8.x-2.x syntax.)
* @param mixed $value
* The value to set.
* @param array $expected
* The expected result.
*
* @covers ::setFilter
*
* @dataProvider providerTestSetFilter
*/
public function testSetFilter(array $element, $at, $value, array $expected) {
$result = NULL;
try {
$result = TwigExtension::setFilter($element, $at, $value);
} catch (\Exception $e) {
$this
->fail('No Exception expected; "' . $e
->getMessage() . '" thrown during: ' . $this
->getName());
}
$this
->assertEquals($expected, $result, $this
->getName());
}
/**
* Data provider for testSetFilter().
*
* @see testSetFilter()
*/
public function providerTestSetFilter() : array {
return [
'Sets a new value' => [
'element' => [
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
],
'id' => 'element',
],
],
],
'at' => 'element.#attributes.placeholder',
'value' => 'Label',
'expected' => [
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
],
'id' => 'element',
'placeholder' => 'Label',
],
],
],
],
'Replaces a targeted array' => [
'element' => [
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
],
'id' => 'element',
],
],
],
'at' => 'element.#attributes.class',
'value' => [
'new-value',
],
'expected' => [
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'new-value',
],
'id' => 'element',
],
],
],
],
'Uses 8.x-2.x syntax for backwards-compatibility' => [
'element' => [
'existing' => 'value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
],
'id' => 'element',
],
],
],
'at' => [
'extra' => 'extra-value',
'element' => [
'#attributes' => [
'class' => [
'new-value',
],
'placeholder' => 'Label',
],
],
],
'value' => NULL,
'expected' => [
'existing' => 'value',
'extra' => 'extra-value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'new-value',
'old-value-2',
],
'id' => 'element',
'placeholder' => 'Label',
],
],
],
],
];
}
/**
* Tests exceptions during add filter.
*
* @covers ::addFilter
*/
public function testAddFilterException() {
try {
TwigExtension::addFilter('not-an-array', 'key', 'value');
$exception = FALSE;
} catch (\Exception $e) {
$needle = 'The add filter only works on arrays or "Traversable" objects, got "string".';
if (method_exists($this, 'assertStringContainsString')) {
$this
->assertStringContainsString($needle, $e
->getMessage());
}
else {
$this
->assertContains($needle, $e
->getMessage());
}
$exception = TRUE;
}
if (!$exception) {
$this
->fail('Expected Exception, none was thrown.');
}
}
/**
* Tests exceptions during add filter.
*
* @covers ::addFilter
*/
public function testAddFilterMissingArgumentException() {
try {
TwigExtension::addFilter([
'an-array',
], NULL, 'value');
$exception = FALSE;
} catch (\Exception $e) {
$this
->assertStringContainsString('Value for argument "at" is required for filter "add".', $e
->getMessage());
$exception = TRUE;
}
if (!$exception) {
$this
->fail('Expected Exception, none was thrown.');
}
}
/**
* Tests the add filter.
*
* @param string $at
* The dotted-path to the deeply nested element to add.
* @param mixed $value
* The value(s) to add.
* @param array $expected
* The expected render array.
*
* @covers ::addFilter
*
* @dataProvider providerTestAddFilter
*/
public function testAddFilter(string $at, $value, array $expected) {
$element = [
'existing' => 'value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
],
'id' => 'element',
],
],
];
$result = NULL;
try {
$result = TwigExtension::addFilter($element, $at, $value);
} catch (\Exception $e) {
$this
->fail('No Exception expected; "' . $e
->getMessage() . '" thrown during: ' . $this
->getName());
}
$this
->assertEquals($expected, $result, 'Failed to replace a value.');
}
/**
* Data provider for testAddFilter().
*
* @see testAddFilter()
*/
public function providerTestAddFilter() : array {
return [
'replacing a value' => [
'at' => 'element.#attributes.id',
'value' => 'new-value',
'expected' => [
'existing' => 'value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
],
'id' => 'new-value',
],
],
],
],
'setting a new property on an existing array' => [
'at' => 'element.#attributes.placeholder',
'value' => 'new-value',
'expected' => [
'existing' => 'value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
],
'id' => 'element',
'placeholder' => 'new-value',
],
],
],
],
'targeting an existing array with a string' => [
'at' => 'element.#attributes.class',
'value' => 'new-value',
'expected' => [
'existing' => 'value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
'new-value',
],
'id' => 'element',
],
],
],
],
'targeting an existing array with an array' => [
'at' => 'element.#attributes.class',
'value' => [
'new-value-1',
'new-value-2',
],
'expected' => [
'existing' => 'value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
'new-value-1',
'new-value-2',
],
'id' => 'element',
],
],
],
],
'targeting a non-existent parent property' => [
'at' => 'new-element.#attributes.class',
'value' => [
'new-value',
],
'expected' => [
'existing' => 'value',
'element' => [
'#type' => 'element',
'#attributes' => [
'class' => [
'old-value-1',
'old-value-2',
],
'id' => 'element',
],
],
'new-element' => [
'#attributes' => [
'class' => [
'new-value',
],
],
],
],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
TwigExtensionFiltersTest:: |
protected | property | The Twig CoreExtension. | |
TwigExtensionFiltersTest:: |
protected | property | The renderer. | |
TwigExtensionFiltersTest:: |
protected | property | The system under test. | |
TwigExtensionFiltersTest:: |
public | function | Data provider for testAddFilter(). | |
TwigExtensionFiltersTest:: |
public | function | Data provider for testRecursiveMergeFilter(). | |
TwigExtensionFiltersTest:: |
public | function | Data provider for testSetFilter(). | |
TwigExtensionFiltersTest:: |
public | function |
Overrides UnitTestCase:: |
|
TwigExtensionFiltersTest:: |
public | function | Tests the add filter. | |
TwigExtensionFiltersTest:: |
public | function | Tests exceptions during add filter. | |
TwigExtensionFiltersTest:: |
public | function | Tests exceptions during add filter. | |
TwigExtensionFiltersTest:: |
public | function | Tests the recursive_merge filter. | |
TwigExtensionFiltersTest:: |
public | function | Tests exceptions during recursive_merge filter. | |
TwigExtensionFiltersTest:: |
public | function | Tests the set filter. | |
TwigExtensionFiltersTest:: |
public | function | Tests exceptions during set filter. | |
TwigExtensionFiltersTest:: |
public | function | Tests exceptions during set filter. | |
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. |