1
|
|
|
<?php |
2
|
|
|
namespace AssetManagerTest\View\Helper; |
3
|
|
|
|
4
|
|
|
use AssetManager\Core\Cache\FilePathCache; |
5
|
|
|
use AssetManager\Core\Resolver\MapResolver; |
6
|
|
|
use AssetManager\Core\Resolver\MimeResolverAwareInterface; |
7
|
|
|
use AssetManager\Core\Service\MimeResolver; |
8
|
|
|
use AssetManager\View\Helper\Asset; |
9
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
10
|
|
|
use Zend\ServiceManager\Config; |
11
|
|
|
use Zend\ServiceManager\ServiceManager; |
12
|
|
|
use Zend\View\HelperPluginManager; |
13
|
|
|
|
14
|
|
|
class AssetTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
private function getGenericResolver() |
17
|
|
|
{ |
18
|
|
|
$resolver = new MapResolver; |
19
|
|
|
|
20
|
|
|
$this->assertTrue($resolver instanceof MimeResolverAwareInterface); |
21
|
|
|
|
22
|
|
|
$mimeResolver = new MimeResolver; |
23
|
|
|
|
24
|
|
|
$resolver->setMimeResolver($mimeResolver); |
25
|
|
|
|
26
|
|
|
return $resolver; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testInvoke() |
30
|
|
|
{ |
31
|
|
|
$configWithCache = array( |
32
|
|
|
'view_helper' => array( |
33
|
|
|
'append_timestamp' => true, |
34
|
|
|
'query_string' => '_', |
35
|
|
|
'cache' => null, |
36
|
|
|
), |
37
|
|
|
'caching' => array( |
38
|
|
|
'default' => array( |
39
|
|
|
'cache' => FilePathCache::class, |
40
|
|
|
'options' => array( |
41
|
|
|
'dir' => 'public/assets', |
42
|
|
|
), |
43
|
|
|
), |
44
|
|
|
), |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$configWithoutCache = array( |
48
|
|
|
'view_helper' => array( |
49
|
|
|
'append_timestamp' => true, |
50
|
|
|
'query_string' => '_', |
51
|
|
|
'cache' => null, |
52
|
|
|
), |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$filename = 'porn-food/bacon.php'; |
56
|
|
|
|
57
|
|
|
$resolver = $this->getGenericResolver(); |
58
|
|
|
|
59
|
|
|
$resolver->setMap(array( |
60
|
|
|
'porn-food/bacon.php' => __FILE__, |
61
|
|
|
)); |
62
|
|
|
|
63
|
|
|
$helperWithCache = new Asset($resolver, null, $configWithCache); |
64
|
|
|
$newFilenameWithCache = $helperWithCache->__invoke($filename); |
65
|
|
|
|
66
|
|
|
// with cache file should have a timestamp query param |
67
|
|
|
$this->assertContains('?_=', $newFilenameWithCache); |
68
|
|
|
|
69
|
|
|
$helperWithoutCache = new Asset($resolver, null, $configWithoutCache); |
70
|
|
|
$newFilenameWithoutCache = $helperWithoutCache->__invoke($filename); |
71
|
|
|
|
72
|
|
|
// without cache file should have a timestamp query param |
73
|
|
|
$this->assertContains('?_=', $newFilenameWithoutCache); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testSameResultWithoutCachingConfig() |
77
|
|
|
{ |
78
|
|
|
$config = array( |
79
|
|
|
'view_helper' => array( |
80
|
|
|
'append_timestamp' => true, |
81
|
|
|
'query_string' => '_', |
82
|
|
|
'cache' => null, |
83
|
|
|
), |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$filename = 'porn-food/bac.on'; |
87
|
|
|
|
88
|
|
|
$resolver = $this->getGenericResolver(); |
89
|
|
|
|
90
|
|
|
$resolver->setMap(array( |
91
|
|
|
'porn-food/bac.on' => __FILE__, |
92
|
|
|
)); |
93
|
|
|
|
94
|
|
|
$helper = new Asset($resolver, null, $config); |
95
|
|
|
$newFilename = $helper->__invoke($filename); |
96
|
|
|
|
97
|
|
|
$this->assertContains('?_=', $newFilename); |
98
|
|
|
$this->assertNotSame($newFilename, $filename); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testForceToNotAppendTimestampWithoutCache() |
102
|
|
|
{ |
103
|
|
|
$config = array( |
104
|
|
|
'view_helper' => array( |
105
|
|
|
'append_timestamp' => false, |
106
|
|
|
'query_string' => '_', |
107
|
|
|
'cache' => null, |
108
|
|
|
), |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$filename = 'porn-food/bac.on'; |
112
|
|
|
|
113
|
|
|
$resolver = $this->getGenericResolver(); |
114
|
|
|
|
115
|
|
|
$resolver->setMap(array( |
116
|
|
|
'porn-food/bac.on' => __FILE__, |
117
|
|
|
)); |
118
|
|
|
|
119
|
|
|
$helper = new Asset($resolver, null, $config); |
120
|
|
|
$newFilename = $helper->__invoke($filename); |
121
|
|
|
|
122
|
|
|
$this->assertNotContains('?_=', $newFilename); |
123
|
|
|
$this->assertSame($newFilename, $filename); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testRetrieveHelperFromPluginManagerByClassConstant() |
127
|
|
|
{ |
128
|
|
|
$config = require __DIR__.'/../../../../config/module.config.php'; |
129
|
|
|
|
130
|
|
|
$serviceConfig = new Config($config['service_manager']); |
131
|
|
|
$serviceManager = new ServiceManager(); |
132
|
|
|
$serviceManager->setService('config', $config); |
133
|
|
|
$serviceConfig->configureServiceManager($serviceManager); |
134
|
|
|
|
135
|
|
|
$pluginManager = new HelperPluginManager($serviceManager, $config['view_helpers']); |
136
|
|
|
$this->assertInstanceOf(Asset::class, $pluginManager->get(Asset::class)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|