PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/integration/framework/Magento/TestFramework/Helper/Api.php

https://gitlab.com/axeltizon/magento-demopoweraccess
PHP | 217 lines | 120 code | 16 blank | 81 comment | 8 complexity | ba347430e7d97cae4e1144c489e9ca64 MD5 | raw file
  1. <?php
  2. /**
  3. * Helper for API integration tests.
  4. *
  5. * Copyright © 2016 Magento. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\TestFramework\Helper;
  9. class Api
  10. {
  11. /**
  12. * Previous error handler
  13. *
  14. * @var mixed
  15. */
  16. protected static $_previousHandler = null;
  17. /**
  18. * Call API method via API handler.
  19. *
  20. * @param \PHPUnit_Framework_TestCase $testCase Active test case
  21. * @param string $path
  22. * @param array $params Order of items matters as they are passed to call_user_func_array
  23. * @return mixed
  24. */
  25. public static function call(\PHPUnit_Framework_TestCase $testCase, $path, $params = [])
  26. {
  27. $soapAdapterMock = $testCase->getMock('Magento\Api\Model\Server\Adapter\Soap', ['fault']);
  28. $soapAdapterMock->expects(
  29. $testCase->any()
  30. )->method(
  31. 'fault'
  32. )->will(
  33. $testCase->returnCallback([__CLASS__, 'soapAdapterFaultCallback'])
  34. );
  35. $serverMock = $testCase->getMock('Magento\Api\Model\Server', ['getAdapter']);
  36. $serverMock->expects($testCase->any())->method('getAdapter')->will($testCase->returnValue($soapAdapterMock));
  37. $apiSessionMock = $testCase->getMock(
  38. 'Magento\Api\Model\Session',
  39. ['isAllowed', 'isLoggedIn'],
  40. [],
  41. '',
  42. false
  43. );
  44. $apiSessionMock->expects($testCase->any())->method('isAllowed')->will($testCase->returnValue(true));
  45. $apiSessionMock->expects($testCase->any())->method('isLoggedIn')->will($testCase->returnValue(true));
  46. $handlerMock = $testCase->getMock(
  47. 'Magento\Api\Model\Server\Handler\Soap',
  48. ['_getServer', '_getSession'],
  49. [],
  50. '',
  51. false
  52. );
  53. self::$_previousHandler = set_error_handler([$handlerMock, 'handlePhpError']);
  54. $handlerMock->expects($testCase->any())->method('_getServer')->will($testCase->returnValue($serverMock));
  55. $handlerMock->expects($testCase->any())->method('_getSession')->will($testCase->returnValue($apiSessionMock));
  56. array_unshift($params, 'sessionId');
  57. /** @var $objectManager \Magento\TestFramework\ObjectManager */
  58. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  59. $objectManager->get('Magento\Framework\Registry')->unregister('isSecureArea');
  60. $objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
  61. $result = call_user_func_array([$handlerMock, $path], $params);
  62. $objectManager->get('Magento\Framework\Registry')->unregister('isSecureArea');
  63. $objectManager->get('Magento\Framework\Registry')->register('isSecureArea', false);
  64. self::restoreErrorHandler();
  65. return $result;
  66. }
  67. /**
  68. * Call API method via API handler that raises SoapFault exception
  69. *
  70. * @param \PHPUnit_Framework_TestCase $testCase Active test case
  71. * @param string $path
  72. * @param array $params Order of items matters as they are passed to call_user_func_array
  73. * @param string $expectedMessage exception message
  74. * @return \SoapFault
  75. */
  76. public static function callWithException(
  77. \PHPUnit_Framework_TestCase $testCase,
  78. $path,
  79. $params = [],
  80. $expectedMessage = ''
  81. ) {
  82. try {
  83. self::call($testCase, $path, $params);
  84. self::restoreErrorHandler();
  85. $testCase->fail('Expected error exception was not raised.');
  86. } catch (\SoapFault $exception) {
  87. self::restoreErrorHandler();
  88. if ($expectedMessage) {
  89. $testCase->assertEquals($expectedMessage, $exception->getMessage());
  90. }
  91. return $exception;
  92. }
  93. }
  94. /**
  95. * Restore previously used error handler
  96. */
  97. public static function restoreErrorHandler()
  98. {
  99. set_error_handler(self::$_previousHandler);
  100. }
  101. /**
  102. * Throw SoapFault exception. Callback for 'fault' method of API.
  103. *
  104. * @param string $exceptionCode
  105. * @param string $exceptionMessage
  106. * @throws \SoapFault
  107. */
  108. public static function soapAdapterFaultCallback($exceptionCode, $exceptionMessage)
  109. {
  110. throw new \SoapFault($exceptionCode, $exceptionMessage);
  111. }
  112. /**
  113. * Convert Simple XML to array
  114. *
  115. * @param \SimpleXMLObject $xml
  116. * @param String $keyTrimmer
  117. * @return object
  118. *
  119. * In XML notation we can't have nodes with digital names in other words fallowing XML will be not valid:
  120. * &lt;24&gt;
  121. * Default category
  122. * &lt;/24&gt;
  123. *
  124. * But this one will not cause any problems:
  125. * &lt;qwe_24&gt;
  126. * Default category
  127. * &lt;/qwe_24&gt;
  128. *
  129. * So when we want to obtain an array with key 24 we will pass the correct XML from above and $keyTrimmer = 'qwe_';
  130. * As a result we will obtain an array with digital key node.
  131. *
  132. * In the other case just don't pass the $keyTrimmer.
  133. */
  134. public static function simpleXmlToArray($xml, $keyTrimmer = null)
  135. {
  136. $result = [];
  137. $isTrimmed = false;
  138. if (null !== $keyTrimmer) {
  139. $isTrimmed = true;
  140. }
  141. if (is_object($xml)) {
  142. foreach (get_object_vars($xml->children()) as $key => $node) {
  143. $arrKey = $key;
  144. if ($isTrimmed) {
  145. $arrKey = str_replace($keyTrimmer, '', $key);
  146. }
  147. if (is_numeric($arrKey)) {
  148. $arrKey = 'Obj' . $arrKey;
  149. }
  150. if (is_object($node)) {
  151. $result[$arrKey] = self::simpleXmlToArray($node, $keyTrimmer);
  152. } elseif (is_array($node)) {
  153. $result[$arrKey] = [];
  154. foreach ($node as $nodeValue) {
  155. $result[$arrKey][] = self::simpleXmlToArray($nodeValue, $keyTrimmer);
  156. }
  157. } else {
  158. $result[$arrKey] = (string)$node;
  159. }
  160. }
  161. } else {
  162. $result = (string)$xml;
  163. }
  164. return $result;
  165. }
  166. /**
  167. * Check specific fields value in some entity data.
  168. *
  169. * @param \PHPUnit_Framework_TestCase $testCase
  170. * @param array $expectedData
  171. * @param array $actualData
  172. * @param array $fieldsToCompare To be able to compare fields from loaded model with fields from API response
  173. * this parameter provides fields mapping.
  174. * Array can store model field name $entityField mapped on field name in API response.
  175. * $fieldsToCompare format is:
  176. * $fieldsToCompare = array($modelFieldName => $apiResponseFieldName);
  177. * Example:
  178. * $fieldsToCompare = array(
  179. * 'entity_id' => 'product_id',
  180. * 'sku',
  181. * 'attribute_set_id' => 'set',
  182. * 'type_id' => 'type',
  183. * 'category_ids',
  184. * );
  185. */
  186. public static function checkEntityFields(
  187. \PHPUnit_Framework_TestCase $testCase,
  188. array $expectedData,
  189. array $actualData,
  190. array $fieldsToCompare = []
  191. ) {
  192. $fieldsToCompare = !empty($fieldsToCompare) ? $fieldsToCompare : array_keys($expectedData);
  193. foreach ($fieldsToCompare as $entityField => $field) {
  194. $testCase->assertEquals(
  195. $expectedData[is_numeric($entityField) ? $field : $entityField],
  196. $actualData[$field],
  197. sprintf('"%s" filed has invalid value.', $field)
  198. );
  199. }
  200. }
  201. }