Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LibraryTest.php
Go to the documentation of this file.
1 <?php
7 
9 use \Magento\Framework\Css\PreProcessor\File\Collector\Library;
12 
18 class LibraryTest extends \PHPUnit\Framework\TestCase
19 {
23  private $library;
24 
29 
33  protected $fileSystemMock;
34 
38  protected $fileFactoryMock;
39 
43  protected $fileListMock;
44 
49 
53  private $readFactoryMock;
54 
60  private $componentRegistrarMock;
61 
65  protected $themeMock;
66 
71  public function setup()
72  {
73  $this->fileListFactoryMock = $this->getMockBuilder(\Magento\Framework\View\File\FileList\Factory::class)
74  ->disableOriginalConstructor()->getMock();
75  $this->fileListMock = $this->getMockBuilder(\Magento\Framework\View\File\FileList::class)
76  ->disableOriginalConstructor()->getMock();
77  $this->fileListFactoryMock->expects($this->any())
78  ->method('create')
79  ->with(\Magento\Framework\Css\PreProcessor\File\FileList\Collator::class)
80  ->will($this->returnValue($this->fileListMock));
81  $this->readFactoryMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadFactory::class)
82  ->disableOriginalConstructor()->getMock();
83  $this->componentRegistrarMock = $this->getMockBuilder(
84  \Magento\Framework\Component\ComponentRegistrarInterface::class
85  )->disableOriginalConstructor()->getMock();
86  $this->fileSystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89  $this->libraryDirectoryMock = $this->getMockBuilder(
90  \Magento\Framework\Filesystem\Directory\ReadInterface::class
91  )->getMock();
92  $this->fileSystemMock->expects($this->any())->method('getDirectoryRead')
93  ->will(
94  $this->returnValueMap(
95  [
96  [DirectoryList::LIB_WEB, Filesystem\DriverPool::FILE, $this->libraryDirectoryMock],
97  ]
98  )
99  );
100 
101  $this->fileFactoryMock = $this->getMockBuilder(\Magento\Framework\View\File\Factory::class)
102  ->disableOriginalConstructor()
103  ->getMock();
104  $this->themeMock = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)->getMock();
105  $this->library = new Library(
106  $this->fileListFactoryMock,
107  $this->fileSystemMock,
108  $this->fileFactoryMock,
109  $this->readFactoryMock,
110  $this->componentRegistrarMock
111  );
112  }
113 
114  public function testGetFilesEmpty()
115  {
116  $this->libraryDirectoryMock->expects($this->any())->method('search')->will($this->returnValue([]));
117  $this->themeMock->expects($this->any())->method('getInheritedThemes')->will($this->returnValue([]));
118 
119  // Verify search/replace are never called if no inheritedThemes
120  $this->readFactoryMock->expects($this->never())
121  ->method('create');
122  $this->componentRegistrarMock->expects($this->never())
123  ->method('getPath');
124 
125  $this->library->getFiles($this->themeMock, '*');
126  }
127 
137  public function testGetFiles($libraryFiles, $themeFiles)
138  {
139  $this->fileListMock->expects($this->any())->method('getAll')->will($this->returnValue(['returnedFile']));
140 
141  $this->libraryDirectoryMock->expects($this->any())->method('search')->will($this->returnValue($libraryFiles));
142  $this->libraryDirectoryMock->expects($this->any())->method('getAbsolutePath')->will($this->returnCallback(
143  function ($file) {
144  return '/opt/Magento/lib/' . $file;
145  }
146  ));
147  $themePath = '/var/Magento/ATheme';
148  $subPath = '*';
149  $readerMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)->getMock();
150  $this->readFactoryMock->expects($this->once())
151  ->method('create')
152  ->will($this->returnValue($readerMock));
153  $this->componentRegistrarMock->expects($this->once())
154  ->method('getPath')
155  ->with(ComponentRegistrar::THEME, $themePath)
156  ->will($this->returnValue(['/path/to/theme']));
157  $readerMock->expects($this->once())
158  ->method('search')
159  ->will($this->returnValue($themeFiles));
160  $inheritedThemeMock = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)->getMock();
161  $inheritedThemeMock->expects($this->any())->method('getFullPath')->will($this->returnValue($themePath));
162  $this->themeMock->expects($this->any())->method('getInheritedThemes')
163  ->will($this->returnValue([$inheritedThemeMock]));
164  $this->assertEquals(['returnedFile'], $this->library->getFiles($this->themeMock, $subPath));
165  }
166 
172  public function getFilesDataProvider()
173  {
174  return [
175  'all files' => [['file1'], ['file2']],
176  'no library' => [[], ['file1', 'file2']],
177  ];
178  }
179 }