24
24
use Magento \ImportExport \Model \Report \ReportProcessorInterface ;
25
25
use PHPUnit \Framework \MockObject \MockObject ;
26
26
use PHPUnit \Framework \TestCase ;
27
+ use Magento \ImportExport \Model \Import ;
28
+ use Magento \ImportExport \Model \Import \AbstractSource ;
29
+ use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingErrorAggregatorInterface ;
27
30
28
31
/**
29
32
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -55,6 +58,11 @@ class ValidateTest extends TestCase
55
58
*/
56
59
private $ validate ;
57
60
61
+ /**
62
+ * @var Import
63
+ */
64
+ private $ importMock ;
65
+
58
66
/**
59
67
* @var Http|MockObject
60
68
*/
@@ -70,6 +78,11 @@ class ValidateTest extends TestCase
70
78
*/
71
79
private $ messageManagerMock ;
72
80
81
+ /**
82
+ * @var AbstractSourceMock|MockObject
83
+ */
84
+ private $ abstractSourceMock ;
85
+
73
86
protected function setUp (): void
74
87
{
75
88
$ objectManagerHelper = new ObjectManagerHelper ($ this );
@@ -126,12 +139,24 @@ protected function setUp(): void
126
139
->disableOriginalConstructor ()
127
140
->getMock ();
128
141
142
+ $ this ->importMock = $ this ->getMockBuilder (import::class)
143
+ ->disableOriginalConstructor ()
144
+ ->getMock ();
145
+
146
+ $ this ->abstractSourceMock = $ this ->getMockBuilder (AbstractSource::class)
147
+ ->disableOriginalConstructor ()
148
+ ->getMockForAbstractClass ();
149
+
129
150
$ this ->validate = new Validate (
130
151
$ this ->contextMock ,
131
152
$ this ->reportProcessorMock ,
132
153
$ this ->historyMock ,
133
154
$ this ->reportHelperMock
134
155
);
156
+ $ reflection = new \ReflectionClass ($ this ->validate );
157
+ $ importProperty = $ reflection ->getProperty ('import ' );
158
+ $ importProperty ->setAccessible (true );
159
+ $ importProperty ->setValue ($ this ->validate , $ this ->importMock );
135
160
}
136
161
137
162
/**
@@ -230,4 +255,86 @@ public function testFileWasNotUploaded()
230
255
231
256
$ this ->assertEquals ($ resultLayoutMock , $ this ->validate ->execute ());
232
257
}
258
+
259
+ /**
260
+ * Test execute() method
261
+ *
262
+ * Check the case in which the import file was not uploaded.
263
+ */
264
+ public function testFileVerifiedWithImport ()
265
+ {
266
+ $ data = ['key ' => 'value ' ];
267
+
268
+ $ this ->requestMock ->expects ($ this ->once ())
269
+ ->method ('getPostValue ' )
270
+ ->willReturn ($ data );
271
+
272
+ $ resultBlock = $ this ->getMockBuilder (Result::class)
273
+ ->disableOriginalConstructor ()
274
+ ->getMock ();
275
+ $ resultBlock ->expects ($ this ->once ())
276
+ ->method ('addSuccess ' )
277
+ ->with (__ ('File is valid! To start import process press "Import" button ' ));
278
+
279
+ $ layoutMock = $ this ->getMockBuilder (LayoutInterface::class)
280
+ ->getMockForAbstractClass ();
281
+ $ layoutMock ->expects ($ this ->once ())
282
+ ->method ('getBlock ' )
283
+ ->with ('import.frame.result ' )
284
+ ->willReturn ($ resultBlock );
285
+
286
+ $ resultLayoutMock = $ this ->getMockBuilder (Layout::class)
287
+ ->disableOriginalConstructor ()
288
+ ->getMock ();
289
+ $ resultLayoutMock ->expects ($ this ->once ())
290
+ ->method ('getLayout ' )
291
+ ->willReturn ($ layoutMock );
292
+ $ this ->importMock ->expects ($ this ->once ())
293
+ ->method ('setData ' )
294
+ ->with ($ data )
295
+ ->willReturn ($ this ->importMock );
296
+ $ this ->importMock ->expects ($ this ->once ())
297
+ ->method ('uploadFileAndGetSource ' )
298
+ ->willReturn ($ this ->abstractSourceMock );
299
+ $ this ->importMock ->expects ($ this ->once ())
300
+ ->method ('validateSource ' )
301
+ ->with ($ this ->abstractSourceMock )
302
+ ->willReturn (true );
303
+
304
+ $ resultBlock ->expects ($ this ->once ())
305
+ ->method ('addAction ' )
306
+ ->willReturn (
307
+ ['show ' , 'import_validation_container ' ],
308
+ ['value ' , Import::FIELD_IMPORT_IDS , [1 , 2 , 3 ]]
309
+ );
310
+ $ this ->importMock ->expects ($ this ->exactly (3 ))
311
+ ->method ('getProcessedRowsCount ' )
312
+ ->willReturn (2 );
313
+ $ this ->importMock ->expects ($ this ->once ())
314
+ ->method ('isImportAllowed ' )
315
+ ->willReturn (true );
316
+
317
+ $ this ->importMock ->expects ($ this ->once ())
318
+ ->method ('getProcessedEntitiesCount ' )
319
+ ->willReturn (10 );
320
+
321
+ $ errorAggregatorMock = $ this ->createMock (ProcessingErrorAggregatorInterface::class);
322
+ $ this ->importMock ->expects ($ this ->any ())
323
+ ->method ('getErrorAggregator ' )
324
+ ->willReturn ($ errorAggregatorMock );
325
+
326
+ $ errorAggregatorMock ->expects ($ this ->exactly (3 ))
327
+ ->method ('getErrorsCount ' )
328
+ ->willReturn (2 );
329
+
330
+ $ errorAggregatorMock ->expects ($ this ->once ())
331
+ ->method ('getAllErrors ' )
332
+ ->willReturn ($ errorAggregatorMock );
333
+
334
+ $ this ->resultFactoryMock ->expects ($ this ->any ())
335
+ ->method ('create ' )
336
+ ->with (ResultFactory::TYPE_LAYOUT )
337
+ ->willReturn ($ resultLayoutMock );
338
+ $ this ->assertEquals ($ resultLayoutMock , $ this ->validate ->execute ());
339
+ }
233
340
}
0 commit comments