Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeployTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Deploy;
7 
20 
26 class DeployTest extends \PHPUnit\Framework\TestCase
27 {
31  private $filesystem;
32 
36  private $staticDir;
37 
41  private $rootDir;
42 
46  private $deployService;
47 
51  private $config;
52 
56  private $bundleConfig;
57 
61  private $staticContentService;
62 
66  private $prevMode;
67 
71  private $options = [
72  Options::DRY_RUN => false,
73  Options::NO_JAVASCRIPT => false,
74  Options::NO_CSS => false,
75  Options::NO_LESS => false,
76  Options::NO_IMAGES => false,
77  Options::NO_FONTS => false,
78  Options::NO_HTML => false,
79  Options::NO_MISC => false,
80  Options::NO_HTML_MINIFY => false,
81  Options::AREA => ['frontend'],
82  Options::EXCLUDE_AREA => ['none'],
83  Options::THEME => ['Magento/zoom1', 'Magento/zoom2', 'Magento/zoom3'],
84  Options::EXCLUDE_THEME => ['none'],
85  Options::LANGUAGE => ['en_US', 'fr_FR', 'pl_PL'],
86  Options::EXCLUDE_LANGUAGE => ['none'],
87  Options::JOBS_AMOUNT => 0,
88  Options::SYMLINK_LOCALE => false,
90  ];
91 
92  protected function setUp()
93  {
95  $this->prevMode = $objectManager->get(State::class)->getMode();
96  $objectManager->get(State::class)->setMode(State::MODE_PRODUCTION);
97 
98  $this->filesystem = $objectManager->get(Filesystem::class);
99  $this->staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
100  $this->rootDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT);
101 
102  $logger = $objectManager->get(\Psr\Log\LoggerInterface::class);
103  $this->deployService = $objectManager->create(DeployStaticContent::class, [
104  'logger' => $logger
105  ]);
106 
107  $this->bundleConfig = $objectManager->create(BundleConfig::class);
108  $this->config = $objectManager->create(View::class);
109 
110  $this->staticContentService = $objectManager->create(DeployStaticContent::class);
111 
112  $this->filesystem->getDirectoryWrite(DirectoryList::PUB)->delete(DirectoryList::STATIC_VIEW);
113  $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)->delete(DirectoryList::TMP_MATERIALIZATION_DIR);
114  }
115 
116  protected function tearDown()
117  {
119  $objectManager->get(State::class)->setMode($this->prevMode);
120  $this->filesystem = $objectManager->get(Filesystem::class);
121  $this->filesystem->getDirectoryWrite(DirectoryList::PUB)->delete(DirectoryList::STATIC_VIEW);
122  $this->staticDir = $this->filesystem->getDirectoryWrite(DirectoryList::STATIC_VIEW);
123  $this->staticDir->getDriver()->createDirectory($this->staticDir->getAbsolutePath());
124 
125  parent::tearDown();
126  }
127 
131  public function testDeploy()
132  {
133  $this->deployService->deploy($this->options);
134 
135  $this->assertFileExists($this->staticDir->getAbsolutePath('frontend/Magento/zoom1/default/css/root.css'));
136  $this->assertFileExists($this->staticDir->getAbsolutePath('frontend/Magento/zoom2/default/css/root.css'));
137  $this->assertFileExists($this->staticDir->getAbsolutePath('frontend/Magento/zoom3/default/css/root.css'));
138  $this->assertFileExists($this->staticDir->getAbsolutePath('frontend/Magento/zoom3/default/css/local.css'));
139 
140  $this->assertFileExistsIsGenerated('requirejs-config.js');
141  $this->assertFileExistsIsGenerated('requirejs-map.js');
142  $this->assertFileExistsIsGenerated('map.json');
143  $this->assertFileExistsIsGenerated('js-translation.json');
144  $this->assertFileExistsIsGenerated('result_map.json');
145 
146  $actualFileContent = $this->staticDir->readFile('frontend/Magento/zoom3/default/css/root.css');
147  $this->assertLessPreProcessor($actualFileContent);
148  $this->assertCssUrlFixerPostProcessor($actualFileContent);
149 
150  foreach (['Magento/zoom1', 'Magento/zoom2', 'Magento/zoom3'] as $theme) {
151  $this->assertBundleSize($theme);
152  $this->assertExcluded($theme, $this->config->getExcludedFiles());
153  $this->assertExcluded($theme, $this->config->getExcludedDir());
154  }
155  }
156 
163  private function assertFileExistsIsGenerated($fileName)
164  {
165  foreach (['Magento/zoom1', 'Magento/zoom2', 'Magento/zoom3'] as $theme) {
166  foreach ($this->options[Options::LANGUAGE] as $locale) {
167  $this->assertFileExists(
168  $this->staticDir->getAbsolutePath(
169  'frontend/' . $theme . '/' . $locale . '/' . $fileName
170  )
171  );
172  }
173  }
174  }
175 
183  private function assertLessPreProcessor($actualRootCssContent)
184  {
185  //_testA is included from Magento/zoom3
186  //_testB is included from Magento/zoom2
187  $this->assertContains('color:#111', $actualRootCssContent);
188  }
189 
196  private function assertCssUrlFixerPostProcessor($actualRootCssContent)
197  {
198  //assert CssUrlFixer fix urls
199  $this->assertContains(
200  'url("../../../../../frontend/Magento/zoom1/default/images/logo-magento-1.png")',
201  $actualRootCssContent
202  );
203  $this->assertContains(
204  'url("../../../../../frontend/Magento/zoom2/default/images/logo-magento-2.png")',
205  $actualRootCssContent
206  );
207  $this->assertContains(
208  'url("../images/logo-magento-3.png")',
209  $actualRootCssContent
210  );
211  //_testA is included from Magento/zoom3
212  //_testB is included from Magento/zoom2
213  $this->assertContains('color:#111', $actualRootCssContent);
214  }
215 
222  private function assertBundleSize($theme)
223  {
224  $expectedSize = $this->bundleConfig->getBundleFileMaxSize('frontend', $theme);
225  $expectedSize *= 1.15;
226 
227  $iterator = $this->getDirectoryIterator("frontend/{$theme}/en_US/js/bundle");
229  foreach ($iterator as $file) {
230  if ($file->isFile()) {
231  $size = (int)$file->getSize() / 1024;
232  $this->assertLessThan($expectedSize, $size);
233  }
234  }
235  }
236 
244  private function assertExcluded($theme, array $excluded)
245  {
246  $iterator = $this->getDirectoryIterator("frontend/{$theme}/en_US/js/bundle");
247  foreach ($excluded as $pathData) {
248  $path = $this->splitPath($pathData);
249  if ($path['module'] !== null) {
250  $path = implode('/', $path);
251  } else {
252  $path = $path['path'];
253  }
255  foreach ($iterator as $file) {
256  if ($file->isFile()) {
257  $bundleContent = $this->staticDir->readFile(
258  $this->staticDir->getRelativePath($file->getPathname())
259  );
260  $this->assertNotContains('"' . $path . '":"', $bundleContent);
261  }
262  }
263  }
264  }
265 
270  private function getDirectoryIterator($path)
271  {
272  $dirIterator = new \RecursiveDirectoryIterator(
273  $this->staticDir->getAbsolutePath($path),
274  \FilesystemIterator::SKIP_DOTS
275  );
276  return new \RecursiveIteratorIterator($dirIterator, \RecursiveIteratorIterator::SELF_FIRST);
277  }
278 
285  private function splitPath($path)
286  {
287  if (strpos($path, '::') !== false) {
288  list($module, $path) = explode('::', $path);
289  return [
290  'module' => $module != 'Lib' ? $module : null,
291  'path' => $path,
292  ];
293  } else {
294  return [
295  'module' => null,
296  'path' => $path,
297  ];
298  }
299  }
300 }
$objectManager
Definition: bootstrap.php:17
$logger
$fileName
Definition: translate.phtml:15
$theme