Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DownloadCssTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class DownloadCssTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $registry;
20 
24  protected $fileFactory;
25 
29  protected $repository;
30 
34  protected $filesystem;
35 
39  protected $objectManager;
40 
44  protected $messageManager;
45 
49  protected $redirect;
50 
54  protected $request;
55 
59  protected $response;
60 
64  protected $resultFactory;
65 
69  protected $controller;
70 
71  protected function setUp()
72  {
73  $context = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
74  ->disableOriginalConstructor()
75  ->getMock();
76  $this->request = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)->getMock();
77  $this->redirect = $this->getMockBuilder(\Magento\Framework\App\Response\RedirectInterface::class)->getMock();
78  $this->response = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
79  ->setMethods(['sendResponse', 'setRedirect'])
80  ->getMock();
81  $this->objectManager = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class)->getMock();
82  $this->messageManager = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)->getMock();
83  $this->resultFactory = $this->getMockBuilder(\Magento\Framework\Controller\ResultFactory::class)
84  ->disableOriginalConstructor()
85  ->getMock();
86  $context->expects($this->any())
87  ->method('getRequest')
88  ->willReturn($this->request);
89  $context->expects($this->any())
90  ->method('getRedirect')
91  ->willReturn($this->redirect);
92  $context->expects($this->any())
93  ->method('getResponse')
94  ->willReturn($this->response);
95  $context->expects($this->any())
96  ->method('getObjectManager')
97  ->willReturn($this->objectManager);
98  $context->expects($this->any())
99  ->method('getMessageManager')
100  ->willReturn($this->messageManager);
101  $context->expects($this->any())
102  ->method('getResultFactory')
103  ->willReturn($this->resultFactory);
104 
105  $this->registry = $this->getMockBuilder(
106  \Magento\Framework\Registry::class
107  )->disableOriginalConstructor()->getMock();
108  $this->fileFactory = $this->getMockBuilder(\Magento\Framework\App\Response\Http\FileFactory::class)
109  ->disableOriginalConstructor()
110  ->getMock();
111  $this->repository = $this->getMockBuilder(\Magento\Framework\View\Asset\Repository::class)
112  ->disableOriginalConstructor()
113  ->getMock();
114  $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
115  ->disableOriginalConstructor()
116  ->getMock();
117 
119  $this->controller = new DownloadCss(
120  $context,
121  $this->registry,
122  $this->fileFactory,
123  $this->repository,
124  $this->filesystem
125  );
126  }
127 
128  public function testExecute()
129  {
130  $themeId = 1;
131  $fileParam = '/path/to/file.ext';
132  $fileId = 'fileId';
133  $sourceFile = '/source/file.ext';
134  $relPath = 'file.ext';
135 
136  $this->request->expects($this->any())
137  ->method('getParam')
138  ->willReturnMap(
139  [
140  ['theme_id', null, $themeId],
141  ['file', null, $fileParam],
142  ]
143  );
144  $file = $this->getMockBuilder(\Magento\Framework\View\Asset\File::class)
145  ->disableOriginalConstructor()
146  ->getMock();
147  $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
148  ->setMethods(['getId', 'load'])
149  ->getMockForAbstractClass();
150  $urlDecoder = $this->getMockBuilder(\Magento\Framework\Url\DecoderInterface::class)->getMock();
151  $directoryRead = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)->getMock();
152  $this->objectManager->expects($this->any())
153  ->method('get')
154  ->with(\Magento\Framework\Url\DecoderInterface::class)
155  ->willReturn($urlDecoder);
156  $this->objectManager->expects($this->any())
157  ->method('create')
158  ->with(\Magento\Framework\View\Design\ThemeInterface::class)
159  ->willReturn($theme);
160  $urlDecoder->expects($this->once())
161  ->method('decode')
162  ->with($fileParam)
163  ->willReturn($fileId);
164  $theme->expects($this->once())
165  ->method('load')
166  ->with($themeId)
167  ->willReturnSelf();
168  $theme->expects($this->once())
169  ->method('getId')
170  ->willReturn($themeId);
171  $this->repository->expects($this->once())
172  ->method('createAsset')
173  ->with($fileId, ['themeModel' => $theme])
174  ->willReturn($file);
175  $this->filesystem->expects($this->once())
176  ->method('getDirectoryRead')
177  ->with(DirectoryList::ROOT)
178  ->willReturn($directoryRead);
179  $file->expects($this->once())
180  ->method('getSourceFile')
181  ->willReturn($sourceFile);
182  $directoryRead->expects($this->once())
183  ->method('getRelativePath')
184  ->with($sourceFile)
185  ->willReturn($relPath);
186  $this->fileFactory->expects($this->once())
187  ->method('create')
188  ->with($relPath, ['type' => 'filename', 'value' => $relPath], DirectoryList::ROOT)
189  ->willReturn($this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)->getMock());
190 
191  $this->assertInstanceOf(\Magento\Framework\App\ResponseInterface::class, $this->controller->execute());
192  }
193 
194  public function testExecuteInvalidArgument()
195  {
196  $themeId = 1;
197  $fileParam = '/path/to/file.ext';
198  $fileId = 'fileId';
199  $refererUrl = 'referer/url';
200 
201  $this->request->expects($this->any())
202  ->method('getParam')
203  ->willReturnMap(
204  [
205  ['theme_id', null, $themeId],
206  ['file', null, $fileParam],
207  ]
208  );
209  $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
210  ->setMethods(['getId', 'load'])
211  ->getMockForAbstractClass();
212  $urlDecoder = $this->getMockBuilder(\Magento\Framework\Url\DecoderInterface::class)->getMock();
213  $logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
214  $this->objectManager->expects($this->any())
215  ->method('get')
216  ->willReturnMap(
217  [
218  [\Magento\Framework\Url\DecoderInterface::class, $urlDecoder],
219  [\Psr\Log\LoggerInterface::class, $logger],
220  ]
221  );
222  $this->objectManager->expects($this->any())
223  ->method('create')
224  ->with(\Magento\Framework\View\Design\ThemeInterface::class)
225  ->willReturn($theme);
226  $urlDecoder->expects($this->once())
227  ->method('decode')
228  ->with($fileParam)
229  ->willReturn($fileId);
230  $theme->expects($this->once())
231  ->method('load')
232  ->with($themeId)
233  ->willReturnSelf();
234  $theme->expects($this->once())
235  ->method('getId')
236  ->willReturn(null);
237  $this->messageManager->expects($this->once())
238  ->method('addException');
239  $logger->expects($this->once())
240  ->method('critical');
241  $this->redirect->expects($this->once())
242  ->method('getRefererUrl')
243  ->willReturn($refererUrl);
244  $this->response->expects($this->once())
245  ->method('setRedirect')
246  ->with($refererUrl);
247 
248  $this->controller->execute();
249  }
250 }
$logger
$theme