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
<?php
/**
* Helper for API integration tests.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\TestFramework\Helper;
class Api
{
/**
* Previous error handler
*
* @var mixed
*/
protected static $_previousHandler = null;
/**
* Call API method via API handler.
*
* @param \PHPUnit\Framework\TestCase $testCase Active test case
* @param string $path
* @param array $params Order of items matters as they are passed to call_user_func_array
* @return mixed
*/
public static function call(\PHPUnit\Framework\TestCase $testCase, $path, $params = [])
{
$soapAdapterMock = $testCase->getMock(\stdClass::class, ['fault']);
$soapAdapterMock->expects(
$testCase->any()
)->method(
'fault'
)->will(
$testCase->returnCallback([__CLASS__, 'soapAdapterFaultCallback'])
);
$serverMock = $testCase->getMock(\stdClass::class, ['getAdapter']);
$serverMock->expects($testCase->any())->method('getAdapter')->will($testCase->returnValue($soapAdapterMock));
$apiSessionMock = $testCase->createPartialMock(\stdClass::class, ['isAllowed', 'isLoggedIn']);
$apiSessionMock->expects($testCase->any())->method('isAllowed')->will($testCase->returnValue(true));
$apiSessionMock->expects($testCase->any())->method('isLoggedIn')->will($testCase->returnValue(true));
$handlerMock = $testCase->createPartialMock(\stdClass::class, ['_getServer', '_getSession']);
self::$_previousHandler = set_error_handler([$handlerMock, 'handlePhpError']);
$handlerMock->expects($testCase->any())->method('_getServer')->will($testCase->returnValue($serverMock));
$handlerMock->expects($testCase->any())->method('_getSession')->will($testCase->returnValue($apiSessionMock));
array_unshift($params, 'sessionId');
/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$objectManager->get(\Magento\Framework\Registry::class)->unregister('isSecureArea');
$objectManager->get(\Magento\Framework\Registry::class)->register('isSecureArea', true);
$result = call_user_func_array([$handlerMock, $path], $params);
$objectManager->get(\Magento\Framework\Registry::class)->unregister('isSecureArea');
$objectManager->get(\Magento\Framework\Registry::class)->register('isSecureArea', false);
self::restoreErrorHandler();
return $result;
}
/**
* Call API method via API handler that raises SoapFault exception
*
* @param \PHPUnit\Framework\TestCase $testCase Active test case
* @param string $path
* @param array $params Order of items matters as they are passed to call_user_func_array
* @param string $expectedMessage exception message
* @return \SoapFault
*/
public static function callWithException(
\PHPUnit\Framework\TestCase $testCase,
$path,
$params = [],
$expectedMessage = ''
) {
try {
self::call($testCase, $path, $params);
self::restoreErrorHandler();
$testCase->fail('Expected error exception was not raised.');
} catch (\SoapFault $exception) {
self::restoreErrorHandler();
if ($expectedMessage) {
$testCase->assertEquals($expectedMessage, $exception->getMessage());
}
return $exception;
}
}
/**
* Restore previously used error handler
*/
public static function restoreErrorHandler()
{
set_error_handler(self::$_previousHandler);
}
/**
* Throw SoapFault exception. Callback for 'fault' method of API.
*
* @param string $exceptionCode
* @param string $exceptionMessage
* @throws \SoapFault
*/
public static function soapAdapterFaultCallback($exceptionCode, $exceptionMessage)
{
throw new \SoapFault($exceptionCode, $exceptionMessage);
}
/**
* Convert Simple XML to array
*
* @param \SimpleXMLObject $xml
* @param String $keyTrimmer
* @return object
*
* In XML notation we can't have nodes with digital names in other words fallowing XML will be not valid:
* <24>
* Default category
* </24>
*
* But this one will not cause any problems:
* <qwe_24>
* Default category
* </qwe_24>
*
* So when we want to obtain an array with key 24 we will pass the correct XML from above and $keyTrimmer = 'qwe_';
* As a result we will obtain an array with digital key node.
*
* In the other case just don't pass the $keyTrimmer.
*/
public static function simpleXmlToArray($xml, $keyTrimmer = null)
{
$result = [];
$isTrimmed = false;
if (null !== $keyTrimmer) {
$isTrimmed = true;
}
if (is_object($xml)) {
foreach (get_object_vars($xml->children()) as $key => $node) {
$arrKey = $key;
if ($isTrimmed) {
$arrKey = str_replace($keyTrimmer, '', $key);
}
if (is_numeric($arrKey)) {
$arrKey = 'Obj' . $arrKey;
}
if (is_object($node)) {
$result[$arrKey] = self::simpleXmlToArray($node, $keyTrimmer);
} elseif (is_array($node)) {
$result[$arrKey] = [];
foreach ($node as $nodeValue) {
$result[$arrKey][] = self::simpleXmlToArray($nodeValue, $keyTrimmer);
}
} else {
$result[$arrKey] = (string)$node;
}
}
} else {
$result = (string)$xml;
}
return $result;
}
/**
* Check specific fields value in some entity data.
*
* @param \PHPUnit\Framework\TestCase $testCase
* @param array $expectedData
* @param array $actualData
* @param array $fieldsToCompare To be able to compare fields from loaded model with fields from API response
* this parameter provides fields mapping.
* Array can store model field name $entityField mapped on field name in API response.
* $fieldsToCompare format is:
* $fieldsToCompare = array($modelFieldName => $apiResponseFieldName);
* Example:
* $fieldsToCompare = array(
* 'entity_id' => 'product_id',
* 'sku',
* 'attribute_set_id' => 'set',
* 'type_id' => 'type',
* 'category_ids',
* );
*/
public static function checkEntityFields(
\PHPUnit\Framework\TestCase $testCase,
array $expectedData,
array $actualData,
array $fieldsToCompare = []
) {
$fieldsToCompare = !empty($fieldsToCompare) ? $fieldsToCompare : array_keys($expectedData);
foreach ($fieldsToCompare as $entityField => $field) {
$testCase->assertEquals(
$expectedData[is_numeric($entityField) ? $field : $entityField],
$actualData[$field],
sprintf('"%s" filed has invalid value.', $field)
);
}
}
}