Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MagentoImportTest.php
Go to the documentation of this file.
1 <?php
8 
12 
16 class MagentoImportTest extends \PHPUnit\Framework\TestCase
17 {
21  private $design;
22 
26  private $fileSource;
27 
31  private $errorHandler;
32 
36  private $asset;
37 
41  private $assetRepo;
42 
46  private $themeProvider;
47 
51  private $object;
52 
53  protected function setUp()
54  {
55  $this->design = $this->getMockForAbstractClass(\Magento\Framework\View\DesignInterface::class);
56  $this->fileSource = $this->getMockForAbstractClass(\Magento\Framework\View\File\CollectorInterface::class);
57  $this->errorHandler = $this->getMockForAbstractClass(
58  \Magento\Framework\Css\PreProcessor\ErrorHandlerInterface::class
59  );
60  $this->asset = $this->createMock(\Magento\Framework\View\Asset\File::class);
61  $this->asset->expects($this->any())->method('getContentType')->will($this->returnValue('css'));
62  $this->assetRepo = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
63  $this->themeProvider = $this->createMock(ThemeProviderInterface::class);
64  $this->object = (new ObjectManager($this))->getObject(MagentoImport::class, [
65  'design' => $this->design,
66  'fileSource' => $this->fileSource,
67  'errorHandler' => $this->errorHandler,
68  'assetRepo' => $this->assetRepo,
69  'themeProvider' => $this->themeProvider
70  ]);
71  }
72 
82  public function testProcess($originalContent, $foundPath, $resolvedPath, $foundFiles, $expectedContent)
83  {
84  $chain = new \Magento\Framework\View\Asset\PreProcessor\Chain($this->asset, $originalContent, 'css', 'path');
85  $relatedAsset = $this->createMock(\Magento\Framework\View\Asset\File::class);
86  $relatedAsset->expects($this->once())
87  ->method('getFilePath')
88  ->will($this->returnValue($resolvedPath));
89  $context = $this->createMock(\Magento\Framework\View\Asset\File\FallbackContext::class);
90  $this->assetRepo->expects($this->once())
91  ->method('createRelated')
92  ->with($foundPath, $this->asset)
93  ->will($this->returnValue($relatedAsset));
94  $relatedAsset->expects($this->once())->method('getContext')->will($this->returnValue($context));
95  $theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
96  $this->themeProvider->expects($this->once())->method('getThemeByFullPath')->will($this->returnValue($theme));
97  $files = [];
98  foreach ($foundFiles as $file) {
99  $fileObject = $this->createMock(\Magento\Framework\View\File::class);
100  $fileObject->expects($this->any())
101  ->method('getModule')
102  ->will($this->returnValue($file['module']));
103  $fileObject->expects($this->any())
104  ->method('getFilename')
105  ->will($this->returnValue($file['filename']));
106  $files[] = $fileObject;
107  }
108  $this->fileSource->expects($this->once())
109  ->method('getFiles')
110  ->with($theme, $resolvedPath)
111  ->will($this->returnValue($files));
112  $this->object->process($chain);
113  $this->assertEquals($expectedContent, $chain->getContent());
114  $this->assertEquals('css', $chain->getContentType());
115  }
116 
120  public function processDataProvider()
121  {
122  return [
123  'non-modular notation' => [
124  '//@magento_import "some/file.css";',
125  'some/file.css',
126  'some/file.css',
127  [
128  ['module' => null, 'filename' => 'some/file.css'],
129  ['module' => null, 'filename' => 'theme/some/file.css'],
130  ],
131  "@import 'some/file.css';\n@import 'some/file.css';\n",
132  ],
133  'modular' => [
134  '//@magento_import "Magento_Module::some/file.css";',
135  'Magento_Module::some/file.css',
136  'some/file.css',
137  [
138  ['module' => 'Magento_Module', 'filename' => 'some/file.css'],
139  ['module' => 'Magento_Two', 'filename' => 'some/file.css'],
140  ],
141  "@import 'Magento_Module::some/file.css';\n@import 'Magento_Two::some/file.css';\n",
142  ],
143  'non-modular reference notation' => [
144  '//@magento_import (reference) "some/file.css";',
145  'some/file.css',
146  'some/file.css',
147  [
148  ['module' => null, 'filename' => 'some/file.css'],
149  ['module' => null, 'filename' => 'theme/some/file.css'],
150  ],
151  "@import (reference) 'some/file.css';\n@import (reference) 'some/file.css';\n",
152  ],
153  'modular reference' => [
154  '//@magento_import (reference) "Magento_Module::some/file.css";',
155  'Magento_Module::some/file.css',
156  'some/file.css',
157  [
158  ['module' => 'Magento_Module', 'filename' => 'some/file.css'],
159  ['module' => 'Magento_Two', 'filename' => 'some/file.css'],
160  ],
161  "@import (reference) 'Magento_Module::some/file.css';\n" .
162  "@import (reference) 'Magento_Two::some/file.css';\n",
163  ],
164  ];
165  }
166 
167  public function testProcessNoImport()
168  {
169  $originalContent = 'color: #000000;';
170  $expectedContent = 'color: #000000;';
171  $chain = new \Magento\Framework\View\Asset\PreProcessor\Chain($this->asset, $originalContent, 'css', 'orig');
172  $this->assetRepo->expects($this->never())
173  ->method('createRelated');
174  $this->object->process($chain);
175  $this->assertEquals($expectedContent, $chain->getContent());
176  $this->assertEquals('css', $chain->getContentType());
177  }
178 
179  public function testProcessException()
180  {
181  $chain = new \Magento\Framework\View\Asset\PreProcessor\Chain(
182  $this->asset,
183  '//@magento_import "some/file.css";',
184  'css',
185  'path'
186  );
187  $exception = new \LogicException('Error happened');
188  $this->assetRepo->expects($this->once())
189  ->method('createRelated')
190  ->will($this->throwException($exception));
191  $this->errorHandler->expects($this->once())
192  ->method('processException')
193  ->with($exception);
194  $this->object->process($chain);
195  $this->assertEquals('', $chain->getContent());
196  $this->assertEquals('css', $chain->getContentType());
197  }
198 }
testProcess($originalContent, $foundPath, $resolvedPath, $foundFiles, $expectedContent)
$theme
foreach($appDirs as $dir) $files