class DimensionTest
Tests dimension value plugin.
Attributes
#[CoversClass(Dimension::class)]
#[Group('design_token')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Theme\DesignToken\DesignTokenValuePluginTestBase extends \Drupal\Tests\UnitTestCase
- class \Drupal\Tests\Core\Theme\DesignToken\Plugin\DimensionTest extends \Drupal\Tests\Core\Theme\DesignToken\DesignTokenValuePluginTestBase
- class \Drupal\Tests\Core\Theme\DesignToken\DesignTokenValuePluginTestBase extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of DimensionTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Theme/ DesignToken/ Plugin/ DimensionTest.php, line 18
Namespace
Drupal\Tests\Core\Theme\DesignToken\PluginView source
class DimensionTest extends DesignTokenValuePluginTestBase {
/**
* {@inheritdoc}
*/
protected string $pluginId = 'dimension';
/**
* {@inheritdoc}
*/
public function testToDtcg(mixed $value, mixed $expected) : void {
$this->assertIsArray($value);
$this->assertIsArray($expected);
$plugin = new Dimension($value, $this->pluginId, []);
$this->assertSame($expected, $plugin->toDtcg());
}
/**
* Data provider for ::testToDtcg().
*
* @return \Generator
* The test cases.
*/
public static function providerToDtcg() : iterable {
yield 'px' => [
[
'value' => 12,
'unit' => 'px',
],
[
'value' => 12,
'unit' => 'px',
],
];
yield 'rem' => [
[
'value' => 2,
'unit' => 'rem',
],
[
'value' => 2,
'unit' => 'rem',
],
];
yield 'with enum' => [
[
'value' => 2,
'unit' => DimensionUnit::Px,
],
[
'value' => 2,
'unit' => 'px',
],
];
}
/**
* {@inheritdoc}
*/
public function testToCss(mixed $value, string $expected) : void {
$this->assertIsArray($value);
$plugin = new Dimension($value, $this->pluginId, []);
$this->assertSame($expected, $plugin->toCss());
}
/**
* Data provider for ::testToCss().
*
* @return \Generator
* The test cases.
*/
public static function providerToCss() : iterable {
yield 'px' => [
[
'value' => 12,
'unit' => 'px',
],
'12px',
];
yield 'rem' => [
[
'value' => 2,
'unit' => 'rem',
],
'2rem',
];
}
/**
* Tests invalid unit.
*/
public function testInvalidUnit() : void {
$this->expectException(\ValueError::class);
$this->expectExceptionMessage('"<script>alert()</script>" is not a valid backing value for enum Drupal\\Core\\Theme\\DesignToken\\Enum\\DimensionUnit');
$configuration = [
'value' => 2,
'unit' => '<script>alert()</script>',
];
new Dimension($configuration, $this->pluginId, []);
}
/**
* Tests creating a dimension plugin with an enum unit.
*/
public function testEnumUnit() : void {
$configuration = [
'value' => 2,
'unit' => DimensionUnit::Rem,
];
$plugin = new Dimension($configuration, $this->pluginId, []);
$this->assertEquals(DimensionUnit::Rem, $plugin->getConfiguration()['unit']);
// Tests with another DimensionUnitInterface than the default one.
$configuration = [
'value' => 2,
'unit' => TestDimensionUnit::Em,
];
$plugin = new Dimension($configuration, $this->pluginId, []);
$this->assertEquals(TestDimensionUnit::Em, $plugin->getConfiguration()['unit']);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.