Skip to content

Commit ce95793

Browse files
committed
Added tests for AssetPathResolver and ThemePackage
1 parent 1e2e873 commit ce95793

4 files changed

Lines changed: 211 additions & 1 deletion

File tree

Asset/AssetPathResolver.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public function __construct(array $designPaths, $webRootDir, LoggerInterface $lo
5050
public function setCurrentDesign($currentDesign)
5151
{
5252
$this->currentDesign = $currentDesign;
53+
if (!isset($this->designPaths[$currentDesign])) {
54+
$this->designPaths[$currentDesign] = [];
55+
}
5356
}
5457

5558
public function resolveAssetPath($path)
@@ -68,7 +71,7 @@ public function resolveAssetPath($path)
6871
$this->logger->warning(
6972
"Asset '$path' cannot be found in any configured themes.\nTried directories: ".implode(
7073
', ',
71-
array_values($this->designPaths)
74+
array_values($this->designPaths[$this->currentDesign])
7275
)
7376
);
7477
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the EzCoreExtraBundle package.
5+
*
6+
* (c) Jérôme Vieilledent <jerome@vieilledent.fr>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Lolautruche\EzCoreExtraBundle\Tests\Asset;
13+
14+
use Lolautruche\EzCoreExtraBundle\Asset\AssetPathResolver;
15+
use PHPUnit_Framework_TestCase;
16+
use org\bovigo\vfs\vfsStream;
17+
18+
class AssetPathResolverTest extends PHPUnit_Framework_TestCase
19+
{
20+
public function testResolveAssetPathFail()
21+
{
22+
$logger = $this->getMock('\Psr\Log\LoggerInterface');
23+
$logger
24+
->expects($this->once())
25+
->method('warning');
26+
27+
$resolver = new AssetPathResolver([], __DIR__, $logger);
28+
$resolver->setCurrentDesign('foo');
29+
$assetPath = 'images/foo.png';
30+
self::assertSame($assetPath, $resolver->resolveAssetPath($assetPath));
31+
}
32+
33+
public function resolveAssetPathProvider()
34+
{
35+
return [
36+
[
37+
[
38+
'foo' => [
39+
'themes/theme1',
40+
'themes/theme2',
41+
'themes/theme3',
42+
]
43+
],
44+
['themes/theme2', 'themes/theme3'],
45+
'images/foo.png',
46+
'themes/theme2/images/foo.png'
47+
],
48+
[
49+
[
50+
'foo' => [
51+
'themes/theme1',
52+
'themes/theme2',
53+
'themes/theme3',
54+
]
55+
],
56+
['themes/theme2'],
57+
'images/foo.png',
58+
'themes/theme2/images/foo.png'
59+
],
60+
[
61+
[
62+
'foo' => [
63+
'themes/theme1',
64+
'themes/theme2',
65+
'themes/theme3',
66+
]
67+
],
68+
['themes/theme1', 'themes/theme2', 'themes/theme3'],
69+
'images/foo.png',
70+
'themes/theme1/images/foo.png'
71+
],
72+
[
73+
[
74+
'foo' => [
75+
'themes/theme1',
76+
'themes/theme2',
77+
'themes/theme3',
78+
]
79+
],
80+
['themes/theme3'],
81+
'images/foo.png',
82+
'themes/theme3/images/foo.png'
83+
],
84+
[
85+
[
86+
'foo' => [
87+
'themes/theme1',
88+
'themes/theme2',
89+
'themes/theme3',
90+
]
91+
],
92+
[],
93+
'images/foo.png',
94+
'images/foo.png'
95+
],
96+
];
97+
}
98+
99+
/**
100+
* @dataProvider resolveAssetPathProvider
101+
*/
102+
public function testResolveAssetPath(array $designPaths, array $existingPaths, $path, $resolvedPath)
103+
{
104+
$webrootDir = vfsStream::setup('web');
105+
foreach ($designPaths['foo'] as $designPath) {
106+
if (in_array($designPath, $existingPaths)) {
107+
$fileInfo = new \SplFileInfo($designPath.'/'.$path);
108+
$parent = $webrootDir;
109+
foreach (explode('/', $fileInfo->getPath()) as $dir) {
110+
if (!$parent->hasChild($dir)) {
111+
$directory = vfsStream::newDirectory($dir)->at($parent);
112+
} else {
113+
$directory = $parent->getChild($dir);
114+
}
115+
116+
$parent = $directory;
117+
}
118+
119+
vfsStream::newFile($fileInfo->getFilename())->at($parent)->setContent('Vive le sucre !!!');
120+
}
121+
}
122+
123+
$resolver = new AssetPathResolver($designPaths, $webrootDir->url());
124+
$resolver->setCurrentDesign('foo');
125+
self::assertSame($resolvedPath, $resolver->resolveAssetPath($path));
126+
}
127+
}

Tests/Asset/ThemePackageTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the EzCoreExtraBundle package.
5+
*
6+
* (c) Jérôme Vieilledent <jerome@vieilledent.fr>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Lolautruche\EzCoreExtraBundle\Tests\Asset;
13+
14+
use Lolautruche\EzCoreExtraBundle\Asset\ThemePackage;
15+
use PHPUnit_Framework_TestCase;
16+
17+
class ThemePackageTest extends PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var \PHPUnit_Framework_MockObject_MockObject|\Lolautruche\EzCoreExtraBundle\Asset\AssetPathResolverInterface
21+
*/
22+
private $assetPathResolver;
23+
24+
/**
25+
* @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Asset\PackageInterface
26+
*/
27+
private $innerPackage;
28+
29+
protected function setUp()
30+
{
31+
parent::setUp();
32+
33+
$this->assetPathResolver = $this->getMock('\Lolautruche\EzCoreExtraBundle\Asset\AssetPathResolverInterface');
34+
$this->innerPackage = $this->getMock('\Symfony\Component\Asset\PackageInterface');
35+
}
36+
37+
public function testGetUrl()
38+
{
39+
$assetPath = 'images/foo.png';
40+
$fullAssetPath = 'assets/'.$assetPath;
41+
42+
$this->assetPathResolver
43+
->expects($this->once())
44+
->method('resolveAssetPath')
45+
->with($assetPath)
46+
->willReturn($fullAssetPath);
47+
$this->innerPackage
48+
->expects($this->once())
49+
->method('getUrl')
50+
->with($fullAssetPath)
51+
->willReturn("/$fullAssetPath");
52+
53+
$package = new ThemePackage($this->assetPathResolver, $this->innerPackage);
54+
self::assertSame("/$fullAssetPath", $package->getUrl($assetPath));
55+
}
56+
57+
public function testGetVersion()
58+
{
59+
$assetPath = 'images/foo.png';
60+
$fullAssetPath = 'assets/'.$assetPath;
61+
62+
$this->assetPathResolver
63+
->expects($this->once())
64+
->method('resolveAssetPath')
65+
->with($assetPath)
66+
->willReturn($fullAssetPath);
67+
$version = 'v1';
68+
$this->innerPackage
69+
->expects($this->once())
70+
->method('getVersion')
71+
->with($fullAssetPath)
72+
->willReturn($version);
73+
74+
$package = new ThemePackage($this->assetPathResolver, $this->innerPackage);
75+
self::assertSame($version, $package->getVersion($assetPath));
76+
}
77+
}

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
],
1414
"autoload": {
1515
"psr-4": {"Lolautruche\\EzCoreExtraBundle\\": ""}
16+
},
17+
"require-dev": {
18+
"mikey179/vfsStream": "^1.6.3"
1619
}
1720
}

0 commit comments

Comments
 (0)