7
7
8
8
use Magento \Framework \App \Filesystem \DirectoryList ;
9
9
use Magento \Framework \Filesystem ;
10
+ use Magento \Framework \Webapi \Exception as WebapiException ;
10
11
use Magento \Webapi \Model \Soap \Fault ;
11
12
use Magento \TestFramework \Helper \Bootstrap ;
12
13
@@ -102,9 +103,11 @@ abstract class WebapiAbstract extends \PHPUnit\Framework\TestCase
102
103
103
104
/**
104
105
* Initialize fixture namespaces.
106
+ * //phpcs:disable
105
107
*/
106
108
public static function setUpBeforeClass ()
107
109
{
110
+ //phpcs:enable
108
111
parent ::setUpBeforeClass ();
109
112
self ::_setFixtureNamespace ();
110
113
}
@@ -113,9 +116,11 @@ public static function setUpBeforeClass()
113
116
* Run garbage collector for cleaning memory
114
117
*
115
118
* @return void
119
+ * //phpcs:disable
116
120
*/
117
121
public static function tearDownAfterClass ()
118
122
{
123
+ //phpcs:enable
119
124
//clear garbage in memory
120
125
gc_collect_cycles ();
121
126
@@ -133,8 +138,7 @@ public static function tearDownAfterClass()
133
138
}
134
139
135
140
/**
136
- * Call safe delete for models which added to delete list
137
- * Restore config values changed during the test
141
+ * Call safe delete for models which added to delete list, Restore config values changed during the test
138
142
*
139
143
* @return void
140
144
*/
@@ -178,6 +182,8 @@ protected function _webApiCall(
178
182
179
183
/**
180
184
* Mark test to be executed for SOAP adapter only.
185
+ *
186
+ * @param ?string $message
181
187
*/
182
188
protected function _markTestAsSoapOnly ($ message = null )
183
189
{
@@ -188,6 +194,8 @@ protected function _markTestAsSoapOnly($message = null)
188
194
189
195
/**
190
196
* Mark test to be executed for REST adapter only.
197
+ *
198
+ * @param ?string $message
191
199
*/
192
200
protected function _markTestAsRestOnly ($ message = null )
193
201
{
@@ -203,9 +211,11 @@ protected function _markTestAsRestOnly($message = null)
203
211
* @param mixed $fixture
204
212
* @param int $tearDown
205
213
* @return void
214
+ * //phpcs:disable
206
215
*/
207
216
public static function setFixture ($ key , $ fixture , $ tearDown = self ::AUTO_TEAR_DOWN_AFTER_METHOD )
208
217
{
218
+ //phpcs:enable
209
219
$ fixturesNamespace = self ::_getFixtureNamespace ();
210
220
if (!isset (self ::$ _fixtures [$ fixturesNamespace ])) {
211
221
self ::$ _fixtures [$ fixturesNamespace ] = [];
@@ -231,9 +241,11 @@ public static function setFixture($key, $fixture, $tearDown = self::AUTO_TEAR_DO
231
241
*
232
242
* @param string $key
233
243
* @return mixed
244
+ * //phpcs:disable
234
245
*/
235
246
public static function getFixture ($ key )
236
247
{
248
+ //phpcs:enable
237
249
$ fixturesNamespace = self ::_getFixtureNamespace ();
238
250
if (array_key_exists ($ key , self ::$ _fixtures [$ fixturesNamespace ])) {
239
251
return self ::$ _fixtures [$ fixturesNamespace ][$ key ];
@@ -247,9 +259,11 @@ public static function getFixture($key)
247
259
* @param \Magento\Framework\Model\AbstractModel $model
248
260
* @param bool $secure
249
261
* @return \Magento\TestFramework\TestCase\WebapiAbstract
262
+ * //phpcs:disable
250
263
*/
251
264
public static function callModelDelete ($ model , $ secure = false )
252
265
{
266
+ //phpcs:enable
253
267
if ($ model instanceof \Magento \Framework \Model \AbstractModel && $ model ->getId ()) {
254
268
if ($ secure ) {
255
269
self ::_enableSecureArea ();
@@ -300,9 +314,11 @@ protected function _getWebApiAdapter($webApiAdapterCode)
300
314
* Set fixtures namespace
301
315
*
302
316
* @throws \RuntimeException
317
+ * //phpcs:disable
303
318
*/
304
319
protected static function _setFixtureNamespace ()
305
320
{
321
+ //phpcs:enable
306
322
if (self ::$ _fixturesNamespace !== null ) {
307
323
throw new \RuntimeException ('Fixture namespace is already set. ' );
308
324
}
@@ -311,9 +327,11 @@ protected static function _setFixtureNamespace()
311
327
312
328
/**
313
329
* Unset fixtures namespace
330
+ * //phpcs:disable
314
331
*/
315
332
protected static function _unsetFixtureNamespace ()
316
333
{
334
+ //phpcs:enable
317
335
$ fixturesNamespace = self ::_getFixtureNamespace ();
318
336
unset(self ::$ _fixtures [$ fixturesNamespace ]);
319
337
self ::$ _fixturesNamespace = null ;
@@ -324,9 +342,12 @@ protected static function _unsetFixtureNamespace()
324
342
*
325
343
* @throws \RuntimeException
326
344
* @return string
345
+ * //phpcs:disable
327
346
*/
328
347
protected static function _getFixtureNamespace ()
329
348
{
349
+ //phpcs:enable
350
+
330
351
$ fixtureNamespace = self ::$ _fixturesNamespace ;
331
352
if ($ fixtureNamespace === null ) {
332
353
throw new \RuntimeException ('Fixture namespace must be set. ' );
@@ -339,9 +360,12 @@ protected static function _getFixtureNamespace()
339
360
*
340
361
* @param bool $flag
341
362
* @return void
363
+ * //phpcs:disable
342
364
*/
343
365
protected static function _enableSecureArea ($ flag = true )
344
366
{
367
+ //phpcs:enable
368
+
345
369
/** @var $objectManager \Magento\TestFramework\ObjectManager */
346
370
$ objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
347
371
@@ -388,9 +412,11 @@ protected function _assertMessagesEqual($expectedMessages, $receivedMessages)
388
412
* Delete array of fixtures
389
413
*
390
414
* @param array $fixtures
415
+ * //phpcs:disable
391
416
*/
392
417
protected static function _deleteFixtures ($ fixtures )
393
418
{
419
+ //phpcs:enable
394
420
foreach ($ fixtures as $ fixture ) {
395
421
self ::deleteFixture ($ fixture , true );
396
422
}
@@ -402,9 +428,11 @@ protected static function _deleteFixtures($fixtures)
402
428
* @param string $key
403
429
* @param bool $secure
404
430
* @return void
431
+ * //phpcs:disable
405
432
*/
406
433
public static function deleteFixture ($ key , $ secure = false )
407
434
{
435
+ //phpcs:enable
408
436
$ fixturesNamespace = self ::_getFixtureNamespace ();
409
437
if (array_key_exists ($ key , self ::$ _fixtures [$ fixturesNamespace ])) {
410
438
self ::callModelDelete (self ::$ _fixtures [$ fixturesNamespace ][$ key ], $ secure );
@@ -456,11 +484,11 @@ protected function _cleanAppConfigCache()
456
484
/**
457
485
* Update application config data
458
486
*
459
- * @param string $path Config path with the form "section/group/node"
460
- * @param string|int|null $value Value of config item
461
- * @param bool $cleanAppCache If TRUE application cache will be refreshed
462
- * @param bool $updateLocalConfig If TRUE local config object will be updated too
463
- * @param bool $restore If TRUE config value will be restored after test run
487
+ * @param string $path Config path with the form "section/group/node"
488
+ * @param string|int|null $value Value of config item
489
+ * @param bool $cleanAppCache If TRUE application cache will be refreshed
490
+ * @param bool $updateLocalConfig If TRUE local config object will be updated too
491
+ * @param bool $restore If TRUE config value will be restored after test run
464
492
* @return \Magento\TestFramework\TestCase\WebapiAbstract
465
493
* @throws \RuntimeException
466
494
*/
@@ -520,6 +548,8 @@ protected function _restoreAppConfig()
520
548
}
521
549
522
550
/**
551
+ * Process rest exception result.
552
+ *
523
553
* @param \Exception $e
524
554
* @return array
525
555
* <pre> ex.
@@ -666,11 +696,19 @@ protected function _checkWrappedErrors($expectedWrappedErrors, $errorDetails)
666
696
}
667
697
668
698
/**
699
+ * Get actual wrapped errors.
700
+ *
669
701
* @param \stdClass $errorNode
670
702
* @return array
671
703
*/
672
704
private function getActualWrappedErrors (\stdClass $ errorNode )
673
705
{
706
+ if (!isset ($ errorNode ->parameters )) {
707
+ return [
708
+ 'message ' => $ errorNode ->message ,
709
+ ];
710
+ }
711
+
674
712
$ actualParameters = [];
675
713
$ parameterNode = $ errorNode ->parameters ->parameter ;
676
714
if (is_array ($ parameterNode )) {
@@ -686,4 +724,42 @@ private function getActualWrappedErrors(\stdClass $errorNode)
686
724
'params ' => $ actualParameters ,
687
725
];
688
726
}
727
+
728
+ /**
729
+ * Assert webapi errors.
730
+ *
731
+ * @param array $serviceInfo
732
+ * @param array $data
733
+ * @param array $expectedErrorData
734
+ * @return void
735
+ * @throws \Exception
736
+ */
737
+ protected function assertWebApiCallErrors (array $ serviceInfo , array $ data , array $ expectedErrorData )
738
+ {
739
+ try {
740
+ $ this ->_webApiCall ($ serviceInfo , $ data );
741
+ $ this ->fail ('Expected throwing exception ' );
742
+ } catch (\Exception $ e ) {
743
+ if (TESTS_WEB_API_ADAPTER === self ::ADAPTER_REST ) {
744
+ self ::assertEquals ($ expectedErrorData , $ this ->processRestExceptionResult ($ e ));
745
+ self ::assertEquals (WebapiException::HTTP_BAD_REQUEST , $ e ->getCode ());
746
+ } elseif (TESTS_WEB_API_ADAPTER === self ::ADAPTER_SOAP ) {
747
+ $ this ->assertInstanceOf ('SoapFault ' , $ e );
748
+ $ expectedWrappedErrors = [];
749
+ foreach ($ expectedErrorData ['errors ' ] as $ error ) {
750
+ // @see \Magento\TestFramework\TestCase\WebapiAbstract::getActualWrappedErrors()
751
+ $ expectedWrappedError = [
752
+ 'message ' => $ error ['message ' ],
753
+ ];
754
+ if (isset ($ error ['parameters ' ])) {
755
+ $ expectedWrappedError ['params ' ] = $ error ['parameters ' ];
756
+ }
757
+ $ expectedWrappedErrors [] = $ expectedWrappedError ;
758
+ }
759
+ $ this ->checkSoapFault ($ e , $ expectedErrorData ['message ' ], 'env:Sender ' , [], $ expectedWrappedErrors );
760
+ } else {
761
+ throw $ e ;
762
+ }
763
+ }
764
+ }
689
765
}
0 commit comments