Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StaticResourceTest.php
Go to the documentation of this file.
1 <?php
8 
13 use Magento\Framework\App\Request\Http as HttpRequest;
21 use Psr\Log\LoggerInterface;
22 use PHPUnit_Framework_MockObject_MockObject as MockObject;
23 
27 class StaticResourceTest extends \PHPUnit\Framework\TestCase
28 {
32  private $stateMock;
33 
37  private $responseMock;
38 
42  private $requestMock;
43 
47  private $publisherMock;
48 
52  private $assetRepoMock;
53 
57  private $moduleListMock;
58 
62  private $objectManagerMock;
63 
67  private $configLoaderMock;
68 
72  private $loggerMock;
73 
77  private $deploymentConfigMock;
78 
82  private $object;
83 
84  protected function setUp()
85  {
86  $this->stateMock = $this->createMock(State::class);
87  $this->responseMock = $this->getMockForAbstractClass(FileInterface::class);
88  $this->requestMock = $this->createMock(HttpRequest::class);
89  $this->publisherMock = $this->createMock(Publisher::class);
90  $this->assetRepoMock = $this->createMock(Repository::class);
91  $this->moduleListMock = $this->createMock(ModuleList::class);
92  $this->objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class);
93  $this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
94  $this->configLoaderMock = $this->createMock(ConfigLoader::class);
95  $this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
96  $this->object = new StaticResource(
97  $this->stateMock,
98  $this->responseMock,
99  $this->requestMock,
100  $this->publisherMock,
101  $this->assetRepoMock,
102  $this->moduleListMock,
103  $this->objectManagerMock,
104  $this->configLoaderMock,
105  $this->deploymentConfigMock
106  );
107  }
108 
109  public function testLaunchProductionMode()
110  {
111  $this->stateMock->expects($this->once())
112  ->method('getMode')
113  ->willReturn(State::MODE_PRODUCTION);
114  $this->responseMock->expects($this->once())
115  ->method('setHttpResponseCode')
116  ->with(404);
117  $this->responseMock->expects($this->never())
118  ->method('setFilePath');
119  $this->stateMock->expects($this->never())->method('setAreaCode');
120  $this->configLoaderMock->expects($this->never())->method('load');
121  $this->objectManagerMock->expects($this->never())->method('configure');
122  $this->requestMock->expects($this->never())->method('get');
123  $this->moduleListMock->expects($this->never())->method('has');
124  $asset = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\LocalInterface::class);
125  $asset->expects($this->never())->method('getSourceFile');
126  $this->assetRepoMock->expects($this->never())->method('createAsset');
127  $this->publisherMock->expects($this->never())->method('publish');
128  $this->responseMock->expects($this->never())->method('setFilePath');
129  $this->object->launch();
130  }
131 
144  public function testLaunch(
145  $mode,
146  $requestedPath,
147  $requestedModule,
148  $moduleExists,
149  $expectedFile,
150  array $expectedParams,
151  $getConfigDataExpects,
152  $staticContentOmDemandInProduction
153  ) {
154  $this->deploymentConfigMock->expects($this->exactly($getConfigDataExpects))
155  ->method('getConfigData')
157  ->willReturn($staticContentOmDemandInProduction);
158  $this->stateMock->expects($this->once())
159  ->method('getMode')
160  ->willReturn($mode);
161  $this->stateMock->expects($this->once())
162  ->method('setAreaCode')
163  ->with('area');
164  $this->configLoaderMock->expects($this->once())
165  ->method('load')
166  ->with('area')
167  ->willReturn(['config']);
168  $this->objectManagerMock->expects($this->once())
169  ->method('configure')
170  ->with(['config']);
171  $this->requestMock->expects($this->once())
172  ->method('get')
173  ->with('resource')
174  ->willReturn($requestedPath);
175  $this->moduleListMock->expects($this->any())
176  ->method('has')
177  ->with($requestedModule)
178  ->willReturn($moduleExists);
179  $asset = $this->getMockForAbstractClass(\Magento\Framework\View\Asset\LocalInterface::class);
180  $asset->expects($this->once())
181  ->method('getSourceFile')
182  ->willReturn('resource/file.css');
183  $this->assetRepoMock->expects($this->once())
184  ->method('createAsset')
185  ->with($expectedFile, $expectedParams)
186  ->willReturn($asset);
187  $this->publisherMock->expects($this->once())
188  ->method('publish')
189  ->with($asset);
190  $this->responseMock->expects($this->once())
191  ->method('setFilePath')
192  ->with('resource/file.css');
193  $this->object->launch();
194  }
195 
199  public function launchDataProvider()
200  {
201  return [
202  'developer mode with non-modular resource' => [
204  'area/Magento/theme/locale/dir/file.js',
205  'dir',
206  false,
207  'dir/file.js',
208  ['area' => 'area', 'locale' => 'locale', 'module' => '', 'theme' => 'Magento/theme'],
209  0,
210  0,
211  ],
212  'default mode with modular resource' => [
214  'area/Magento/theme/locale/Namespace_Module/dir/file.js',
215  'Namespace_Module',
216  true,
217  'dir/file.js',
218  [
219  'area' => 'area', 'locale' => 'locale', 'module' => 'Namespace_Module', 'theme' => 'Magento/theme'
220  ],
221  0,
222  0,
223  ],
224  'production mode with static_content_on_demand_in_production and with non-modular resource' => [
226  'area/Magento/theme/locale/dir/file.js',
227  'dir',
228  false,
229  'dir/file.js',
230  ['area' => 'area', 'locale' => 'locale', 'module' => '', 'theme' => 'Magento/theme'],
231  1,
232  1,
233  ],
234  'production mode with static_content_on_demand_in_production and with modular resource' => [
236  'area/Magento/theme/locale/Namespace_Module/dir/file.js',
237  'Namespace_Module',
238  true,
239  'dir/file.js',
240  [
241  'area' => 'area', 'locale' => 'locale', 'module' => 'Namespace_Module', 'theme' => 'Magento/theme'
242  ],
243  1,
244  1,
245  ],
246  ];
247  }
248 
253  public function testLaunchWrongPath()
254  {
255  $this->stateMock->expects($this->once())
256  ->method('getMode')
257  ->will($this->returnValue(\Magento\Framework\App\State::MODE_DEVELOPER));
258  $this->requestMock->expects($this->once())
259  ->method('get')
260  ->with('resource')
261  ->willReturn('short/path.js');
262  $this->object->launch();
263  }
264 
266  {
267  $this->objectManagerMock->expects($this->once())
268  ->method('get')
269  ->with(\Psr\Log\LoggerInterface::class)
270  ->willReturn($this->loggerMock);
271  $this->loggerMock->expects($this->once())
272  ->method('critical');
273  $bootstrap = $this->getMockBuilder(Bootstrap::class)
274  ->disableOriginalConstructor()
275  ->getMock();
276  $bootstrap->expects($this->once())
277  ->method('isDeveloperMode')
278  ->willReturn(true);
279  $exception = new \Exception('Error: nothing works');
280  $this->responseMock->expects($this->once())
281  ->method('setHttpResponseCode')
282  ->with(404);
283  $this->responseMock->expects($this->once())
284  ->method('sendResponse');
285  $this->assertTrue($this->object->catchException($bootstrap, $exception));
286  }
287 
291  public function testLaunchPathAbove()
292  {
293  $path = 'frontend/..\..\folder_above/././Magento_Ui/template/messages.html';
294  $this->stateMock->expects($this->once())
295  ->method('getMode')
296  ->will($this->returnValue(State::MODE_DEVELOPER));
297  $this->requestMock->expects($this->once())
298  ->method('get')
299  ->with('resource')
300  ->willReturn('frontend/..\..\folder_above/././Magento_Ui/template/messages.html');
301  $this->expectExceptionMessage("Requested path '$path' is wrong.");
302 
303  $this->object->launch();
304  }
305 }
if(defined('TESTS_MAGENTO_INSTALLATION') &&TESTS_MAGENTO_INSTALLATION==='enabled') $bootstrap
Definition: bootstrap.php:73
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
testLaunch( $mode, $requestedPath, $requestedModule, $moduleExists, $expectedFile, array $expectedParams, $getConfigDataExpects, $staticContentOmDemandInProduction)