Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-32164: Implemented IconPathResolver service #24

Merged
merged 5 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
13 changes: 10 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="true"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuite name="EzPlatformCoreBundle">
<directory>tests/EzPlatformCoreBundle/bundle</directory>
</testsuite>
<testsuites>
<testsuite name="EzPlatformCoreBundle">
<directory>tests/EzPlatformCoreBundle/bundle</directory>
</testsuite>
<testsuite name="IbexaPlatformAssetsBundle">
<directory>tests/IbexaPlatformAssetsBundle/bundle</directory>
<directory>tests/IbexaPlatformAssetsBundle/lib</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
imports:
- { resource: services/resolver.yaml }
- { resource: services/twig.yaml }

##
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
40 changes: 40 additions & 0 deletions src/IbexaPlatformAssetsBundle/lib/Resolver/IconPathResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Platform\Assets\Resolver;

use eZ\Publish\Core\MVC\ConfigResolverInterface;
use Symfony\Component\Asset\Packages;

/**
* @internal
*/
final class IconPathResolver implements IconPathResolverInterface
{
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

/** @var \Symfony\Component\Asset\Packages */
private $packages;

public function __construct(
ConfigResolverInterface $configResolver,
Packages $packages
) {
$this->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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Platform\Assets\Resolver;

interface IconPathResolverInterface
{
public function resolve(string $icon, ?string $set = null): string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Platform\Tests\Assets\Resolver;

use eZ\Publish\Core\MVC\ConfigResolverInterface;
use Ibexa\Platform\Assets\Resolver\IconPathResolver;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Asset\Packages;

final class IconPathResolverTest extends TestCase
{
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface|\PHPUnit\Framework\MockObject\MockObject */
private $configResolver;

/** @var \PHPUnit\Framework\MockObject\MockObject|\Symfony\Component\Asset\Packages */
private $packages;

public function setUp(): void
{
$config = $this->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;
}
}