Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DownloadCustomCssTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class DownloadCustomCssTest 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 DownloadCustomCss(
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  $fileName = 'file.ext';
132  $fullPath = 'path/to/file';
133 
134  $file = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\FileInterface::class)->getMock();
135  $customization = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\Customization::class)
136  ->disableOriginalConstructor()
137  ->getMock();
138  $theme = $this->getMockBuilder(\Magento\Framework\View\Design\ThemeInterface::class)
139  ->setMethods(['getCustomization'])
140  ->getMockForAbstractClass();
141  $file->expects($this->once())
142  ->method('getContent')
143  ->willReturn('some_content');
144  $file->expects($this->once())
145  ->method('getFilename')
146  ->willReturn($fileName);
147  $file->expects($this->once())
148  ->method('getFullPath')
149  ->willReturn($fullPath);
150  $theme->expects($this->once())
151  ->method('getCustomization')
152  ->willReturn($customization);
153  $customization->expects($this->once())
154  ->method('getFilesByType')
155  ->with(\Magento\Theme\Model\Theme\Customization\File\CustomCss::TYPE)
156  ->willReturn([$file]);
157  $this->request->expects($this->any())
158  ->method('getParam')
159  ->with('theme_id')
160  ->willReturn($themeId);
161  $themeFactory = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\FlyweightFactory::class)
162  ->setMethods(['create'])
163  ->disableOriginalConstructor()
164  ->getMock();
165  $this->objectManager->expects($this->any())
166  ->method('create')
167  ->with(\Magento\Framework\View\Design\Theme\FlyweightFactory::class)
168  ->willReturn($themeFactory);
169  $themeFactory->expects($this->once())
170  ->method('create')
171  ->with($themeId)
172  ->willReturn($theme);
173  $this->fileFactory->expects($this->once())
174  ->method('create')
175  ->with($fileName, ['type' => 'filename', 'value' => $fullPath], DirectoryList::ROOT)
176  ->willReturn($this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)->getMock());
177 
178  $this->assertInstanceOf(\Magento\Framework\App\ResponseInterface::class, $this->controller->execute());
179  }
180 
181  public function testExecuteInvalidArgument()
182  {
183  $themeId = 1;
184  $refererUrl = 'referer/url';
185 
186  $this->request->expects($this->any())
187  ->method('getParam')
188  ->with('theme_id')
189  ->willReturn($themeId);
190  $themeFactory = $this->getMockBuilder(\Magento\Framework\View\Design\Theme\FlyweightFactory::class)
191  ->setMethods(['create'])
192  ->disableOriginalConstructor()
193  ->getMock();
194  $logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
195  $this->objectManager->expects($this->any())
196  ->method('get')
197  ->with(\Psr\Log\LoggerInterface::class)
198  ->willReturn($logger);
199  $this->objectManager->expects($this->any())
200  ->method('create')
201  ->with(\Magento\Framework\View\Design\Theme\FlyweightFactory::class)
202  ->willReturn($themeFactory);
203  $themeFactory->expects($this->once())
204  ->method('create')
205  ->with($themeId)
206  ->willReturn(null);
207  $this->messageManager->expects($this->once())
208  ->method('addException');
209  $logger->expects($this->once())
210  ->method('critical');
211  $this->redirect->expects($this->once())
212  ->method('getRefererUrl')
213  ->willReturn($refererUrl);
214  $this->response->expects($this->once())
215  ->method('setRedirect')
216  ->with($refererUrl);
217 
218  $this->controller->execute();
219  }
220 }
$logger
$fileName
Definition: translate.phtml:15
$theme