forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebapiAbstract.php
670 lines (616 loc) · 22 KB
/
WebapiAbstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<?php
/**
* Generic test case for Web API functional tests.
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\TestCase;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Webapi\Model\Soap\Fault;
/**
* @SuppressWarnings(PHPMD.NumberOfChildren)
*/
abstract class WebapiAbstract extends \PHPUnit_Framework_TestCase
{
/** TODO: Reconsider implementation of fixture-management methods after implementing several tests */
/**#@+
* Auto tear down options in setFixture
*/
const AUTO_TEAR_DOWN_DISABLED = 0;
const AUTO_TEAR_DOWN_AFTER_METHOD = 1;
const AUTO_TEAR_DOWN_AFTER_CLASS = 2;
/**#@-*/
/**#@+
* Web API adapters that are used to perform actual calls.
*/
const ADAPTER_SOAP = 'soap';
const ADAPTER_REST = 'rest';
/**#@-*/
/**
* Application cache model.
*
* @var \Magento\Framework\App\Cache
*/
protected $_appCache;
/**
* The list of models to be deleted automatically in tearDown().
*
* @var array
*/
protected $_modelsToDelete = [];
/**
* Namespace for fixtures is different for each test case.
*
* @var string
*/
protected static $_fixturesNamespace;
/**
* The list of registered fixtures.
*
* @var array
*/
protected static $_fixtures = [];
/**
* Fixtures to be deleted in tearDown().
*
* @var array
*/
protected static $_methodLevelFixtures = [];
/**
* Fixtures to be deleted in tearDownAfterClass().
*
* @var array
*/
protected static $_classLevelFixtures = [];
/**
* Original Magento config values.
*
* @var array
*/
protected $_origConfigValues = [];
/**
* The list of instantiated Web API adapters.
*
* @var \Magento\TestFramework\TestCase\Webapi\AdapterInterface[]
*/
protected $_webApiAdapters;
/**
* The list of available Web API adapters.
*
* @var array
*/
protected $_webApiAdaptersMap = [
self::ADAPTER_SOAP => 'Magento\TestFramework\TestCase\Webapi\Adapter\Soap',
self::ADAPTER_REST => 'Magento\TestFramework\TestCase\Webapi\Adapter\Rest',
];
/**
* Initialize fixture namespaces.
*/
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
self::_setFixtureNamespace();
}
/**
* Run garbage collector for cleaning memory
*
* @return void
*/
public static function tearDownAfterClass()
{
//clear garbage in memory
if (version_compare(PHP_VERSION, '5.3', '>=')) {
gc_collect_cycles();
}
$fixtureNamespace = self::_getFixtureNamespace();
if (isset(self::$_classLevelFixtures[$fixtureNamespace])
&& count(self::$_classLevelFixtures[$fixtureNamespace])
) {
self::_deleteFixtures(self::$_classLevelFixtures[$fixtureNamespace]);
}
//ever disable secure area on class down
self::_enableSecureArea(false);
self::_unsetFixtureNamespace();
parent::tearDownAfterClass();
}
/**
* Call safe delete for models which added to delete list
* Restore config values changed during the test
*
* @return void
*/
protected function tearDown()
{
$fixtureNamespace = self::_getFixtureNamespace();
if (isset(self::$_methodLevelFixtures[$fixtureNamespace])
&& count(self::$_methodLevelFixtures[$fixtureNamespace])
) {
self::_deleteFixtures(self::$_methodLevelFixtures[$fixtureNamespace]);
}
$this->_callModelsDelete();
$this->_restoreAppConfig();
parent::tearDown();
}
/**
* Perform Web API call to the system under test.
*
* @see \Magento\TestFramework\TestCase\Webapi\AdapterInterface::call()
* @param array $serviceInfo
* @param array $arguments
* @param string|null $webApiAdapterCode
* @param string|null $storeCode
* @return array|int|string|float|bool Web API call results
*/
protected function _webApiCall($serviceInfo, $arguments = [], $webApiAdapterCode = null, $storeCode = null)
{
if (is_null($webApiAdapterCode)) {
/** Default adapter code is defined in PHPUnit configuration */
$webApiAdapterCode = strtolower(TESTS_WEB_API_ADAPTER);
}
return $this->_getWebApiAdapter($webApiAdapterCode)->call($serviceInfo, $arguments, $storeCode);
}
/**
* Mark test to be executed for SOAP adapter only.
*/
protected function _markTestAsSoapOnly($message = null)
{
if (TESTS_WEB_API_ADAPTER != self::ADAPTER_SOAP) {
$this->markTestSkipped($message ? $message : "The test is intended to be executed for SOAP adapter only.");
}
}
/**
* Mark test to be executed for REST adapter only.
*/
protected function _markTestAsRestOnly($message = null)
{
if (TESTS_WEB_API_ADAPTER != self::ADAPTER_REST) {
$this->markTestSkipped($message ? $message : "The test is intended to be executed for REST adapter only.");
}
}
/**
* Set fixture to registry
*
* @param string $key
* @param mixed $fixture
* @param int $tearDown
* @return void
*/
public static function setFixture($key, $fixture, $tearDown = self::AUTO_TEAR_DOWN_AFTER_METHOD)
{
$fixturesNamespace = self::_getFixtureNamespace();
if (!isset(self::$_fixtures[$fixturesNamespace])) {
self::$_fixtures[$fixturesNamespace] = [];
}
self::$_fixtures[$fixturesNamespace][$key] = $fixture;
if ($tearDown == self::AUTO_TEAR_DOWN_AFTER_METHOD) {
if (!isset(self::$_methodLevelFixtures[$fixturesNamespace])) {
self::$_methodLevelFixtures[$fixturesNamespace] = [];
}
self::$_methodLevelFixtures[$fixturesNamespace][] = $key;
} else {
if ($tearDown == self::AUTO_TEAR_DOWN_AFTER_CLASS) {
if (!isset(self::$_classLevelFixtures[$fixturesNamespace])) {
self::$_classLevelFixtures[$fixturesNamespace] = [];
}
self::$_classLevelFixtures[$fixturesNamespace][] = $key;
}
}
}
/**
* Get fixture by key
*
* @param string $key
* @return mixed
*/
public static function getFixture($key)
{
$fixturesNamespace = self::_getFixtureNamespace();
if (array_key_exists($key, self::$_fixtures[$fixturesNamespace])) {
return self::$_fixtures[$fixturesNamespace][$key];
}
return null;
}
/**
* Call safe delete for model
*
* @param \Magento\Framework\Model\AbstractModel $model
* @param bool $secure
* @return \Magento\TestFramework\TestCase\WebapiAbstract
*/
public static function callModelDelete($model, $secure = false)
{
if ($model instanceof \Magento\Framework\Model\AbstractModel && $model->getId()) {
if ($secure) {
self::_enableSecureArea();
}
$model->delete();
if ($secure) {
self::_enableSecureArea(false);
}
}
}
/**
* Call safe delete for model
*
* @param \Magento\Framework\Model\AbstractModel $model
* @param bool $secure
* @return \Magento\TestFramework\TestCase\WebapiAbstract
*/
public function addModelToDelete($model, $secure = false)
{
$this->_modelsToDelete[] = ['model' => $model, 'secure' => $secure];
return $this;
}
/**
* Get Web API adapter (create if requested one does not exist).
*
* @param string $webApiAdapterCode
* @return \Magento\TestFramework\TestCase\Webapi\AdapterInterface
* @throws \LogicException When requested Web API adapter is not declared
*/
protected function _getWebApiAdapter($webApiAdapterCode)
{
if (!isset($this->_webApiAdapters[$webApiAdapterCode])) {
if (!isset($this->_webApiAdaptersMap[$webApiAdapterCode])) {
throw new \LogicException(
sprintf('Declaration of the requested Web API adapter "%s" was not found.', $webApiAdapterCode)
);
}
$this->_webApiAdapters[$webApiAdapterCode] = new $this->_webApiAdaptersMap[$webApiAdapterCode]();
}
return $this->_webApiAdapters[$webApiAdapterCode];
}
/**
* Set fixtures namespace
*
* @throws \RuntimeException
*/
protected static function _setFixtureNamespace()
{
if (!is_null(self::$_fixturesNamespace)) {
throw new \RuntimeException('Fixture namespace is already set.');
}
self::$_fixturesNamespace = uniqid();
}
/**
* Unset fixtures namespace
*/
protected static function _unsetFixtureNamespace()
{
$fixturesNamespace = self::_getFixtureNamespace();
unset(self::$_fixtures[$fixturesNamespace]);
self::$_fixturesNamespace = null;
}
/**
* Get fixtures namespace
*
* @throws \RuntimeException
* @return string
*/
protected static function _getFixtureNamespace()
{
$fixtureNamespace = self::$_fixturesNamespace;
if (is_null($fixtureNamespace)) {
throw new \RuntimeException('Fixture namespace must be set.');
}
return $fixtureNamespace;
}
/**
* Enable secure/admin area
*
* @param bool $flag
* @return void
*/
protected static function _enableSecureArea($flag = true)
{
/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$objectManager->get('Magento\Framework\Registry')->unregister('isSecureArea');
if ($flag) {
$objectManager->get('Magento\Framework\Registry')->register('isSecureArea', $flag);
}
}
/**
* Call delete models from list
*
* @return \Magento\TestFramework\TestCase\WebapiAbstract
*/
protected function _callModelsDelete()
{
if ($this->_modelsToDelete) {
foreach ($this->_modelsToDelete as $key => $modelData) {
/** @var $model \Magento\Framework\Model\AbstractModel */
$model = $modelData['model'];
$this->callModelDelete($model, $modelData['secure']);
unset($this->_modelsToDelete[$key]);
}
}
return $this;
}
/**
* Check if all error messages are expected ones
*
* @param array $expectedMessages
* @param array $receivedMessages
*/
protected function _assertMessagesEqual($expectedMessages, $receivedMessages)
{
foreach ($receivedMessages as $message) {
$this->assertContains($message, $expectedMessages, "Unexpected message: '{$message}'");
}
$expectedErrorsCount = count($expectedMessages);
$this->assertCount($expectedErrorsCount, $receivedMessages, 'Invalid messages quantity received');
}
/**
* Delete array of fixtures
*
* @param array $fixtures
*/
protected static function _deleteFixtures($fixtures)
{
foreach ($fixtures as $fixture) {
self::deleteFixture($fixture, true);
}
}
/**
* Delete fixture by key
*
* @param string $key
* @param bool $secure
* @return void
*/
public static function deleteFixture($key, $secure = false)
{
$fixturesNamespace = self::_getFixtureNamespace();
if (array_key_exists($key, self::$_fixtures[$fixturesNamespace])) {
self::callModelDelete(self::$_fixtures[$fixturesNamespace][$key], $secure);
unset(self::$_fixtures[$fixturesNamespace][$key]);
}
}
/** TODO: Remove methods below if not used, otherwise fix them (after having some tests implemented)*/
/**
* Get application cache model
*
* @return \Magento\Framework\App\Cache
*/
protected function _getAppCache()
{
if (null === $this->_appCache) {
//set application path
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var \Magento\Framework\App\Config\ScopeConfigInterface $config */
$config = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface');
$options = $config->getOptions();
$currentCacheDir = $options->getCacheDir();
$currentEtcDir = $options->getEtcDir();
/** @var Filesystem $filesystem */
$filesystem = $objectManager->get('Magento\Framework\Filesystem');
$options->setCacheDir($filesystem->getDirectoryRead(DirectoryList::CACHE)->getAbsolutePath());
$options->setEtcDir($filesystem->getDirectoryRead(DirectoryList::CONFIG)->getAbsolutePath());
$this->_appCache = $objectManager->get('Magento\Framework\App\Cache');
//revert paths options
$options->setCacheDir($currentCacheDir);
$options->setEtcDir($currentEtcDir);
}
return $this->_appCache;
}
/**
* Clean config cache of application
*
* @return bool
*/
protected function _cleanAppConfigCache()
{
return $this->_getAppCache()->clean(\Magento\Framework\App\Config::CACHE_TAG);
}
/**
* Update application config data
*
* @param string $path Config path with the form "section/group/node"
* @param string|int|null $value Value of config item
* @param bool $cleanAppCache If TRUE application cache will be refreshed
* @param bool $updateLocalConfig If TRUE local config object will be updated too
* @param bool $restore If TRUE config value will be restored after test run
* @return \Magento\TestFramework\TestCase\WebapiAbstract
* @throws \RuntimeException
*/
protected function _updateAppConfig(
$path,
$value,
$cleanAppCache = true,
$updateLocalConfig = false,
$restore = false
) {
list($section, $group, $node) = explode('/', $path);
if (!$section || !$group || !$node) {
throw new \RuntimeException(
sprintf('Config path must have view as "section/group/node" but now it "%s"', $path)
);
}
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var $config \Magento\Backend\Model\Config */
$config = $objectManager->create('Magento\Backend\Model\Config');
$data[$group]['fields'][$node]['value'] = $value;
$config->setSection($section)->setGroups($data)->save();
if ($restore && !isset($this->_origConfigValues[$path])) {
$this->_origConfigValues[$path] = (string)$objectManager->get(
'Magento\Framework\App\Config\ScopeConfigInterface'
)->getNode(
$path,
'default'
);
}
//refresh local cache
if ($cleanAppCache) {
if ($updateLocalConfig) {
$objectManager->get('Magento\Framework\App\Config\ReinitableConfigInterface')->reinit();
$objectManager->get('Magento\Store\Model\StoreManagerInterface')->reinitStores();
}
if (!$this->_cleanAppConfigCache()) {
throw new \RuntimeException('Application configuration cache cannot be cleaned.');
}
}
return $this;
}
/**
* Restore config values changed during tests
*/
protected function _restoreAppConfig()
{
foreach ($this->_origConfigValues as $configPath => $origValue) {
$this->_updateAppConfig($configPath, $origValue, true, true);
}
}
/**
* @param \Exception $e
* @return array
* <pre> ex.
* 'message' => "No such entity with %fieldName1 = %value1, %fieldName2 = %value2"
* 'parameters' => [
* "fieldName1" => "email",
* "value1" => "dummy@example.com",
* "fieldName2" => "websiteId",
* "value2" => 0
* ]
*
* </pre>
*/
public function processRestExceptionResult(\Exception $e)
{
$error = json_decode($e->getMessage(), true);
//Remove line breaks and replace with space
$error['message'] = trim(preg_replace('/\s+/', ' ', $error['message']));
// remove trace and type, will only be present if server is in dev mode
unset($error['trace']);
unset($error['type']);
return $error;
}
/**
* Verify that SOAP fault contains necessary information.
*
* @param \SoapFault $soapFault
* @param string $expectedMessage
* @param string $expectedFaultCode
* @param array $expectedErrorParams
* @param array $expectedWrappedErrors
* @param string $traceString
*/
protected function checkSoapFault(
$soapFault,
$expectedMessage,
$expectedFaultCode,
$expectedErrorParams = [],
$expectedWrappedErrors = [],
$traceString = null
) {
$this->assertContains($expectedMessage, $soapFault->getMessage(), "Fault message is invalid.");
$errorDetailsNode = 'GenericFault';
$errorDetails = isset($soapFault->detail->$errorDetailsNode) ? $soapFault->detail->$errorDetailsNode : null;
if (!empty($expectedErrorParams) || !empty($expectedWrappedErrors)) {
/** Check SOAP fault details */
$this->assertNotNull($errorDetails, "Details must be present.");
$this->_checkFaultParams($expectedErrorParams, $errorDetails);
$this->_checkWrappedErrors($expectedWrappedErrors, $errorDetails);
}
if ($traceString) {
/** Check error trace */
$traceNode = Fault::NODE_DETAIL_TRACE;
$mode = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\Framework\App\State')
->getMode();
if ($mode == \Magento\Framework\App\State::MODE_DEVELOPER) {
/** Developer mode changes tested behavior and it cannot properly be tested for now */
$this->assertContains(
$traceString,
$errorDetails->$traceNode,
'Trace Information is incorrect.'
);
} else {
$this->assertNull($errorDetails, "Details are not expected.");
}
}
/** Check SOAP fault code */
$this->assertNotNull($soapFault->faultcode, "Fault code must not be empty.");
$this->assertEquals($expectedFaultCode, $soapFault->faultcode, "Fault code is invalid.");
}
/**
* Check additional error parameters.
*
* @param array $expectedErrorParams
* @param \stdClass $errorDetails
*/
protected function _checkFaultParams($expectedErrorParams, $errorDetails)
{
$paramsNode = Fault::NODE_DETAIL_PARAMETERS;
if ($expectedErrorParams) {
$paramNode = Fault::NODE_DETAIL_PARAMETER;
$paramKey = Fault::NODE_DETAIL_PARAMETER_KEY;
$paramValue = Fault::NODE_DETAIL_PARAMETER_VALUE;
$actualParams = [];
if (isset($errorDetails->$paramsNode->$paramNode)) {
if (is_array($errorDetails->$paramsNode->$paramNode)) {
foreach ($errorDetails->$paramsNode->$paramNode as $param) {
$actualParams[$param->$paramKey] = $param->$paramValue;
}
} else {
$param = $errorDetails->$paramsNode->$paramNode;
$actualParams[$param->$paramKey] = $param->$paramValue;
}
}
$this->assertEquals(
$expectedErrorParams,
$actualParams,
"Parameters in fault details are invalid."
);
} else {
$this->assertFalse(isset($errorDetails->$paramsNode), "Parameters are not expected in fault details.");
}
}
/**
* Check additional wrapped errors.
*
* @param array $expectedWrappedErrors
* @param \stdClass $errorDetails
*/
protected function _checkWrappedErrors($expectedWrappedErrors, $errorDetails)
{
$wrappedErrorsNode = Fault::NODE_DETAIL_WRAPPED_ERRORS;
if ($expectedWrappedErrors) {
$wrappedErrorNode = Fault::NODE_DETAIL_WRAPPED_ERROR;
$wrappedErrorNodeFieldName = 'fieldName';
$wrappedErrorNodeValue = Fault::NODE_DETAIL_WRAPPED_ERROR_VALUE;
$actualWrappedErrors = [];
if (isset($errorDetails->$wrappedErrorsNode->$wrappedErrorNode)) {
if (is_array($errorDetails->$wrappedErrorsNode->$wrappedErrorNode)) {
foreach ($errorDetails->$wrappedErrorsNode->$wrappedErrorNode as $error) {
$actualParameters = [];
foreach ($error->parameters->parameter as $parameter) {
$actualParameters[$parameter->key] = $parameter->value;
}
$actualWrappedErrors[] = [
'message' => $error->message,
'params' => $actualParameters,
];
}
} else {
$error = $errorDetails->$wrappedErrorsNode->$wrappedErrorNode;
$actualWrappedErrors[] = [
"fieldName" => $error->$wrappedErrorNodeFieldName,
"value" => $error->$wrappedErrorNodeValue,
];
}
}
$this->assertEquals(
$expectedWrappedErrors,
$actualWrappedErrors,
"Wrapped errors in fault details are invalid."
);
} else {
$this->assertFalse(
isset($errorDetails->$wrappedErrorsNode),
"Wrapped errors are not expected in fault details."
);
}
}
}