Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileManagerTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\RequireJs\Model\FileManager;
11 
12 class FileManagerTest extends \PHPUnit\Framework\TestCase
13 {
17  private $configMock;
18 
22  private $fileSystem;
23 
27  private $dir;
28 
32  private $appState;
33 
37  private $asset;
38 
42  private $object;
43 
47  private $assetRepoMock;
48 
49  protected function setUp()
50  {
51  $this->configMock = $this->createMock(\Magento\Framework\RequireJs\Config::class);
52  $this->fileSystem = $this->createMock(\Magento\Framework\Filesystem::class);
53  $this->appState = $this->createMock(\Magento\Framework\App\State::class);
54  $this->assetRepoMock = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
55  $this->object = new FileManager($this->configMock, $this->fileSystem, $this->appState, $this->assetRepoMock);
56  $this->dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
57  $this->asset = $this->createMock(\Magento\Framework\View\Asset\File::class);
58  }
59 
64  public function testCreateRequireJsConfigAsset($exists)
65  {
66  $this->configMock->expects($this->once())
67  ->method('getConfigFileRelativePath')
68  ->will($this->returnValue('requirejs/file.js'));
69  $this->fileSystem->expects($this->once())
70  ->method('getDirectoryWrite')
72  ->will($this->returnValue($this->dir));
73  $this->assetRepoMock->expects($this->once())
74  ->method('createArbitrary')
75  ->with('requirejs/file.js', '')
76  ->will($this->returnValue($this->asset));
77 
78  $this->appState->expects($this->once())->method('getMode')->will($this->returnValue('anything'));
79  $this->dir->expects($this->once())
80  ->method('isExist')
81  ->with('requirejs/file.js')
82  ->will($this->returnValue($exists));
83  if ($exists) {
84  $this->configMock->expects($this->never())->method('getConfig');
85  $this->dir->expects($this->never())->method('writeFile');
86  } else {
87  $data = 'requirejs config data';
88  $this->configMock->expects($this->once())->method('getConfig')->will($this->returnValue($data));
89  $this->dir->expects($this->once())->method('writeFile')->with('requirejs/file.js', $data);
90  }
91  $this->assertSame($this->asset, $this->object->createRequireJsConfigAsset());
92  }
93 
98  {
99  return [[true], [false]];
100  }
101 
103  {
104  $this->configMock->expects($this->once())
105  ->method('getConfigFileRelativePath')
106  ->will($this->returnValue('requirejs/file.js'));
107  $this->fileSystem->expects($this->once())
108  ->method('getDirectoryWrite')
110  ->will($this->returnValue($this->dir));
111  $this->assetRepoMock->expects($this->once())
112  ->method('createArbitrary')
113  ->with('requirejs/file.js', '')
114  ->will($this->returnValue($this->asset));
115 
116  $this->appState->expects($this->once())
117  ->method('getMode')
118  ->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
119  $this->dir->expects($this->never())->method('isExist');
120  $data = 'requirejs config data';
121  $this->configMock->expects($this->once())->method('getConfig')->will($this->returnValue($data));
122  $this->dir->expects($this->once())->method('writeFile')->with('requirejs/file.js', $data);
123  $this->assertSame($this->asset, $this->object->createRequireJsConfigAsset());
124  }
125 
126  public function testCreateBundleJsPool()
127  {
128  unset($this->configMock);
129  $dirRead = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\Read::class)
130  ->setMockClassName('libDir')
131  ->disableOriginalConstructor()
132  ->getMock();
133  $context = $this->createMock(\Magento\Framework\View\Asset\File\FallbackContext::class);
134  $assetRepo = $this->createMock(\Magento\Framework\View\Asset\Repository::class);
135  $config = $this->createMock(\Magento\Framework\RequireJs\Config::class);
136 
137  $config
138  ->expects($this->never())
139  ->method('getConfigFileRelativePath')
140  ->willReturn(null);
141 
142  $context
143  ->expects($this->once())
144  ->method('getPath')
145  ->willReturn('path/to/bundle/dir');
146 
147  $dirRead
148  ->expects($this->once())
149  ->method('isExist')
150  ->with('path/to/bundle/dir/js/bundle')
151  ->willReturn(true);
152  $dirRead
153  ->expects($this->once())
154  ->method('read')
155  ->with('path/to/bundle/dir/js/bundle')
156  ->willReturn(['bundle1.js', 'bundle2.js', 'some_file.not_js']);
157  $dirRead
158  ->expects($this->exactly(2))
159  ->method('getRelativePath')
160  ->willReturnMap([
161  'path/to/bundle1.js',
162  'path/to/bundle2.js'
163  ]);
164  $assetRepo
165  ->expects($this->exactly(2))
166  ->method('createArbitrary')
167  ->willReturnMap([
168  $this->asset,
169  $this->asset
170  ]);
171 
172  $assetRepo
173  ->expects($this->once())
174  ->method('getStaticViewFileContext')
175  ->willReturn($context);
176 
177  $this->appState
178  ->expects($this->once())
179  ->method('getMode')
180  ->willReturn('production');
181 
182  $this->fileSystem
183  ->expects($this->once())
184  ->method('getDirectoryRead')
185  ->with('static')
186  ->willReturn($dirRead);
187 
188  $object = new FileManager($config, $this->fileSystem, $this->appState, $assetRepo);
189 
190  $result = $object->createBundleJsPool();
191 
192  $this->assertArrayHasKey('0', $result);
193  $this->assertArrayHasKey('1', $result);
194  }
195 
196  public function testCreateMinResolverAsset()
197  {
198  $this->configMock
199  ->expects($this->any())
200  ->method('getMinResolverRelativePath')
201  ->willReturn('relative path');
202  $this->assetRepoMock
203  ->expects($this->once())
204  ->method('createArbitrary')
205  ->with('relative path');
206  $this->fileSystem->expects($this->once())
207  ->method('getDirectoryWrite')
209  ->will($this->returnValue($this->dir));
210 
211  $this->object->createMinResolverAsset();
212  }
213 
215  {
216  $path = 'relative path';
217  $this->configMock
218  ->expects($this->once())
219  ->method('getMixinsFileRelativePath')
220  ->will($this->returnValue($path));
221  $this->assetRepoMock
222  ->expects($this->once())
223  ->method('createArbitrary')
224  ->with($path, '')
225  ->willReturn($this->asset);
226 
227  $this->assertSame($this->asset, $this->object->createRequireJsMixinsAsset());
228  }
229 
230  public function testClearBundleJsPool()
231  {
232  $context = $this->getMockBuilder(\Magento\Framework\View\Asset\File\FallbackContext::class)
233  ->disableOriginalConstructor()
234  ->getMock();
235  $this->fileSystem->expects($this->once())
236  ->method('getDirectoryWrite')
238  ->willReturn($this->dir);
239  $this->assetRepoMock
240  ->expects($this->once())
241  ->method('getStaticViewFileContext')
242  ->willReturn($context);
243  $context->expects($this->once())
244  ->method('getPath')
245  ->willReturn('/path/to/directory');
246  $this->dir->expects($this->once())
247  ->method('delete')
248  ->with('/path/to/directory/' . \Magento\Framework\RequireJs\Config::BUNDLE_JS_DIR)
249  ->willReturn(true);
250  $this->assertTrue($this->object->clearBundleJsPool());
251  }
252 }
$config
Definition: fraud_order.php:17