class ExtractorTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Theme/Icon/Plugin/ExtractorTest.php \Drupal\Tests\Core\Theme\Icon\Plugin\ExtractorTest

Tests icon extractor with base and finder base plugin.

Attributes

#[Group('icon')]

Hierarchy

Expanded class hierarchy of ExtractorTest

File

core/tests/Drupal/Tests/Core/Theme/Icon/Plugin/ExtractorTest.php, line 21

Namespace

Drupal\Tests\Core\Theme\Icon\Plugin
View source
class ExtractorTest extends UnitTestCase {
  
  /**
   * This test plugin id (icon pack id).
   */
  private string $pluginId = 'test_extractor';
  
  /**
   * Test the IconExtractorBase::label and IconExtractorBase::description.
   */
  public function testIconExtractorBase() : void {
    $extractorPlugin = new TestExtractor([], $this->pluginId, [
      'label' => 'foo',
      'description' => 'bar',
    ]);
    $this->assertSame('foo', $extractorPlugin->label());
    $this->assertSame('bar', $extractorPlugin->description());
  }
  
  /**
   * Test the IconExtractorBase::buildConfigurationForm.
   */
  public function testBuildConfigurationForm() : void {
    // Test no settings.
    $extractorPlugin = new TestExtractor([], $this->pluginId, []);
    $form_state = $this->getMockBuilder('Drupal\\Core\\Form\\FormState')
      ->disableOriginalConstructor()
      ->getMock();
    $result = $extractorPlugin->buildConfigurationForm([], $form_state);
    $this->assertEmpty($result);
    // Test with settings.
    $extractorPlugin = new TestExtractor([
      'settings' => [
        'foo' => [
          'type' => 'string',
        ],
      ],
    ], $this->pluginId, []);
    $subform_state = $this->createMock(SubformStateInterface::class);
    $form_state = $this->createMock(SubformStateInterface::class);
    $form_state->method('getCompleteFormState')
      ->willReturn($subform_state);
    $result = $extractorPlugin->buildConfigurationForm([], $form_state);
    $expected = [
      'foo' => [
        '#title' => 'foo',
        '#type' => 'textfield',
      ],
    ];
    $this->assertSame($expected, $result);
  }
  
  /**
   * Test the IconExtractorBase:createIcon method.
   */
  public function testCreateIcon() : void {
    $extractorPlugin = new TestExtractor([
      'id' => $this->pluginId,
      'template' => '_bar_',
    ], $this->pluginId, []);
    $icon = $extractorPlugin->createIcon('foo');
    $expected = IconDefinition::create($this->pluginId, 'foo', '_bar_', NULL, NULL, [
      'id' => $this->pluginId,
    ]);
    $this->assertEquals($expected, $icon);
    $icon = $extractorPlugin->createIcon('foo', 'bar', 'baz', [
      'corge' => 'qux',
    ]);
    $expected = IconDefinition::create($this->pluginId, 'foo', '_bar_', 'bar', 'baz', [
      'id' => $this->pluginId,
      'corge' => 'qux',
    ]);
    $this->assertEquals($expected, $icon);
  }
  
  /**
   * Test the IconExtractorBase:createIcon method with Exception.
   */
  public function testCreateIconExceptionTemplate() : void {
    $extractorPlugin = new TestExtractor([], $this->pluginId, []);
    $this->expectException(IconPackConfigErrorException::class);
    $this->expectExceptionMessage('Missing `template` in your definition, extractor test_extractor requires this value.');
    $extractorPlugin->createIcon('foo');
  }
  
  /**
   * Test the IconExtractorBase:loadIcon.
   */
  public function testLoadIcon() : void {
    $icon_data = [
      'icon_id' => 'foo',
      'source' => 'path/foo.svg',
      'group' => 'bar',
    ];
    $extractorPlugin = new TestExtractor([
      'id' => $this->pluginId,
      'template' => '_bar_',
    ], $this->pluginId, []);
    $icon = $extractorPlugin->loadIcon($icon_data);
    $this->assertInstanceOf(IconDefinitionInterface::class, $icon);
    $this->assertSame($icon_data['icon_id'], $icon->getIconId());
    $this->assertSame($icon_data['source'], $icon->getSource());
    $this->assertSame($icon_data['group'] ?? NULL, $icon->getGroup());
  }
  
  /**
   * Test the IconExtractorBase:loadIcon with missing data.
   */
  public function testLoadIconMissingData() : void {
    $extractorPlugin = new TestExtractor([
      'id' => $this->pluginId,
      'template' => '_bar_',
    ], $this->pluginId, []);
    $icon = $extractorPlugin->loadIcon([]);
    $this->assertNull($icon);
  }
  
  /**
   * Test the IconExtractorWithFinder:checkRequiredConfigSources method.
   */
  public function testDiscoverIconsExceptionSource() : void {
    $extractorPlugin = new TestExtractorWithFinder([], $this->pluginId, [], $this->createMock(IconFinder::class));
    $this->expectException(IconPackConfigErrorException::class);
    $this->expectExceptionMessage('Missing or invalid `config: sources` in your definition, extractor test_extractor requires this value as array.');
    $extractorPlugin->discoverIcons();
  }
  
  /**
   * Test the IconExtractorWithFinder:checkRequiredConfigSources method.
   */
  public function testDiscoverIconsExceptionSourceEmpty() : void {
    $pathExtractorPlugin = new TestExtractorWithFinder([
      'config' => [
        'sources' => [],
      ],
    ], $this->pluginId, [], $this->createMock(IconFinder::class));
    $this->expectException(IconPackConfigErrorException::class);
    $this->expectExceptionMessage('Missing or invalid `config: sources` in your definition, extractor test_extractor requires this value as array.');
    $pathExtractorPlugin->discoverIcons();
  }
  
  /**
   * Test the IconExtractorWithFinder::getFilesFromSources method.
   */
  public function testGetFilesFromSourcesExceptionRelativePath() : void {
    $pathExtractorPlugin = new TestExtractorWithFinder([
      'config' => [
        'sources' => [
          'foo/bar',
        ],
      ],
    ], $this->pluginId, [
      'label' => 'Test',
      'description' => 'Test description',
    ], $this->createMock(IconFinder::class));
    $this->expectException(IconPackConfigErrorException::class);
    $this->expectExceptionMessage('Empty relative path for extractor test_extractor.');
    $pathExtractorPlugin->discoverIcons();
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution.
ExpectDeprecationTrait::expectDeprecation Deprecated public function Adds an expected deprecation.
ExpectDeprecationTrait::regularExpressionForFormatDescription private function
ExtractorTest::$pluginId private property This test plugin id (icon pack id).
ExtractorTest::testBuildConfigurationForm public function Test the IconExtractorBase::buildConfigurationForm.
ExtractorTest::testCreateIcon public function Test the IconExtractorBase:createIcon method.
ExtractorTest::testCreateIconExceptionTemplate public function Test the IconExtractorBase:createIcon method with Exception.
ExtractorTest::testDiscoverIconsExceptionSource public function Test the IconExtractorWithFinder:checkRequiredConfigSources method.
ExtractorTest::testDiscoverIconsExceptionSourceEmpty public function Test the IconExtractorWithFinder:checkRequiredConfigSources method.
ExtractorTest::testGetFilesFromSourcesExceptionRelativePath public function Test the IconExtractorWithFinder::getFilesFromSources method.
ExtractorTest::testIconExtractorBase public function Test the IconExtractorBase::label and IconExtractorBase::description.
ExtractorTest::testLoadIcon public function Test the IconExtractorBase:loadIcon.
ExtractorTest::testLoadIconMissingData public function Test the IconExtractorBase:loadIcon with missing data.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
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::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 366
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.