-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathAssetCompressMiddlewareTest.php
More file actions
183 lines (149 loc) · 5.29 KB
/
Copy pathAssetCompressMiddlewareTest.php
File metadata and controls
183 lines (149 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types=1);
namespace AssetCompress\Test\TestCase\Middleware;
use AssetCompress\Middleware\AssetCompressMiddleware;
use Cake\Core\Configure;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\TestSuite\TestCase;
use MiniAsset\AssetConfig;
class AssetCompressMiddlewareTest extends TestCase
{
protected $nextInvoked = false;
protected $testConfig;
protected $middleware;
protected $request;
protected $response;
protected $handler;
/**
* Setup method
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->testConfig = APP . 'config' . DS . 'integration.ini';
$this->nextInvoked = false;
$this->loadPlugins(['TestAssetIni']);
$config = new AssetConfig([]);
$config->load($this->testConfig);
$config->load(APP . 'Plugin/TestAssetIni/config/asset_compress.ini', 'TestAssetIni.');
$config->load(APP . 'Plugin/TestAssetIni/config/asset_compress.local.ini', 'TestAssetIni.');
$this->middleware = new AssetCompressMiddleware($config);
$this->request = new ServerRequest();
$this->response = new Response();
$this->handler = new RequestHandlerStub(function () {
$this->nextInvoked = true;
return new Response();
});
}
/**
* teardown method.
*
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
$this->clearPlugins();
}
/**
* test building assets interactively
*
* @return void
*/
public function testBuildFile()
{
$uri = $this->request->getUri()->withPath('/cache_js/libs.js');
$request = $this->request->withUri($uri);
$result = $this->middleware->process($request, $this->handler);
$body = $result->getBody()->getContents();
$this->assertStringContainsString('var BaseClass = new Class', $body);
$this->assertStringContainsString('var Template = new Class', $body);
}
public static function contentTypesProvider()
{
return [
['/cache_js/libs.js', 'application/javascript'],
['/cache_css/all.css', 'text/css'],
['/cache_svg/foo.bar.svg', 'image/svg+xml'],
];
}
/**
* test returned content types
*
* @dataProvider contentTypesProvider
* @return void
*/
public function testBuildFileContentTypes($path, $expected)
{
$uri = $this->request->getUri()->withPath($path);
$request = $this->request->withUri($uri);
$result = $this->middleware->process($request, $this->handler);
$this->assertEquals($expected, $result->getHeaderLine('Content-Type'));
}
/**
* test building plugin assets.
*
* @return void
*/
public function testPluginIniBuildFile()
{
$this->loadPlugins(['TestAssetIni']);
$uri = $this->request->getUri()->withPath('/cache_js/TestAssetIni.libs.js');
$request = $this->request->withUri($uri);
$result = $this->middleware->process($request, $this->handler);
$body = $result->getBody()->getContents();
$this->assertStringContainsString('var BaseClass = new Class', $body);
$this->assertStringContainsString('var Template = new Class', $body);
}
/**
* test that predefined builds get cached to disk.
*
* @return void
*/
public function testBuildFileIsCached()
{
$uri = $this->request->getUri()->withPath('/cache_js/libs.js');
$request = $this->request->withUri($uri);
$result = $this->middleware->process($request, $this->handler);
$body = $result->getBody()->getContents();
$this->assertStringContainsString('BaseClass', $body);
$this->assertTrue(file_exists(CACHE . 'asset_compress' . DS . 'libs.js'), 'Cache file was created.');
unlink(CACHE . 'asset_compress' . DS . 'libs.js');
}
public function testProductionMode()
{
Configure::write('debug', false);
$uri = $this->request->getUri()->withPath('/cache_js/libs.js');
$request = $this->request->withUri($uri);
$this->middleware->process($request, $this->handler);
$this->assertTrue($this->nextInvoked);
}
public function testBuildThemedAsset()
{
$this->loadPlugins(['Blue']);
$configFile = APP . 'config' . DS . 'themed.ini';
$map = [
'WEBROOT' => WWW_ROOT,
'TEST_FILES' => APP,
];
$config = new AssetConfig([], $map);
$config->load($configFile);
$this->middleware = new AssetCompressMiddleware($config);
$uri = $this->request->getUri()->withPath('/cache_css/themed.css');
$request = $this->request->withUri($uri)
->withQueryParams(['theme' => 'Blue']);
$result = $this->middleware->process($request, $this->handler);
$body = $result->getBody()->getContents();
$this->assertStringContainsString('color: blue', $body);
}
public function testDelegateOnUndefinedAsset()
{
$uri = $this->request->getUri()->withPath('/cache_js/derpy.js');
$request = $this->request->withUri($uri);
$this->middleware->process($request, $this->handler);
$this->assertTrue($this->nextInvoked);
}
}