diff --git a/composer.json b/composer.json index f89f315..f40887b 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,14 @@ "psr-4": { "EzSystems\\EzPlatformCoreBundle\\": "src/EzPlatformCoreBundle/bundle/", "EzSystems\\EzPlatformEncoreBundle\\": "src/EzPlatformEncoreBundle/bundle/", - "Ibexa\\Platform\\Bundle\\Assets\\": "src/IbexaPlatformAssetsBundle/bundle/" + "Ibexa\\Platform\\Bundle\\Assets\\": "src/IbexaPlatformAssetsBundle/bundle/", + "Ibexa\\Platform\\Assets\\": "src/IbexaPlatformAssetsBundle/lib/" } }, "autoload-dev": { "psr-4": { - "EzSystems\\Tests\\EzPlatformCoreBundle\\": "tests/EzPlatformCoreBundle/bundle/" + "EzSystems\\Tests\\EzPlatformCoreBundle\\": "tests/EzPlatformCoreBundle/bundle/", + "Ibexa\\Platform\\Tests\\Assets\\": "tests/IbexaPlatformAssetsBundle/lib/" } }, "require": { diff --git a/phpunit.xml b/phpunit.xml index 7183ebf..cf667ae 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,11 +6,18 @@ convertWarningsToExceptions="true" beStrictAboutTestsThatDoNotTestAnything="true" colors="true" + bootstrap="vendor/autoload.php" > - - tests/EzPlatformCoreBundle/bundle - + + + tests/EzPlatformCoreBundle/bundle + + + tests/IbexaPlatformAssetsBundle/bundle + tests/IbexaPlatformAssetsBundle/lib + + diff --git a/src/IbexaPlatformAssetsBundle/bundle/Resources/config/services.yaml b/src/IbexaPlatformAssetsBundle/bundle/Resources/config/services.yaml index 8976790..ec20b66 100644 --- a/src/IbexaPlatformAssetsBundle/bundle/Resources/config/services.yaml +++ b/src/IbexaPlatformAssetsBundle/bundle/Resources/config/services.yaml @@ -1,4 +1,5 @@ imports: + - { resource: services/resolver.yaml } - { resource: services/twig.yaml } ## diff --git a/src/IbexaPlatformAssetsBundle/bundle/Resources/config/services/resolver.yaml b/src/IbexaPlatformAssetsBundle/bundle/Resources/config/services/resolver.yaml new file mode 100644 index 0000000..057ae30 --- /dev/null +++ b/src/IbexaPlatformAssetsBundle/bundle/Resources/config/services/resolver.yaml @@ -0,0 +1,9 @@ +services: + _defaults: + autoconfigure: true + autowire: true + public: false + + Ibexa\Platform\Assets\Resolver\IconPathResolver: ~ + + Ibexa\Platform\Assets\Resolver\IconPathResolverInterface: '@Ibexa\Platform\Assets\Resolver\IconPathResolver' diff --git a/src/IbexaPlatformAssetsBundle/bundle/Twig/Extension/IconSetExtension.php b/src/IbexaPlatformAssetsBundle/bundle/Twig/Extension/IconSetExtension.php index 4425d9c..5977ad0 100644 --- a/src/IbexaPlatformAssetsBundle/bundle/Twig/Extension/IconSetExtension.php +++ b/src/IbexaPlatformAssetsBundle/bundle/Twig/Extension/IconSetExtension.php @@ -8,25 +8,18 @@ namespace Ibexa\Platform\Bundle\Assets\Twig\Extension; -use eZ\Publish\Core\MVC\ConfigResolverInterface; -use Symfony\Component\Asset\Packages; +use Ibexa\Platform\Assets\Resolver\IconPathResolverInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; class IconSetExtension extends AbstractExtension { - /** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */ - private $configResolver; + /** @var \Ibexa\Platform\Assets\Resolver\IconPathResolverInterface */ + private $iconPathResolver; - /** @var \Symfony\Component\Asset\Packages */ - private $packages; - - public function __construct( - ConfigResolverInterface $configResolver, - Packages $packages - ) { - $this->configResolver = $configResolver; - $this->packages = $packages; + public function __construct(IconPathResolverInterface $iconPathResolver) + { + $this->iconPathResolver = $iconPathResolver; } public function getFunctions(): array @@ -44,9 +37,6 @@ public function getFunctions(): array public function getIconPath(string $icon, string $set = null): string { - $iconSetName = $set ?? $this->configResolver->getParameter('assets.default_icon_set'); - $iconSets = $this->configResolver->getParameter('assets.icon_sets'); - - return sprintf('%s#%s', $this->packages->getUrl($iconSets[$iconSetName]), $icon); + return $this->iconPathResolver->resolve($icon, $set); } } diff --git a/src/IbexaPlatformAssetsBundle/lib/Resolver/IconPathResolver.php b/src/IbexaPlatformAssetsBundle/lib/Resolver/IconPathResolver.php new file mode 100644 index 0000000..2fc8dc4 --- /dev/null +++ b/src/IbexaPlatformAssetsBundle/lib/Resolver/IconPathResolver.php @@ -0,0 +1,40 @@ +configResolver = $configResolver; + $this->packages = $packages; + } + + public function resolve(string $icon, ?string $set = null): string + { + $iconSetName = $set ?? $this->configResolver->getParameter('assets.default_icon_set'); + $iconSets = $this->configResolver->getParameter('assets.icon_sets'); + + return sprintf('%s#%s', $this->packages->getUrl($iconSets[$iconSetName]), $icon); + } +} diff --git a/src/IbexaPlatformAssetsBundle/lib/Resolver/IconPathResolverInterface.php b/src/IbexaPlatformAssetsBundle/lib/Resolver/IconPathResolverInterface.php new file mode 100644 index 0000000..7e9efdc --- /dev/null +++ b/src/IbexaPlatformAssetsBundle/lib/Resolver/IconPathResolverInterface.php @@ -0,0 +1,14 @@ +getDefaultConfig(); + + $this->configResolver = $this->getConfigResolverMock($config); + $this->packages = $this->getPackagesMock($config); + } + + /** + * @dataProvider resolveDataProvider + */ + public function testResolve(string $icon, ?string $set, string $expectedPath): void + { + $iconPathResolver = new IconPathResolver($this->configResolver, $this->packages); + + self::assertEquals($expectedPath, $iconPathResolver->resolve($icon, $set)); + } + + public function resolveDataProvider(): array + { + return [ + [ + 'bookmark', + 'my_icon_set', + '/bundles/mybundle/my-icons.svg#bookmark', + ], + [ + 'folder', + null, + '/bundles/mybundle/my-icons.svg#folder', + ], + [ + 'bookmark', + 'my_other_icon_set', + '/bundles/my_other_icon_set/my-other-icons.svg#bookmark', + ], + ]; + } + + private function getDefaultConfig(): array + { + return [ + 'icon_sets' => [ + 'my_icon_set' => '/bundles/mybundle/my-icons.svg', + 'my_other_icon_set' => '/bundles/my_other_icon_set/my-other-icons.svg', + ], + 'default_icon_set' => 'my_icon_set', + ]; + } + + private function getConfigResolverMock(array $config): ConfigResolverInterface + { + $configResolver = $this->createMock(ConfigResolverInterface::class); + $configResolver->method('getParameter')->willReturnMap([ + ['assets.icon_sets', null, null, $config['icon_sets']], + ['assets.default_icon_set', null, null, $config['default_icon_set']], + ]); + + return $configResolver; + } + + private function getPackagesMock(array $config): Packages + { + $packages = $this->createMock(Packages::class); + $packages->method('getUrl')->willReturnMap([ + [$config['icon_sets']['my_icon_set'], null, $config['icon_sets']['my_icon_set']], + [$config['icon_sets']['my_other_icon_set'], null, $config['icon_sets']['my_other_icon_set']], + ]); + + return $packages; + } +}