View source
<?php
namespace Drupal\Tests\ui_patterns\Unit\Definition;
use Drupal\Component\Serialization\Yaml;
use Drupal\Tests\ui_patterns\Unit\AbstractUiPatternsTest;
use Drupal\ui_patterns\Definition\PatternDefinition;
class PatternDefinitionTest extends AbstractUiPatternsTest {
public function testGettersSetters($getter, $name, $value) {
$pattern_definition = new PatternDefinition([
$name => $value,
]);
$this
->assertEquals(call_user_func([
$pattern_definition,
$getter,
]), $value);
}
public function testFields() {
$fields = [
'name' => [
'name' => 'name',
'label' => 'Label',
],
];
$pattern_definition = new PatternDefinition();
$pattern_definition
->setFields($fields);
$this
->assertEquals([
$fields['name']['label'],
$fields['name']['name'],
NULL,
NULL,
NULL,
], [
$pattern_definition
->getField('name')
->getLabel(),
$pattern_definition
->getField('name')
->getName(),
$pattern_definition
->getField('name')
->getType(),
$pattern_definition
->getField('name')
->getDescription(),
$pattern_definition
->getField('name')
->getPreview(),
]);
$pattern_definition
->getField('name')
->setType('type');
$pattern_definition
->getField('name')
->setPreview('preview');
$pattern_definition
->getField('name')
->setDescription('description');
$this
->assertEquals([
'type',
'description',
'preview',
], [
$pattern_definition
->getField('name')
->getType(),
$pattern_definition
->getField('name')
->getDescription(),
$pattern_definition
->getField('name')
->getPreview(),
]);
}
public function testFieldsProcessing($actual, $expected) {
$pattern_definition = new PatternDefinition();
$data = $pattern_definition
->setFields($actual)
->toArray();
$this
->assertEquals($expected, $data['fields']);
}
public function fieldsProcessingProvider() {
return Yaml::decode(file_get_contents($this
->getFixturePath() . '/definition/fields_processing.yml'));
}
public function testVariantsProcessing($actual, $expected) {
$pattern_definition = new PatternDefinition();
$data = $pattern_definition
->setVariants($actual)
->toArray();
$this
->assertEquals($expected, $data['variants']);
}
public function variantsProcessingProvider() {
return Yaml::decode(file_get_contents($this
->getFixturePath() . '/definition/variants_processing.yml'));
}
public function definitionGettersProvider() {
return [
[
'getProvider',
'provider',
'my_module',
],
[
'id',
'id',
'pattern_id',
],
[
'getLabel',
'label',
'Pattern label',
],
[
'getDescription',
'description',
'Pattern description.',
],
[
'getUse',
'use',
'template.twig',
],
[
'hasCustomThemeHook',
'custom theme hook',
FALSE,
],
[
'getThemeHook',
'theme hook',
'eme hook: custom_my_theme_hook',
],
[
'getTemplate',
'template',
'my-template.html.twig',
],
[
'getFileName',
'file name',
'/path/to/filename.ui_patterns.yml',
],
[
'getClass',
'class',
'\\Drupal\\ui_patterns\\MyClass',
],
[
'getBasePath',
'base path',
'/path/to',
],
[
'getTags',
'tags',
[
'a',
'b',
],
],
];
}
}