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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Deploy;
use Magento\Deploy\Package\Processor\PreProcessor\Less;
use Magento\Deploy\Service\DeployStaticContent;
use Magento\Deploy\Strategy\DeployStrategyFactory;
use Magento\Framework\App\State;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\ReadInterface;
use Magento\Deploy\Console\DeployStaticOptions as Options;
use Magento\Framework\Config\View;
use Magento\Deploy\Config\BundleConfig;
use Magento\Framework\Filesystem\Directory\WriteInterface;
/**
* Class DeployTest
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class DeployTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var WriteInterface
*/
private $staticDir;
/**
* @var ReadInterface
*/
private $rootDir;
/**
* @var DeployStaticContent
*/
private $deployService;
/**
* @var View
*/
private $config;
/**
* @var BundleConfig
*/
private $bundleConfig;
/**
* @var DeployStaticContent
*/
private $staticContentService;
/**
* @var string
*/
private $prevMode;
/**
* @var array
*/
private $options = [
Options::DRY_RUN => false,
Options::NO_JAVASCRIPT => false,
Options::NO_CSS => false,
Options::NO_LESS => false,
Options::NO_IMAGES => false,
Options::NO_FONTS => false,
Options::NO_HTML => false,
Options::NO_MISC => false,
Options::NO_HTML_MINIFY => false,
Options::AREA => ['frontend'],
Options::EXCLUDE_AREA => ['none'],
Options::THEME => ['Magento/zoom1', 'Magento/zoom2', 'Magento/zoom3'],
Options::EXCLUDE_THEME => ['none'],
Options::LANGUAGE => ['en_US', 'fr_FR', 'pl_PL'],
Options::EXCLUDE_LANGUAGE => ['none'],
Options::JOBS_AMOUNT => 0,
Options::SYMLINK_LOCALE => false,
Options::STRATEGY => DeployStrategyFactory::DEPLOY_STRATEGY_COMPACT,
];
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->prevMode = $objectManager->get(State::class)->getMode();
$objectManager->get(State::class)->setMode(State::MODE_PRODUCTION);
$this->filesystem = $objectManager->get(Filesystem::class);
$this->staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
$this->rootDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT);
$logger = $objectManager->get(\Psr\Log\LoggerInterface::class);
$this->deployService = $objectManager->create(DeployStaticContent::class, [
'logger' => $logger
]);
$this->bundleConfig = $objectManager->create(BundleConfig::class);
$this->config = $objectManager->create(View::class);
$this->staticContentService = $objectManager->create(DeployStaticContent::class);
$this->filesystem->getDirectoryWrite(DirectoryList::PUB)->delete(DirectoryList::STATIC_VIEW);
$this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)->delete(DirectoryList::TMP_MATERIALIZATION_DIR);
}
protected function tearDown()
{
$objectManager = Bootstrap::getObjectManager();
$objectManager->get(State::class)->setMode($this->prevMode);
$this->filesystem = $objectManager->get(Filesystem::class);
$this->filesystem->getDirectoryWrite(DirectoryList::PUB)->delete(DirectoryList::STATIC_VIEW);
$this->staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
$this->staticDir->getDriver()->createDirectory($this->staticDir->getAbsolutePath());
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Deploy/_files/theme.php
*/
public function testDeploy()
{
$this->deployService->deploy($this->options);
$this->assertFileExists($this->staticDir->getAbsolutePath('frontend/Magento/zoom1/default/css/root.css'));
$this->assertFileExists($this->staticDir->getAbsolutePath('frontend/Magento/zoom2/default/css/root.css'));
$this->assertFileExists($this->staticDir->getAbsolutePath('frontend/Magento/zoom3/default/css/root.css'));
$this->assertFileExists($this->staticDir->getAbsolutePath('frontend/Magento/zoom3/default/css/local.css'));
$this->assertFileExistsIsGenerated('requirejs-config.js');
$this->assertFileExistsIsGenerated('requirejs-map.js');
$this->assertFileExistsIsGenerated('map.json');
$this->assertFileExistsIsGenerated('js-translation.json');
$this->assertFileExistsIsGenerated('result_map.json');
$actualFileContent = $this->staticDir->readFile('frontend/Magento/zoom3/default/css/root.css');
$this->assertLessPreProcessor($actualFileContent);
$this->assertCssUrlFixerPostProcessor($actualFileContent);
foreach (['Magento/zoom1', 'Magento/zoom2', 'Magento/zoom3'] as $theme) {
$this->assertBundleSize($theme);
$this->assertExcluded($theme, $this->config->getExcludedFiles());
$this->assertExcluded($theme, $this->config->getExcludedDir());
}
}
/**
* Assert file exists in all themes and locales
*
* @param string $fileName
* @return void
*/
private function assertFileExistsIsGenerated($fileName)
{
foreach (['Magento/zoom1', 'Magento/zoom2', 'Magento/zoom3'] as $theme) {
foreach ($this->options[Options::LANGUAGE] as $locale) {
$this->assertFileExists(
$this->staticDir->getAbsolutePath(
'frontend/' . $theme . '/' . $locale . '/' . $fileName
)
);
}
}
}
/**
* Assert Less pre-processor
*
* @see Less
* @param $actualRootCssContent
* @return void
*/
private function assertLessPreProcessor($actualRootCssContent)
{
//_testA is included from Magento/zoom3
//_testB is included from Magento/zoom2
$this->assertContains('color:#111', $actualRootCssContent);
}
/**
* Assert CssUrlFixer post-processor
*
* @param $actualRootCssContent
* @return void
*/
private function assertCssUrlFixerPostProcessor($actualRootCssContent)
{
//assert CssUrlFixer fix urls
$this->assertContains(
'url("../../../../../frontend/Magento/zoom1/default/images/logo-magento-1.png")',
$actualRootCssContent
);
$this->assertContains(
'url("../../../../../frontend/Magento/zoom2/default/images/logo-magento-2.png")',
$actualRootCssContent
);
$this->assertContains(
'url("../images/logo-magento-3.png")',
$actualRootCssContent
);
//_testA is included from Magento/zoom3
//_testB is included from Magento/zoom2
$this->assertContains('color:#111', $actualRootCssContent);
}
/**
* Assert correct bundle size according to configuration set in view.xml
*
* @param string $theme
* @return void
*/
private function assertBundleSize($theme)
{
$expectedSize = $this->bundleConfig->getBundleFileMaxSize('frontend', $theme);
$expectedSize *= 1.15;
$iterator = $this->getDirectoryIterator("frontend/{$theme}/en_US/js/bundle");
/** @var \SplFileInfo $file */
foreach ($iterator as $file) {
if ($file->isFile()) {
$size = (int)$file->getSize() / 1024;
$this->assertLessThan($expectedSize, $size);
}
}
}
/**
* Assert that file is excluded from view.xml
*
* @param string $theme
* @param array $excluded
* @return void
*/
private function assertExcluded($theme, array $excluded)
{
$iterator = $this->getDirectoryIterator("frontend/{$theme}/en_US/js/bundle");
foreach ($excluded as $pathData) {
$path = $this->splitPath($pathData);
if ($path['module'] !== null) {
$path = implode('/', $path);
} else {
$path = $path['path'];
}
/** @var \SplFileInfo $file */
foreach ($iterator as $file) {
if ($file->isFile()) {
$bundleContent = $this->staticDir->readFile(
$this->staticDir->getRelativePath($file->getPathname())
);
$this->assertNotContains('"' . $path . '":"', $bundleContent);
}
}
}
}
/**
* @param string $path
* @return \RecursiveIteratorIterator
*/
private function getDirectoryIterator($path)
{
$dirIterator = new \RecursiveDirectoryIterator(
$this->staticDir->getAbsolutePath($path),
\FilesystemIterator::SKIP_DOTS
);
return new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::SELF_FIRST);
}
/**
* Get excluded module and path from complex string
*
* @param string $path
* @return array
*/
private function splitPath($path)
{
if (strpos($path, '::') !== false) {
list($module, $path) = explode('::', $path);
return [
'module' => $module != 'Lib' ? $module : null,
'path' => $path,
];
} else {
return [
'module' => null,
'path' => $path,
];
}
}
}