Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UploadJsTest.php
Go to the documentation of this file.
1 <?php
7 
9 {
11  protected $name = 'UploadJs';
12 
14  protected $serviceModel;
15 
17  protected $themeFactory;
18 
20  protected $customizationJs;
21 
23  protected $jsonHelper;
24 
26  protected $logger;
27 
30 
31  protected function setUp()
32  {
33  parent::setUp();
34  $this->serviceModel = $this->createMock(\Magento\Theme\Model\Uploader\Service::class);
35  $this->themeFactory = $this->createMock(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
36  $this->jsonHelper = $this->createMock(\Magento\Framework\Json\Helper\Data::class);
37  $this->logger = $this->getMockForAbstractClass(\Psr\Log\LoggerInterface::class, [], '', false);
38  $this->themeCustomization = $this->getMockForAbstractClass(
39  \Magento\Framework\View\Design\Theme\CustomizationInterface::class,
40  [],
41  '',
42  false,
43  false,
44  true,
45  [
46  'generateFileInfo',
47  'getFilesByType'
48  ]
49  );
50  $this->customizationJs = $this->createMock(\Magento\Framework\View\Design\Theme\Customization\File\Js::class);
51  }
52 
53  public function testExecuteWithoutTheme()
54  {
55  $themeId = 23;
56 
57  $this->_request->expects($this->at(0))
58  ->method('getParam')
59  ->with('id')
60  ->willReturn($themeId);
61 
62  $this->_objectManagerMock
63  ->expects($this->at(0))
64  ->method('get')
65  ->with(\Magento\Theme\Model\Uploader\Service::class)
66  ->willReturn($this->serviceModel);
67  $this->_objectManagerMock
68  ->expects($this->at(1))
69  ->method('get')
70  ->with(\Magento\Framework\View\Design\Theme\FlyweightFactory::class)
71  ->willReturn($this->themeFactory);
72  $this->_objectManagerMock
73  ->expects($this->at(2))
74  ->method('get')
75  ->with(\Magento\Framework\View\Design\Theme\Customization\File\Js::class)
76  ->willReturn($this->customizationJs);
77  $this->_objectManagerMock
78  ->expects($this->at(3))
79  ->method('get')
80  ->with(\Magento\Framework\Json\Helper\Data::class)
81  ->willReturn($this->jsonHelper);
82 
83  $this->themeFactory->expects($this->once())
84  ->method('create')
85  ->willReturn(null);
86  $this->jsonHelper
87  ->expects($this->once())
88  ->method('jsonEncode')
89  ->with(['error' => true, 'message' => "We cannot find a theme with id \"$themeId\"."])
90  ->willReturn('{"error":"true","message":"We cannot find a theme with id "' . $themeId . '"."}');
91  $this->response->expects($this->once())
92  ->method('representJson')
93  ->with('{"error":"true","message":"We cannot find a theme with id "' . $themeId . '"."}');
94 
95  $this->_model->execute();
96  }
97 
98  public function testExecuteWithException()
99  {
100  $themeId = 23;
101 
102  $this->_request->expects($this->at(0))
103  ->method('getParam')
104  ->with('id')
105  ->willReturn($themeId);
106 
107  $this->_objectManagerMock->expects($this->at(0))
108  ->method('get')
109  ->with(\Magento\Theme\Model\Uploader\Service::class)
110  ->willReturn($this->serviceModel);
111  $this->_objectManagerMock->expects($this->at(1))
112  ->method('get')
113  ->with(\Magento\Framework\View\Design\Theme\FlyweightFactory::class)
114  ->willReturn($this->themeFactory);
115  $this->_objectManagerMock
116  ->expects($this->at(2))
117  ->method('get')
118  ->with(\Magento\Framework\View\Design\Theme\Customization\File\Js::class)
119  ->willReturn($this->customizationJs);
120  $this->_objectManagerMock
121  ->expects($this->at(4))
122  ->method('get')
123  ->with(\Magento\Framework\Json\Helper\Data::class)
124  ->willReturn($this->jsonHelper);
125 
126  $this->themeFactory->expects($this->once())
127  ->method('create')
128  ->willThrowException(new \Exception('Message'));
129 
130  $this->_objectManagerMock->expects($this->at(3))
131  ->method('get')
132  ->with(\Psr\Log\LoggerInterface::class)
133  ->willReturn($this->logger);
134  $this->logger->expects($this->once())
135  ->method('critical');
136 
137  $this->jsonHelper->expects($this->once())
138  ->method('jsonEncode')
139  ->with(['error' => true, 'message' => 'We can\'t upload the JS file right now.'])
140  ->willReturn('{"error":"true","message":"We can\'t upload the JS file right now."}');
141  $this->response->expects($this->once())
142  ->method('representJson')
143  ->with('{"error":"true","message":"We can\'t upload the JS file right now."}');
144 
145  $this->_model->execute();
146  }
147 
148  public function testExecute()
149  {
150  $themeId = 23;
151  $theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class, [], '', false);
152  $jsFile = $this->getMockForAbstractClass(
153  \Magento\Framework\View\Design\Theme\FileInterface::class,
154  [],
155  '',
156  false,
157  true,
158  true,
159  [
160  'setTheme',
161  'setFileName',
162  'setData',
163  'save',
164  ]
165  );
166 
167  $this->_request->expects($this->at(0))
168  ->method('getParam')
169  ->with('id')
170  ->willReturn($themeId);
171 
172  $this->_objectManagerMock->expects($this->at(0))
173  ->method('get')
174  ->with(\Magento\Theme\Model\Uploader\Service::class)
175  ->willReturn($this->serviceModel);
176  $this->_objectManagerMock->expects($this->at(1))
177  ->method('get')
178  ->with(\Magento\Framework\View\Design\Theme\FlyweightFactory::class)
179  ->willReturn($this->themeFactory);
180  $this->_objectManagerMock->expects($this->at(2))
181  ->method('get')
182  ->with(\Magento\Framework\View\Design\Theme\Customization\File\Js::class)
183  ->willReturn($this->customizationJs);
184  $this->_objectManagerMock->expects($this->at(4))
185  ->method('get')
186  ->with(\Magento\Framework\Json\Helper\Data::class)
187  ->willReturn($this->jsonHelper);
188 
189  $this->themeFactory->expects($this->once())
190  ->method('create')
191  ->willReturn($theme);
192  $this->serviceModel
193  ->expects($this->once())
194  ->method('uploadJsFile')
195  ->with('js_files_uploader')
196  ->willReturn(['filename' => 'filename', 'content' => 'content']);
197  $this->customizationJs
198  ->expects($this->once())
199  ->method('create')
200  ->willReturn($jsFile);
201  $jsFile->expects($this->once())
202  ->method('setTheme')
203  ->with($theme);
204  $jsFile->expects($this->once())
205  ->method('setFileName')
206  ->with('filename');
207  $jsFile->expects($this->once())
208  ->method('setData')
209  ->with('content', 'content');
210  $jsFile->expects($this->once())
211  ->method('save');
212 
213  $this->_objectManagerMock->expects($this->once())
214  ->method('create')
215  ->with(
216  \Magento\Framework\View\Design\Theme\CustomizationInterface::class,
217  ['theme' => $theme]
218  )
219  ->willReturn($this->themeCustomization);
220  $this->themeCustomization
221  ->expects($this->once())
222  ->method('getFilesByType')
223  ->with(\Magento\Framework\View\Design\Theme\Customization\File\Js::TYPE)
224  ->willReturn([$jsFile]);
225  $this->themeCustomization
226  ->expects($this->once())
227  ->method('generateFileInfo')
228  ->with([$jsFile])
229  ->willReturn(['fileOne' => ['name' => 'name']]);
230 
231  $this->jsonHelper
232  ->expects($this->once())
233  ->method('jsonEncode')
234  ->with(['error' => false, 'files' => ['fileOne' => ['name' => 'name']]])
235  ->willReturn('{"error":false,"files":{"fileOne":{"name":"name"}}}');
236  $this->response->expects($this->once())
237  ->method('representJson')
238  ->with('{"error":false,"files":{"fileOne":{"name":"name"}}}');
239 
240  $this->_model->execute();
241  }
242 }
$theme