Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FrontendCompilationTest.php
Go to the documentation of this file.
1 <?php
7 
17 
25 class FrontendCompilationTest extends \PHPUnit\Framework\TestCase
26 {
27  const AREA = 'test-area';
28 
29  const THEME = 'test-theme';
30 
31  const LOCALE = 'test-locale';
32 
33  const FILE_PATH = 'test-file';
34 
35  const MODULE = 'test-module';
36 
37  const NEW_CONTENT = 'test-new-content';
38 
42  private $lockerProcessMock;
43 
47  private $assetBuilderMock;
48 
52  private $alternativeSourceMock;
53 
57  private $assetSourceMock;
58 
62  protected function setUp()
63  {
64  $this->lockerProcessMock = $this->getMockBuilder(LockerProcessInterface::class)
65  ->getMockForAbstractClass();
66  $this->assetBuilderMock = $this->getMockBuilder(AssetBuilder::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69  $this->alternativeSourceMock = $this->getMockBuilder(AlternativeSourceInterface::class)
70  ->getMockForAbstractClass();
71  $this->assetSourceMock = $this->getMockBuilder(Source::class)
72  ->disableOriginalConstructor()
73  ->getMock();
74  }
75 
79  public function testProcessException()
80  {
81  $this->lockerProcessMock->expects(self::once())
82  ->method('lockProcess')
83  ->with(self::isType('string'));
84  $this->lockerProcessMock->expects(self::once())
85  ->method('unlockProcess');
86 
87  $this->alternativeSourceMock->expects(self::once())
88  ->method('getAlternativesExtensionsNames')
89  ->willReturn(['less']);
90 
91  $this->assetBuilderMock->expects(self::once())
92  ->method('setArea')
93  ->with(self::AREA)
94  ->willReturnSelf();
95  $this->assetBuilderMock->expects(self::once())
96  ->method('setTheme')
97  ->with(self::THEME)
98  ->willReturnSelf();
99  $this->assetBuilderMock->expects(self::once())
100  ->method('setLocale')
101  ->with(self::LOCALE)
102  ->willReturnSelf();
103  $this->assetBuilderMock->expects(self::once())
104  ->method('setModule')
105  ->with(self::MODULE)
106  ->willReturnSelf();
107  $this->assetBuilderMock->expects(self::once())
108  ->method('setPath')
109  ->with(self::FILE_PATH)
110  ->willReturnSelf();
111  $this->assetBuilderMock->expects(self::once())
112  ->method('build')
113  ->willThrowException(new \Exception());
114 
115  $this->assetSourceMock->expects(self::never())
116  ->method('getContent');
117 
118  $frontendCompilation = new FrontendCompilation(
119  $this->assetSourceMock,
120  $this->assetBuilderMock,
121  $this->alternativeSourceMock,
122  $this->lockerProcessMock,
123  'lock'
124  );
125 
126  try {
127  $frontendCompilation->process($this->getChainMockExpects('', 0, 1));
128  } catch (\Exception $e) {
129  self::assertInstanceOf('\Exception', $e);
130  }
131  }
132 
136  public function testProcess()
137  {
138  $newContentType = 'less';
139 
140  $this->lockerProcessMock->expects(self::once())
141  ->method('lockProcess')
142  ->with(self::isType('string'));
143  $this->lockerProcessMock->expects(self::once())
144  ->method('unlockProcess');
145 
146  $assetMock = $this->getAssetNew();
147 
148  $this->assetBuilderMock->expects(self::once())
149  ->method('setArea')
150  ->with(self::AREA)
151  ->willReturnSelf();
152  $this->assetBuilderMock->expects(self::once())
153  ->method('setTheme')
154  ->with(self::THEME)
155  ->willReturnSelf();
156  $this->assetBuilderMock->expects(self::once())
157  ->method('setLocale')
158  ->with(self::LOCALE)
159  ->willReturnSelf();
160  $this->assetBuilderMock->expects(self::once())
161  ->method('setModule')
162  ->with(self::MODULE)
163  ->willReturnSelf();
164  $this->assetBuilderMock->expects(self::once())
165  ->method('setPath')
166  ->with(self::FILE_PATH)
167  ->willReturnSelf();
168  $this->assetBuilderMock->expects(self::once())
169  ->method('build')
170  ->willReturn($assetMock);
171 
172  $this->alternativeSourceMock->expects(self::once())
173  ->method('getAlternativesExtensionsNames')
174  ->willReturn([$newContentType]);
175 
176  $this->assetSourceMock->expects(self::once())
177  ->method('getContent')
178  ->with($assetMock)
179  ->willReturn(self::NEW_CONTENT);
180 
181  $frontendCompilation = new FrontendCompilation(
182  $this->assetSourceMock,
183  $this->assetBuilderMock,
184  $this->alternativeSourceMock,
185  $this->lockerProcessMock,
186  'lock'
187  );
188 
189  $frontendCompilation->process($this->getChainMockExpects('', 1, 1, $newContentType));
190  }
191 
195  private function getChainMock()
196  {
197  $chainMock = $this->getMockBuilder(Chain::class)
198  ->disableOriginalConstructor()
199  ->getMock();
200 
201  return $chainMock;
202  }
203 
211  private function getChainMockExpects($content = '', $contentExactly = 1, $pathExactly = 1, $newContentType = '')
212  {
213  $chainMock = $this->getChainMock();
214 
215  $chainMock->expects(self::once())
216  ->method('getContent')
217  ->willReturn($content);
218  $chainMock->expects(self::exactly(3))
219  ->method('getAsset')
220  ->willReturn($this->getAssetMockExpects($pathExactly));
221  $chainMock->expects(self::exactly($contentExactly))
222  ->method('setContent')
223  ->with(self::NEW_CONTENT);
224  $chainMock->expects(self::exactly($contentExactly))
225  ->method('setContentType')
226  ->with($newContentType);
227 
228  return $chainMock;
229  }
230 
234  private function getAssetNew()
235  {
236  $assetMock = $this->getMockBuilder(File::class)
237  ->disableOriginalConstructor()
238  ->getMock();
239 
240  return $assetMock;
241  }
242 
246  private function getAssetMock()
247  {
248  $assetMock = $this->getMockBuilder(LocalInterface::class)
249  ->disableOriginalConstructor()
250  ->getMock();
251 
252  return $assetMock;
253  }
254 
259  private function getAssetMockExpects($pathExactly = 1)
260  {
261  $assetMock = $this->getAssetMock();
262 
263  $assetMock->expects(self::once())
264  ->method('getContext')
265  ->willReturn($this->getContextMock());
266  $assetMock->expects(self::exactly($pathExactly))
267  ->method('getFilePath')
268  ->willReturn(self::FILE_PATH);
269  $assetMock->expects(self::once())
270  ->method('getModule')
271  ->willReturn(self::MODULE);
272 
273  return $assetMock;
274  }
275 
279  private function getContextMock()
280  {
281  $contextMock = $this->getMockBuilder(FallbackContext::class)
282  ->disableOriginalConstructor()
283  ->getMock();
284 
285  $contextMock->expects(self::once())
286  ->method('getAreaCode')
287  ->willReturn(self::AREA);
288  $contextMock->expects(self::once())
289  ->method('getThemePath')
290  ->willReturn(self::THEME);
291  $contextMock->expects(self::once())
292  ->method('getLocale')
293  ->willReturn(self::LOCALE);
294 
295  return $contextMock;
296  }
297 }