5
5
*/
6
6
namespace Magento \Cms \Test \Unit \Controller \Adminhtml \Wysiwyg ;
7
7
8
+ use Magento \Backend \App \Action \Context ;
9
+ use Magento \Cms \Controller \Adminhtml \Wysiwyg \Directive ;
10
+ use Magento \Cms \Model \Template \Filter ;
11
+ use Magento \Cms \Model \Wysiwyg \Config ;
12
+ use Magento \Framework \App \RequestInterface ;
13
+ use Magento \Framework \App \ResponseInterface ;
14
+ use Magento \Framework \Controller \Result \Raw ;
15
+ use Magento \Framework \Controller \Result \RawFactory ;
16
+ use Magento \Framework \Filesystem \Driver \File ;
17
+ use Magento \Framework \Image \Adapter \AdapterInterface ;
18
+ use Magento \Framework \Image \AdapterFactory ;
19
+ use Magento \Framework \ObjectManagerInterface ;
20
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
21
+ use Magento \Framework \Url \DecoderInterface ;
22
+ use PHPUnit \Framework \TestCase ;
23
+ use PHPUnit_Framework_MockObject_MockObject ;
24
+ use Psr \Log \LoggerInterface ;
25
+
8
26
/**
9
27
* @covers \Magento\Cms\Controller\Adminhtml\Wysiwyg\Directive
10
28
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
11
29
*/
12
- class DirectiveTest extends \ PHPUnit \ Framework \ TestCase
30
+ class DirectiveTest extends TestCase
13
31
{
14
32
const IMAGE_PATH = 'pub/media/wysiwyg/image.jpg ' ;
15
33
16
34
/**
17
- * @var \Magento\Cms\Controller\Adminhtml\Wysiwyg\ Directive
35
+ * @var Directive
18
36
*/
19
37
protected $ wysiwygDirective ;
20
38
21
39
/**
22
- * @var \Magento\Backend\App\Action\ Context|\ PHPUnit_Framework_MockObject_MockObject
40
+ * @var Context|PHPUnit_Framework_MockObject_MockObject
23
41
*/
24
42
protected $ actionContextMock ;
25
43
26
44
/**
27
- * @var \Magento\Framework\App\ RequestInterface|\ PHPUnit_Framework_MockObject_MockObject
45
+ * @var RequestInterface|PHPUnit_Framework_MockObject_MockObject
28
46
*/
29
47
protected $ requestMock ;
30
48
31
49
/**
32
- * @var \Magento\Framework\Url\ DecoderInterface|\ PHPUnit_Framework_MockObject_MockObject
50
+ * @var DecoderInterface|PHPUnit_Framework_MockObject_MockObject
33
51
*/
34
52
protected $ urlDecoderMock ;
35
53
36
54
/**
37
- * @var \Magento\Framework\ ObjectManagerInterface|\ PHPUnit_Framework_MockObject_MockObject
55
+ * @var ObjectManagerInterface|PHPUnit_Framework_MockObject_MockObject
38
56
*/
39
57
protected $ objectManagerMock ;
40
58
41
59
/**
42
- * @var \Magento\Cms\Model\Template\ Filter|\ PHPUnit_Framework_MockObject_MockObject
60
+ * @var Filter|PHPUnit_Framework_MockObject_MockObject
43
61
*/
44
62
protected $ templateFilterMock ;
45
63
46
64
/**
47
- * @var \Magento\Framework\Image\ AdapterFactory|\ PHPUnit_Framework_MockObject_MockObject
65
+ * @var AdapterFactory|PHPUnit_Framework_MockObject_MockObject
48
66
*/
49
67
protected $ imageAdapterFactoryMock ;
50
68
51
69
/**
52
- * @var \Magento\Framework\Image\Adapter\ AdapterInterface|\ PHPUnit_Framework_MockObject_MockObject
70
+ * @var AdapterInterface|PHPUnit_Framework_MockObject_MockObject
53
71
*/
54
72
protected $ imageAdapterMock ;
55
73
56
74
/**
57
- * @var \Magento\Framework\App\ ResponseInterface|\ PHPUnit_Framework_MockObject_MockObject
75
+ * @var ResponseInterface|PHPUnit_Framework_MockObject_MockObject
58
76
*/
59
77
protected $ responseMock ;
60
78
61
79
/**
62
- * @var \Magento\Cms\Model\Wysiwyg\Config|\PHPUnit_Framework_MockObject_MockObject
80
+ * @var File|PHPUnit_Framework_MockObject_MockObject
81
+ */
82
+ protected $ fileMock ;
83
+
84
+ /**
85
+ * @var Config|PHPUnit_Framework_MockObject_MockObject
63
86
*/
64
87
protected $ wysiwygConfigMock ;
65
88
66
89
/**
67
- * @var \Psr\Log\ LoggerInterface|\ PHPUnit_Framework_MockObject_MockObject
90
+ * @var LoggerInterface|PHPUnit_Framework_MockObject_MockObject
68
91
*/
69
92
protected $ loggerMock ;
70
93
71
94
/**
72
- * @var \Magento\Framework\Controller\Result\ RawFactory|\ PHPUnit_Framework_MockObject_MockObject
95
+ * @var RawFactory|PHPUnit_Framework_MockObject_MockObject
73
96
*/
74
97
protected $ rawFactoryMock ;
75
98
76
99
/**
77
- * @var \Magento\Framework\Controller\Result\ Raw|\ PHPUnit_Framework_MockObject_MockObject
100
+ * @var Raw|PHPUnit_Framework_MockObject_MockObject
78
101
*/
79
102
protected $ rawMock ;
80
103
81
104
protected function setUp ()
82
105
{
83
- $ this ->actionContextMock = $ this ->getMockBuilder (\ Magento \ Backend \ App \ Action \ Context::class)
106
+ $ this ->actionContextMock = $ this ->getMockBuilder (Context::class)
84
107
->disableOriginalConstructor ()
85
108
->getMock ();
86
- $ this ->requestMock = $ this ->getMockBuilder (\ Magento \ Framework \ App \ RequestInterface::class)
109
+ $ this ->requestMock = $ this ->getMockBuilder (RequestInterface::class)
87
110
->disableOriginalConstructor ()
88
111
->getMock ();
89
- $ this ->urlDecoderMock = $ this ->getMockBuilder (\ Magento \ Framework \ Url \ DecoderInterface::class)
112
+ $ this ->urlDecoderMock = $ this ->getMockBuilder (DecoderInterface::class)
90
113
->disableOriginalConstructor ()
91
114
->getMock ();
92
- $ this ->objectManagerMock = $ this ->getMockBuilder (\ Magento \ Framework \ ObjectManagerInterface::class)
115
+ $ this ->objectManagerMock = $ this ->getMockBuilder (ObjectManagerInterface::class)
93
116
->disableOriginalConstructor ()
94
117
->getMock ();
95
- $ this ->templateFilterMock = $ this ->getMockBuilder (\ Magento \ Cms \ Model \ Template \ Filter::class)
118
+ $ this ->templateFilterMock = $ this ->getMockBuilder (Filter::class)
96
119
->disableOriginalConstructor ()
97
120
->getMock ();
98
- $ this ->imageAdapterFactoryMock = $ this ->getMockBuilder (\ Magento \ Framework \ Image \ AdapterFactory::class)
121
+ $ this ->imageAdapterFactoryMock = $ this ->getMockBuilder (AdapterFactory::class)
99
122
->disableOriginalConstructor ()
100
123
->getMock ();
101
- $ this ->imageAdapterMock = $ this ->getMockBuilder (\ Magento \ Framework \ Image \ Adapter \ AdapterInterface::class)
124
+ $ this ->imageAdapterMock = $ this ->getMockBuilder (AdapterInterface::class)
102
125
->disableOriginalConstructor ()
103
126
->setMethods (
104
127
[
@@ -117,21 +140,25 @@ protected function setUp()
117
140
]
118
141
)
119
142
->getMock ();
120
- $ this ->responseMock = $ this ->getMockBuilder (\ Magento \ Framework \ App \ ResponseInterface::class)
143
+ $ this ->responseMock = $ this ->getMockBuilder (ResponseInterface::class)
121
144
->disableOriginalConstructor ()
122
145
->setMethods (['setHeader ' , 'setBody ' , 'sendResponse ' ])
123
146
->getMock ();
124
- $ this ->wysiwygConfigMock = $ this ->getMockBuilder (\ Magento \ Cms \ Model \ Wysiwyg \Config ::class)
147
+ $ this ->fileMock = $ this ->getMockBuilder (File ::class)
125
148
->disableOriginalConstructor ()
149
+ ->setMethods (['fileGetContents ' ])
126
150
->getMock ();
127
- $ this ->loggerMock = $ this ->getMockBuilder (\ Psr \ Log \LoggerInterface ::class)
151
+ $ this ->wysiwygConfigMock = $ this ->getMockBuilder (Config ::class)
128
152
->disableOriginalConstructor ()
129
153
->getMock ();
130
- $ this ->rawFactoryMock = $ this ->getMockBuilder (\Magento \Framework \Controller \Result \RawFactory::class)
154
+ $ this ->loggerMock = $ this ->getMockBuilder (LoggerInterface::class)
155
+ ->disableOriginalConstructor ()
156
+ ->getMock ();
157
+ $ this ->rawFactoryMock = $ this ->getMockBuilder (RawFactory::class)
131
158
->setMethods (['create ' ])
132
159
->disableOriginalConstructor ()
133
160
->getMock ();
134
- $ this ->rawMock = $ this ->getMockBuilder (\ Magento \ Framework \ Controller \ Result \ Raw::class)
161
+ $ this ->rawMock = $ this ->getMockBuilder (Raw::class)
135
162
->disableOriginalConstructor ()
136
163
->getMock ();
137
164
@@ -145,17 +172,18 @@ protected function setUp()
145
172
->method ('getObjectManager ' )
146
173
->willReturn ($ this ->objectManagerMock );
147
174
148
- $ objectManager = new \ Magento \ Framework \ TestFramework \ Unit \ Helper \ ObjectManager ($ this );
175
+ $ objectManager = new ObjectManager ($ this );
149
176
$ this ->wysiwygDirective = $ objectManager ->getObject (
150
- \ Magento \ Cms \ Controller \ Adminhtml \ Wysiwyg \ Directive::class,
177
+ Directive::class,
151
178
[
152
179
'context ' => $ this ->actionContextMock ,
153
180
'urlDecoder ' => $ this ->urlDecoderMock ,
154
181
'resultRawFactory ' => $ this ->rawFactoryMock ,
155
182
'adapterFactory ' => $ this ->imageAdapterFactoryMock ,
156
183
'logger ' => $ this ->loggerMock ,
157
184
'config ' => $ this ->wysiwygConfigMock ,
158
- 'filter ' => $ this ->templateFilterMock
185
+ 'filter ' => $ this ->templateFilterMock ,
186
+ 'file ' => $ this ->fileMock ,
159
187
]
160
188
);
161
189
}
@@ -172,23 +200,29 @@ public function testExecute()
172
200
$ this ->imageAdapterMock ->expects ($ this ->once ())
173
201
->method ('open ' )
174
202
->with (self ::IMAGE_PATH );
175
- $ this ->imageAdapterMock ->expects ($ this ->once ())
203
+ $ this ->imageAdapterMock ->expects ($ this ->atLeastOnce ())
176
204
->method ('getMimeType ' )
177
205
->willReturn ($ mimeType );
178
- $ this ->rawMock ->expects ($ this ->once ())
206
+ $ this ->rawMock ->expects ($ this ->atLeastOnce ())
179
207
->method ('setHeader ' )
180
208
->with ('Content-Type ' , $ mimeType )
181
209
->willReturnSelf ();
182
- $ this ->rawMock ->expects ($ this ->once ())
210
+ $ this ->rawMock ->expects ($ this ->atLeastOnce ())
183
211
->method ('setContents ' )
184
212
->with ($ imageBody )
185
213
->willReturnSelf ();
186
214
$ this ->imageAdapterMock ->expects ($ this ->once ())
187
215
->method ('getImage ' )
188
216
->willReturn ($ imageBody );
217
+ $ this ->fileMock ->expects ($ this ->once ())
218
+ ->method ('fileGetContents ' )
219
+ ->willReturn ($ imageBody );
189
220
$ this ->rawFactoryMock ->expects ($ this ->any ())
190
221
->method ('create ' )
191
222
->willReturn ($ this ->rawMock );
223
+ $ this ->imageAdapterFactoryMock ->expects ($ this ->once ())
224
+ ->method ('create ' )
225
+ ->willReturn ($ this ->imageAdapterMock );
192
226
193
227
$ this ->assertSame (
194
228
$ this ->rawMock ,
@@ -217,27 +251,34 @@ public function testExecuteException()
217
251
$ this ->imageAdapterMock ->expects ($ this ->at (1 ))
218
252
->method ('open ' )
219
253
->with ($ placeholderPath );
220
- $ this ->imageAdapterMock ->expects ($ this ->once ())
254
+ $ this ->imageAdapterMock ->expects ($ this ->atLeastOnce ())
221
255
->method ('getMimeType ' )
222
256
->willReturn ($ mimeType );
223
- $ this ->rawMock ->expects ($ this ->once ())
257
+ $ this ->rawMock ->expects ($ this ->atLeastOnce ())
224
258
->method ('setHeader ' )
225
259
->with ('Content-Type ' , $ mimeType )
226
260
->willReturnSelf ();
227
- $ this ->rawMock ->expects ($ this ->once ())
261
+ $ this ->rawMock ->expects ($ this ->atLeastOnce ())
228
262
->method ('setContents ' )
229
263
->with ($ imageBody )
230
264
->willReturnSelf ();
231
- $ this ->imageAdapterMock ->expects ($ this ->once ())
265
+ $ this ->imageAdapterMock ->expects ($ this ->any ())
232
266
->method ('getImage ' )
233
267
->willReturn ($ imageBody );
268
+ $ this ->fileMock ->expects ($ this ->once ())
269
+ ->method ('fileGetContents ' )
270
+ ->willReturn ($ imageBody );
234
271
$ this ->loggerMock ->expects ($ this ->once ())
235
272
->method ('warning ' )
236
273
->with ($ exception );
237
274
$ this ->rawFactoryMock ->expects ($ this ->any ())
238
275
->method ('create ' )
239
276
->willReturn ($ this ->rawMock );
240
277
278
+ $ this ->imageAdapterFactoryMock ->expects ($ this ->exactly (1 ))
279
+ ->method ('create ' )
280
+ ->willReturn ($ this ->imageAdapterMock );
281
+
241
282
$ this ->assertSame (
242
283
$ this ->rawMock ,
243
284
$ this ->wysiwygDirective ->execute ()
@@ -297,6 +338,20 @@ public function testExecuteWithDeletedImage()
297
338
->method ('warning ' )
298
339
->with ($ exception );
299
340
341
+ $ this ->rawMock ->expects ($ this ->once ())
342
+ ->method ('setHeader ' )
343
+ ->with ('Content-Type ' , null )
344
+ ->willReturnSelf ();
345
+
346
+ $ this ->rawMock ->expects ($ this ->once ())
347
+ ->method ('setContents ' )
348
+ ->with (null )
349
+ ->willReturnSelf ();
350
+
351
+ $ this ->rawFactoryMock ->expects ($ this ->any ())
352
+ ->method ('create ' )
353
+ ->willReturn ($ this ->rawMock );
354
+
300
355
$ this ->wysiwygDirective ->execute ();
301
356
}
302
357
}
0 commit comments