You are here

class PatternDefinitionTest in UI Patterns 8

@coversDefaultClass \Drupal\ui_patterns\Definition\PatternDefinition

@group ui_patterns

Hierarchy

Expanded class hierarchy of PatternDefinitionTest

File

tests/src/Unit/Definition/PatternDefinitionTest.php, line 14

Namespace

Drupal\Tests\ui_patterns\Unit\Definition
View source
class PatternDefinitionTest extends AbstractUiPatternsTest {

  /**
   * Test getters.
   *
   * @dataProvider definitionGettersProvider
   */
  public function testGettersSetters($getter, $name, $value) {
    $pattern_definition = new PatternDefinition([
      $name => $value,
    ]);
    $this
      ->assertEquals(call_user_func([
      $pattern_definition,
      $getter,
    ]), $value);
  }

  /**
   * Test field singleton.
   *
   * @dataProvider definitionGettersProvider
   */
  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(),
    ]);
  }

  /**
   * Test fields processing.
   *
   * @dataProvider fieldsProcessingProvider
   */
  public function testFieldsProcessing($actual, $expected) {
    $pattern_definition = new PatternDefinition();
    $data = $pattern_definition
      ->setFields($actual)
      ->toArray();
    $this
      ->assertEquals($expected, $data['fields']);
  }

  /**
   * Provider.
   *
   * @return array
   *   Data.
   */
  public function fieldsProcessingProvider() {
    return Yaml::decode(file_get_contents($this
      ->getFixturePath() . '/definition/fields_processing.yml'));
  }

  /**
   * Test fields processing.
   *
   * @dataProvider variantsProcessingProvider
   */
  public function testVariantsProcessing($actual, $expected) {
    $pattern_definition = new PatternDefinition();
    $data = $pattern_definition
      ->setVariants($actual)
      ->toArray();
    $this
      ->assertEquals($expected, $data['variants']);
  }

  /**
   * Provider.
   *
   * @return array
   *   Data.
   */
  public function variantsProcessingProvider() {
    return Yaml::decode(file_get_contents($this
      ->getFixturePath() . '/definition/variants_processing.yml'));
  }

  /**
   * Provider.
   *
   * @return array
   *   Data.
   */
  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',
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractUiPatternsTest::getFixturePath protected function Get fixtures base path.
PatternDefinitionTest::definitionGettersProvider public function Provider.
PatternDefinitionTest::fieldsProcessingProvider public function Provider.
PatternDefinitionTest::testFields public function Test field singleton.
PatternDefinitionTest::testFieldsProcessing public function Test fields processing.
PatternDefinitionTest::testGettersSetters public function Test getters.
PatternDefinitionTest::testVariantsProcessing public function Test fields processing.
PatternDefinitionTest::variantsProcessingProvider public function Provider.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340