class PathExtractorTest

@coversDefaultClass \Drupal\Core\Theme\Plugin\IconExtractor\PathExtractor

@group icon

Hierarchy

Expanded class hierarchy of PathExtractorTest

File

core/tests/Drupal/Tests/Core/Theme/Icon/Plugin/PathExtractorTest.php, line 18

Namespace

Drupal\Tests\Core\Theme\Icon\Plugin
View source
class PathExtractorTest extends UnitTestCase {
    use IconTestTrait;
    
    /**
     * This test plugin id (icon pack id).
     */
    private string $pluginId = 'test_path';
    
    /**
     * The PathExtractor instance.
     *
     * @var \Drupal\Core\Theme\Plugin\IconExtractor\PathExtractor
     */
    private PathExtractor $pathExtractorPlugin;
    
    /**
     * The IconFinder instance.
     *
     * @var \Drupal\Core\Theme\Icon\IconFinder|\PHPUnit\Framework\MockObject\MockObject
     */
    private IconFinder $iconFinder;
    
    /**
     * {@inheritdoc}
     */
    public function setUp() : void {
        parent::setUp();
        $this->iconFinder = $this->createMock(IconFinder::class);
        $this->pathExtractorPlugin = new PathExtractor([
            'id' => $this->pluginId,
            'config' => [
                'sources' => [
                    'foo/bar/baz.svg',
                ],
            ],
            'template' => '_foo_',
            'relative_path' => 'modules/my_module',
        ], $this->pluginId, [], $this->iconFinder);
    }
    
    /**
     * Data provider for ::testDiscoverIconsPath().
     *
     * @return \Generator
     *   The test cases, icons data with expected result.
     */
    public static function providerDiscoverIconsPath() : iterable {
        (yield 'empty files' => [
            [],
            TRUE,
        ]);
        (yield 'single file' => [
            [
                'foo' => [
                    'icon_id' => 'foo',
                    'source' => 'source/foo.svg',
                    'absolute_path' => '/path/source/foo.svg',
                ],
            ],
        ]);
        (yield 'multiple files with group' => [
            [
                'foo' => [
                    'icon_id' => 'foo',
                    'source' => 'source/foo.svg',
                    'absolute_path' => '/path/source/foo.svg',
                    'group' => 'baz',
                ],
                'bar' => [
                    'icon_id' => 'bar',
                    'source' => 'source/bar.svg',
                    'absolute_path' => '/path/source/bar.svg',
                    'group' => NULL,
                ],
                'baz' => [
                    'icon_id' => 'baz',
                    'source' => 'source/baz.svg',
                    'absolute_path' => '/path/source/baz.svg',
                ],
            ],
        ]);
    }
    
    /**
     * Test the PathExtractor::discoverIcons() method.
     *
     * @param array<array<string, string>> $files
     *   The files to test from IconFinder::getFilesFromSources.
     * @param bool $expected_empty
     *   Has icon result, default FALSE.
     *
     * @dataProvider providerDiscoverIconsPath
     */
    public function testDiscoverIconsPath(array $files, bool $expected_empty = FALSE) : void {
        $this->iconFinder
            ->method('getFilesFromSources')
            ->willReturn($files);
        $result = $this->pathExtractorPlugin
            ->discoverIcons();
        if (TRUE === $expected_empty) {
            $this->assertEmpty($result);
            return;
        }
        // Result expected is keyed by icon_id with values 'source' and 'group'.
        $expected_result = [];
        foreach ($files as $icon) {
            $expected_id = $this->pluginId . IconDefinition::ICON_SEPARATOR . $icon['icon_id'];
            if (!isset($icon['group'])) {
                $icon['group'] = NULL;
            }
            unset($icon['icon_id']);
            $expected_result[$expected_id] = $icon;
        }
        $this->assertEquals($expected_result, $result);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
IconTestTrait::createTestIcon protected function Create an icon.
PathExtractorTest::$iconFinder private property The IconFinder instance.
PathExtractorTest::$pathExtractorPlugin private property The PathExtractor instance.
PathExtractorTest::$pluginId private property This test plugin id (icon pack id).
PathExtractorTest::providerDiscoverIconsPath public static function Data provider for ::testDiscoverIconsPath().
PathExtractorTest::setUp public function Overrides UnitTestCase::setUp
PathExtractorTest::testDiscoverIconsPath public function Test the PathExtractor::discoverIcons() method.
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::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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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