Skip to content

Commit 4a08e32

Browse files
ENGCOM-5984: #async-import/issues/207: Provide the ability to check errors in WebAPI tests in a single approach for different adapters (SOAP/REST) #24792
- Merge Pull Request #24792 from naydav/magento2:async-import#207 - Merged commits: 1. 13ca551 2. 9598e57
2 parents 3cbe632 + 9598e57 commit 4a08e32

File tree

1 file changed

+83
-7
lines changed

1 file changed

+83
-7
lines changed

dev/tests/api-functional/framework/Magento/TestFramework/TestCase/WebapiAbstract.php

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\Framework\Filesystem;
10+
use Magento\Framework\Webapi\Exception as WebapiException;
1011
use Magento\Webapi\Model\Soap\Fault;
1112
use Magento\TestFramework\Helper\Bootstrap;
1213

@@ -102,9 +103,11 @@ abstract class WebapiAbstract extends \PHPUnit\Framework\TestCase
102103

103104
/**
104105
* Initialize fixture namespaces.
106+
* //phpcs:disable
105107
*/
106108
public static function setUpBeforeClass()
107109
{
110+
//phpcs:enable
108111
parent::setUpBeforeClass();
109112
self::_setFixtureNamespace();
110113
}
@@ -113,9 +116,11 @@ public static function setUpBeforeClass()
113116
* Run garbage collector for cleaning memory
114117
*
115118
* @return void
119+
* //phpcs:disable
116120
*/
117121
public static function tearDownAfterClass()
118122
{
123+
//phpcs:enable
119124
//clear garbage in memory
120125
gc_collect_cycles();
121126

@@ -133,8 +138,7 @@ public static function tearDownAfterClass()
133138
}
134139

135140
/**
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
138142
*
139143
* @return void
140144
*/
@@ -178,6 +182,8 @@ protected function _webApiCall(
178182

179183
/**
180184
* Mark test to be executed for SOAP adapter only.
185+
*
186+
* @param ?string $message
181187
*/
182188
protected function _markTestAsSoapOnly($message = null)
183189
{
@@ -188,6 +194,8 @@ protected function _markTestAsSoapOnly($message = null)
188194

189195
/**
190196
* Mark test to be executed for REST adapter only.
197+
*
198+
* @param ?string $message
191199
*/
192200
protected function _markTestAsRestOnly($message = null)
193201
{
@@ -203,9 +211,11 @@ protected function _markTestAsRestOnly($message = null)
203211
* @param mixed $fixture
204212
* @param int $tearDown
205213
* @return void
214+
* //phpcs:disable
206215
*/
207216
public static function setFixture($key, $fixture, $tearDown = self::AUTO_TEAR_DOWN_AFTER_METHOD)
208217
{
218+
//phpcs:enable
209219
$fixturesNamespace = self::_getFixtureNamespace();
210220
if (!isset(self::$_fixtures[$fixturesNamespace])) {
211221
self::$_fixtures[$fixturesNamespace] = [];
@@ -231,9 +241,11 @@ public static function setFixture($key, $fixture, $tearDown = self::AUTO_TEAR_DO
231241
*
232242
* @param string $key
233243
* @return mixed
244+
* //phpcs:disable
234245
*/
235246
public static function getFixture($key)
236247
{
248+
//phpcs:enable
237249
$fixturesNamespace = self::_getFixtureNamespace();
238250
if (array_key_exists($key, self::$_fixtures[$fixturesNamespace])) {
239251
return self::$_fixtures[$fixturesNamespace][$key];
@@ -247,9 +259,11 @@ public static function getFixture($key)
247259
* @param \Magento\Framework\Model\AbstractModel $model
248260
* @param bool $secure
249261
* @return \Magento\TestFramework\TestCase\WebapiAbstract
262+
* //phpcs:disable
250263
*/
251264
public static function callModelDelete($model, $secure = false)
252265
{
266+
//phpcs:enable
253267
if ($model instanceof \Magento\Framework\Model\AbstractModel && $model->getId()) {
254268
if ($secure) {
255269
self::_enableSecureArea();
@@ -300,9 +314,11 @@ protected function _getWebApiAdapter($webApiAdapterCode)
300314
* Set fixtures namespace
301315
*
302316
* @throws \RuntimeException
317+
* //phpcs:disable
303318
*/
304319
protected static function _setFixtureNamespace()
305320
{
321+
//phpcs:enable
306322
if (self::$_fixturesNamespace !== null) {
307323
throw new \RuntimeException('Fixture namespace is already set.');
308324
}
@@ -311,9 +327,11 @@ protected static function _setFixtureNamespace()
311327

312328
/**
313329
* Unset fixtures namespace
330+
* //phpcs:disable
314331
*/
315332
protected static function _unsetFixtureNamespace()
316333
{
334+
//phpcs:enable
317335
$fixturesNamespace = self::_getFixtureNamespace();
318336
unset(self::$_fixtures[$fixturesNamespace]);
319337
self::$_fixturesNamespace = null;
@@ -324,9 +342,12 @@ protected static function _unsetFixtureNamespace()
324342
*
325343
* @throws \RuntimeException
326344
* @return string
345+
* //phpcs:disable
327346
*/
328347
protected static function _getFixtureNamespace()
329348
{
349+
//phpcs:enable
350+
330351
$fixtureNamespace = self::$_fixturesNamespace;
331352
if ($fixtureNamespace === null) {
332353
throw new \RuntimeException('Fixture namespace must be set.');
@@ -339,9 +360,12 @@ protected static function _getFixtureNamespace()
339360
*
340361
* @param bool $flag
341362
* @return void
363+
* //phpcs:disable
342364
*/
343365
protected static function _enableSecureArea($flag = true)
344366
{
367+
//phpcs:enable
368+
345369
/** @var $objectManager \Magento\TestFramework\ObjectManager */
346370
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
347371

@@ -388,9 +412,11 @@ protected function _assertMessagesEqual($expectedMessages, $receivedMessages)
388412
* Delete array of fixtures
389413
*
390414
* @param array $fixtures
415+
* //phpcs:disable
391416
*/
392417
protected static function _deleteFixtures($fixtures)
393418
{
419+
//phpcs:enable
394420
foreach ($fixtures as $fixture) {
395421
self::deleteFixture($fixture, true);
396422
}
@@ -402,9 +428,11 @@ protected static function _deleteFixtures($fixtures)
402428
* @param string $key
403429
* @param bool $secure
404430
* @return void
431+
* //phpcs:disable
405432
*/
406433
public static function deleteFixture($key, $secure = false)
407434
{
435+
//phpcs:enable
408436
$fixturesNamespace = self::_getFixtureNamespace();
409437
if (array_key_exists($key, self::$_fixtures[$fixturesNamespace])) {
410438
self::callModelDelete(self::$_fixtures[$fixturesNamespace][$key], $secure);
@@ -456,11 +484,11 @@ protected function _cleanAppConfigCache()
456484
/**
457485
* Update application config data
458486
*
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
464492
* @return \Magento\TestFramework\TestCase\WebapiAbstract
465493
* @throws \RuntimeException
466494
*/
@@ -520,6 +548,8 @@ protected function _restoreAppConfig()
520548
}
521549

522550
/**
551+
* Process rest exception result.
552+
*
523553
* @param \Exception $e
524554
* @return array
525555
* <pre> ex.
@@ -666,11 +696,19 @@ protected function _checkWrappedErrors($expectedWrappedErrors, $errorDetails)
666696
}
667697

668698
/**
699+
* Get actual wrapped errors.
700+
*
669701
* @param \stdClass $errorNode
670702
* @return array
671703
*/
672704
private function getActualWrappedErrors(\stdClass $errorNode)
673705
{
706+
if (!isset($errorNode->parameters)) {
707+
return [
708+
'message' => $errorNode->message,
709+
];
710+
}
711+
674712
$actualParameters = [];
675713
$parameterNode = $errorNode->parameters->parameter;
676714
if (is_array($parameterNode)) {
@@ -686,4 +724,42 @@ private function getActualWrappedErrors(\stdClass $errorNode)
686724
'params' => $actualParameters,
687725
];
688726
}
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+
}
689765
}

0 commit comments

Comments
 (0)