10
10
use Magento \Backend \App \Action \Context ;
11
11
use Magento \Catalog \Model \Product \Media \Config ;
12
12
use Magento \Framework \App \RequestInterface ;
13
+ use Magento \Framework \Controller \Result \Raw ;
13
14
use Magento \Framework \Controller \Result \RawFactory ;
14
15
use Magento \Framework \DataObject ;
15
16
use Magento \Framework \Filesystem ;
@@ -99,6 +100,11 @@ class RetrieveImageTest extends TestCase
99
100
*/
100
101
private $ fileDriverMock ;
101
102
103
+ /**
104
+ * @var Raw|MockObject
105
+ */
106
+ private $ responseMock ;
107
+
102
108
private function setupObjectManagerForCheckImageExist ($ return )
103
109
{
104
110
$ objectManagerMock = $ this ->getMockForAbstractClass (ObjectManagerInterface::class);
@@ -119,8 +125,8 @@ protected function setUp(): void
119
125
->createMock (NotProtectedExtension::class);
120
126
$ this ->rawFactoryMock =
121
127
$ this ->createPartialMock (RawFactory::class, ['create ' ]);
122
- $ response = new DataObject ( );
123
- $ this ->rawFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ response );
128
+ $ this -> responseMock = $ this -> createMock (Raw::class );
129
+ $ this ->rawFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ this -> responseMock );
124
130
$ this ->configMock = $ this ->createMock (Config::class);
125
131
$ this ->filesystemMock = $ this ->createMock (Filesystem::class);
126
132
$ this ->adapterMock =
@@ -140,6 +146,8 @@ protected function setUp(): void
140
146
->getMockForAbstractClass ();
141
147
$ this ->contextMock ->expects ($ this ->any ())->method ('getRequest ' )->willReturn ($ this ->request );
142
148
$ this ->contextMock ->expects ($ this ->any ())->method ('getObjectManager ' )->willReturn ($ managerMock );
149
+ $ this ->fileDriverMock ->method ('stat ' )
150
+ ->willReturn (['size ' => 200 ]);
143
151
144
152
$ this ->image = $ objectManager ->getObject (
145
153
RetrieveImage::class,
@@ -172,12 +180,24 @@ public function testExecute()
172
180
$ writeInterface = $ this ->createMock (
173
181
WriteInterface::class
174
182
);
183
+ $ writeInterface ->method ('getDriver ' )
184
+ ->willReturn ($ this ->fileDriverMock );
175
185
$ this ->filesystemMock ->expects ($ this ->any ())->method ('getDirectoryRead ' )->willReturn ($ readInterface );
176
186
$ readInterface ->expects ($ this ->any ())->method ('getAbsolutePath ' )->willReturn ('' );
177
187
$ this ->abstractAdapter ->expects ($ this ->any ())->method ('validateUploadFile ' )->willReturn ('true ' );
178
188
$ this ->validatorMock ->expects ($ this ->once ())->method ('isValid ' )->with ('jpg ' )->willReturn ('true ' );
179
189
$ this ->filesystemMock ->expects ($ this ->once ())->method ('getDirectoryWrite ' )->willReturn ($ writeInterface );
180
190
$ this ->curlMock ->expects ($ this ->once ())->method ('read ' )->willReturn ('testimage ' );
191
+ $ this ->responseMock ->expects (self ::once ())
192
+ ->method ('setContents ' )
193
+ ->with (json_encode ([
194
+ 'name ' => 'test.jpg ' ,
195
+ 'type ' => null ,
196
+ 'error ' => 0 ,
197
+ 'size ' => 200 ,
198
+ 'url ' => null ,
199
+ 'file ' => '/t/e/test.jpg '
200
+ ], JSON_THROW_ON_ERROR ));
181
201
182
202
$ this ->image ->execute ();
183
203
}
@@ -192,6 +212,8 @@ public function testExecuteInvalidFileImage()
192
212
);
193
213
$ readInterface = $ this ->createMock (ReadInterface::class);
194
214
$ writeInterface = $ this ->createMock (WriteInterface::class);
215
+ $ writeInterface ->method ('getDriver ' )
216
+ ->willReturn ($ this ->fileDriverMock );
195
217
$ this ->filesystemMock ->expects ($ this ->any ())->method ('getDirectoryRead ' )->willReturn ($ readInterface );
196
218
$ readInterface ->expects ($ this ->any ())->method ('getAbsolutePath ' )->willReturn ('' );
197
219
$ this ->abstractAdapter ->expects ($ this ->any ())
@@ -216,6 +238,8 @@ public function testExecuteInvalidFileType()
216
238
);
217
239
$ readInterface = $ this ->createMock (ReadInterface::class);
218
240
$ writeInterface = $ this ->createMock (WriteInterface::class);
241
+ $ writeInterface ->method ('getDriver ' )
242
+ ->willReturn ($ this ->fileDriverMock );
219
243
$ this ->filesystemMock ->expects ($ this ->any ())->method ('getDirectoryRead ' )->willReturn ($ readInterface );
220
244
$ readInterface ->expects ($ this ->any ())->method ('getAbsolutePath ' )->willReturn ('' );
221
245
$ this ->abstractAdapter ->expects ($ this ->never ())->method ('validateUploadFile ' );
0 commit comments