Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AlternativeSourceTest.php
Go to the documentation of this file.
1 <?php
7 
20 
28 class AlternativeSourceTest extends \PHPUnit\Framework\TestCase
29 {
30  const AREA = 'test-area';
31 
32  const THEME = 'test-theme';
33 
34  const LOCALE = 'test-locale';
35 
36  const FILE_PATH = 'test-file';
37 
38  const MODULE = 'test-module';
39 
40  const NEW_CONTENT = 'test-new-content';
41 
45  private $sorterMock;
46 
50  private $objectManagerMock;
51 
55  private $lockerProcessMock;
56 
60  private $assetBuilderMock;
61 
65  private $alternativeMock;
66 
70  private $filenameResolverMock;
71 
75  protected function setUp()
76  {
77  $this->sorterMock = $this->getMockBuilder(SortInterface::class)
78  ->getMockForAbstractClass();
79  $this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
80  ->getMockForAbstractClass();
81  $this->lockerProcessMock = $this->getMockBuilder(LockerProcessInterface::class)
82  ->getMockForAbstractClass();
83  $this->assetBuilderMock = $this->getMockBuilder(AssetBuilder::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->alternativeMock = $this->getMockBuilder(ContentProcessorInterface::class)
87  ->getMockForAbstractClass();
88  $this->filenameResolverMock = $this->getMockBuilder(FilenameResolverInterface::class)
89  ->getMockForAbstractClass();
90  }
91 
95  public function testProcessException()
96  {
97  $alternatives = [
98  'processor' => [
100  ]
101  ];
102 
103  $this->lockerProcessMock->expects(self::once())
104  ->method('lockProcess')
105  ->with(self::isType('string'));
106  $this->lockerProcessMock->expects(self::once())
107  ->method('unlockProcess');
108 
109  $this->sorterMock->expects(self::once())
110  ->method('sort')
111  ->with($alternatives)
112  ->willReturn($alternatives);
113 
114  $this->filenameResolverMock->expects(self::once())
115  ->method('resolve')
116  ->with(self::FILE_PATH)
117  ->willReturn(self::FILE_PATH);
118 
119  $this->assetBuilderMock->expects(self::once())
120  ->method('setArea')
121  ->with(self::AREA)
122  ->willReturnSelf();
123  $this->assetBuilderMock->expects(self::once())
124  ->method('setTheme')
125  ->with(self::THEME)
126  ->willReturnSelf();
127  $this->assetBuilderMock->expects(self::once())
128  ->method('setLocale')
129  ->with(self::LOCALE)
130  ->willReturnSelf();
131  $this->assetBuilderMock->expects(self::once())
132  ->method('setModule')
133  ->with(self::MODULE)
134  ->willReturnSelf();
135  $this->assetBuilderMock->expects(self::once())
136  ->method('setPath')
137  ->with(self::FILE_PATH)
138  ->willReturnSelf();
139  $this->assetBuilderMock->expects(self::once())
140  ->method('build')
141  ->willReturn($this->getAssetNew());
142 
143  $this->objectManagerMock->expects(self::once())
144  ->method('get')
145  ->with('stdClass')
146  ->willReturn(new \stdClass());
147 
148  $alternativeSource = new AlternativeSource(
149  $this->filenameResolverMock,
150  $this->objectManagerMock,
151  $this->lockerProcessMock,
152  $this->sorterMock,
153  $this->assetBuilderMock,
154  'lock',
155  $alternatives
156  );
157  try {
158  $alternativeSource->process($this->getChainMockExpects('', 0));
159  } catch (\UnexpectedValueException $e) {
160  self::assertInstanceOf('\UnexpectedValueException', $e);
161  }
162  }
163 
167  public function testProcess()
168  {
169  $alternatives = [
170  'processor' => [
171  AlternativeSource::PROCESSOR_CLASS => \Magento\Framework\View\Asset\ContentProcessorInterface::class
172  ]
173  ];
174 
175  $this->lockerProcessMock->expects(self::once())
176  ->method('lockProcess')
177  ->with(self::isType('string'));
178  $this->lockerProcessMock->expects(self::once())
179  ->method('unlockProcess');
180 
181  $this->sorterMock->expects(self::once())
182  ->method('sort')
183  ->with($alternatives)
184  ->willReturn($alternatives);
185 
186  $this->filenameResolverMock->expects(self::once())
187  ->method('resolve')
188  ->with(self::FILE_PATH)
189  ->willReturn(self::FILE_PATH);
190 
191  $assetMock = $this->getAssetNew();
192 
193  $this->assetBuilderMock->expects(self::once())
194  ->method('setArea')
195  ->with(self::AREA)
196  ->willReturnSelf();
197  $this->assetBuilderMock->expects(self::once())
198  ->method('setTheme')
199  ->with(self::THEME)
200  ->willReturnSelf();
201  $this->assetBuilderMock->expects(self::once())
202  ->method('setLocale')
203  ->with(self::LOCALE)
204  ->willReturnSelf();
205  $this->assetBuilderMock->expects(self::once())
206  ->method('setModule')
207  ->with(self::MODULE)
208  ->willReturnSelf();
209  $this->assetBuilderMock->expects(self::once())
210  ->method('setPath')
211  ->with(self::FILE_PATH)
212  ->willReturnSelf();
213  $this->assetBuilderMock->expects(self::once())
214  ->method('build')
215  ->willReturn($assetMock);
216 
217  $this->objectManagerMock->expects(self::once())
218  ->method('get')
219  ->with(\Magento\Framework\View\Asset\ContentProcessorInterface::class)
220  ->willReturn($this->getProcessorMock($assetMock));
221 
222  $alternativeSource = new AlternativeSource(
223  $this->filenameResolverMock,
224  $this->objectManagerMock,
225  $this->lockerProcessMock,
226  $this->sorterMock,
227  $this->assetBuilderMock,
228  'lock',
229  $alternatives
230  );
231 
232  $alternativeSource->process($this->getChainMockExpects());
233  }
234 
238  public function testProcessContentNotEmpty()
239  {
240  $chainMock = $this->getChainMock();
241  $assetMock = $this->getAssetMock();
242 
243  $chainMock->expects(self::once())
244  ->method('getContent')
245  ->willReturn('test-content');
246 
247  $chainMock->expects(self::once())
248  ->method('getAsset')
249  ->willReturn($assetMock);
250 
251  $this->filenameResolverMock->expects(self::never())
252  ->method('resolve');
253 
254  $this->lockerProcessMock->expects(self::never())
255  ->method('lockProcess');
256  $this->lockerProcessMock->expects(self::never())
257  ->method('unlockProcess');
258 
259  $alternativeSource = new AlternativeSource(
260  $this->filenameResolverMock,
261  $this->objectManagerMock,
262  $this->lockerProcessMock,
263  $this->sorterMock,
264  $this->assetBuilderMock,
265  'lock',
266  []
267  );
268 
269  $alternativeSource->process($chainMock);
270  }
271 
276  private function getProcessorMock($asset)
277  {
278  $processorMock = $this->getMockBuilder(ContentProcessorInterface::class)
279  ->getMockForAbstractClass();
280 
281  $processorMock->expects(self::once())
282  ->method('processContent')
283  ->with($asset)
284  ->willReturn(self::NEW_CONTENT);
285 
286  return $processorMock;
287  }
288 
292  private function getChainMock()
293  {
294  $chainMock = $this->getMockBuilder(Chain::class)
295  ->disableOriginalConstructor()
296  ->getMock();
297 
298  return $chainMock;
299  }
300 
306  private function getChainMockExpects($content = '', $contentExactly = 1)
307  {
308  $chainMock = $this->getChainMock();
309 
310  $chainMock->expects(self::once())
311  ->method('getContent')
312  ->willReturn($content);
313  $chainMock->expects(self::exactly(3))
314  ->method('getAsset')
315  ->willReturn($this->getAssetMockExpects());
316  $chainMock->expects(self::exactly($contentExactly))
317  ->method('setContent')
318  ->willReturn(self::NEW_CONTENT);
319 
320  return $chainMock;
321  }
322 
326  private function getAssetNew()
327  {
328  $assetMock = $this->getMockBuilder(File::class)
329  ->disableOriginalConstructor()
330  ->getMock();
331 
332  return $assetMock;
333  }
334 
338  private function getAssetMock()
339  {
340  $assetMock = $this->getMockBuilder(LocalInterface::class)
341  ->disableOriginalConstructor()
342  ->getMock();
343 
344  return $assetMock;
345  }
346 
350  private function getAssetMockExpects()
351  {
352  $assetMock = $this->getAssetMock();
353 
354  $assetMock->expects(self::once())
355  ->method('getContext')
356  ->willReturn($this->getContextMock());
357  $assetMock->expects(self::once())
358  ->method('getFilePath')
359  ->willReturn(self::FILE_PATH);
360  $assetMock->expects(self::once())
361  ->method('getModule')
362  ->willReturn(self::MODULE);
363 
364  return $assetMock;
365  }
366 
370  private function getContextMock()
371  {
372  $contextMock = $this->getMockBuilder(FallbackContext::class)
373  ->disableOriginalConstructor()
374  ->getMock();
375 
376  $contextMock->expects(self::once())
377  ->method('getAreaCode')
378  ->willReturn(self::AREA);
379  $contextMock->expects(self::once())
380  ->method('getThemePath')
381  ->willReturn(self::THEME);
382  $contextMock->expects(self::once())
383  ->method('getLocale')
384  ->willReturn(self::LOCALE);
385 
386  return $contextMock;
387  }
388 }