Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DirectTest.php
Go to the documentation of this file.
1 <?php
7 
9 use \Magento\Framework\View\Asset\MergeStrategy\Direct;
10 
12 
13 class DirectTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $object;
19 
23  protected $cssUrlResolver;
24 
28  protected $staticDir;
29 
33  protected $tmpDir;
34 
38  protected $resultAsset;
39 
40  protected function setUp()
41  {
42  $this->cssUrlResolver = $this->createMock(\Magento\Framework\View\Url\CssResolver::class);
43  $filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
44  $this->staticDir = $this->getMockBuilder(WriteInterface::class)->getMockForAbstractClass();
45  $this->tmpDir = $this->getMockBuilder(WriteInterface::class)->getMockForAbstractClass();
46  $filesystem->expects($this->any())
47  ->method('getDirectoryWrite')
48  ->willReturnMap([
49  [DirectoryList::STATIC_VIEW, \Magento\Framework\Filesystem\DriverPool::FILE, $this->staticDir],
50  [DirectoryList::TMP, \Magento\Framework\Filesystem\DriverPool::FILE, $this->tmpDir],
51  ]);
52  $this->resultAsset = $this->createMock(\Magento\Framework\View\Asset\File::class);
53  $this->object = new Direct($filesystem, $this->cssUrlResolver);
54  }
55 
56  public function testMergeNoAssets()
57  {
58  $this->resultAsset->expects($this->once())->method('getPath')->will($this->returnValue('foo/result'));
59  $this->staticDir->expects($this->never())->method('writeFile');
60  $this->tmpDir->expects($this->once())->method('writeFile')->with('foo/result', '');
61  $this->tmpDir->expects($this->once())->method('renameFile')->with('foo/result', 'foo/result', $this->staticDir);
62  $this->object->merge([], $this->resultAsset);
63  }
64 
65  public function testMergeGeneric()
66  {
67  $this->resultAsset->expects($this->once())->method('getPath')->will($this->returnValue('foo/result'));
68  $assets = $this->prepareAssetsToMerge([' one', 'two']); // note leading space intentionally
69  $this->staticDir->expects($this->never())->method('writeFile');
70  $this->tmpDir->expects($this->once())->method('writeFile')->with('foo/result', 'onetwo');
71  $this->tmpDir->expects($this->once())->method('renameFile')->with('foo/result', 'foo/result', $this->staticDir);
72  $this->object->merge($assets, $this->resultAsset);
73  }
74 
75  public function testMergeCss()
76  {
77  $this->resultAsset->expects($this->exactly(3))
78  ->method('getPath')
79  ->will($this->returnValue('foo/result'));
80  $this->resultAsset->expects($this->any())->method('getContentType')->will($this->returnValue('css'));
81  $assets = $this->prepareAssetsToMerge(['one', 'two']);
82  $this->cssUrlResolver->expects($this->exactly(2))
83  ->method('relocateRelativeUrls')
84  ->will($this->onConsecutiveCalls('1', '2'));
85  $this->cssUrlResolver->expects($this->once())
86  ->method('aggregateImportDirectives')
87  ->with('12')
88  ->will($this->returnValue('1020'));
89  $this->staticDir->expects($this->never())->method('writeFile');
90  $this->tmpDir->expects($this->once())->method('writeFile')->with('foo/result', '1020');
91  $this->tmpDir->expects($this->once())->method('renameFile')->with('foo/result', 'foo/result', $this->staticDir);
92  $this->object->merge($assets, $this->resultAsset);
93  }
94 
101  private function prepareAssetsToMerge(array $data)
102  {
103  $result = [];
104  foreach ($data as $content) {
105  $asset = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\LocalInterface::class);
106  $asset->expects($this->once())->method('getContent')->will($this->returnValue($content));
107  $result[] = $asset;
108  }
109  return $result;
110  }
111 }
$filesystem