CarrierLinkManagementTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryApi\Test\Api\SourceRepository;
  8. use Magento\Framework\Webapi\Rest\Request;
  9. use Magento\InventoryApi\Api\Data\SourceCarrierLinkInterface;
  10. use Magento\InventoryApi\Api\Data\SourceInterface;
  11. use Magento\TestFramework\TestCase\WebapiAbstract;
  12. class CarrierLinkManagementTest extends WebapiAbstract
  13. {
  14. /**#@+
  15. * Service constants
  16. */
  17. const RESOURCE_PATH = '/V1/inventory/sources';
  18. const SERVICE_NAME = 'inventoryApiSourceRepositoryV1';
  19. /**#@-*/
  20. /**
  21. * @param array $carrierLinks
  22. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php
  23. * @dataProvider dataProviderCarrierLinks
  24. */
  25. public function testCarrierLinksManagement(array $carrierLinks)
  26. {
  27. $this->markTestSkipped('Binding carriers to individual sources is not implemented in MSI MVP');
  28. $sourceCode = 'source-code-1';
  29. $expectedData = [
  30. SourceInterface::NAME => 'source-name-1',
  31. SourceInterface::POSTCODE => 'source-postcode',
  32. SourceInterface::COUNTRY_ID => 'US',
  33. SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 0,
  34. SourceInterface::CARRIER_LINKS => $carrierLinks,
  35. ];
  36. $this->saveSource($sourceCode, $expectedData);
  37. $sourceData = $this->getSourceDataByCode($sourceCode);
  38. self::assertArrayHasKey(SourceInterface::USE_DEFAULT_CARRIER_CONFIG, $sourceData);
  39. self::assertEquals(
  40. $expectedData[SourceInterface::USE_DEFAULT_CARRIER_CONFIG],
  41. $sourceData[SourceInterface::USE_DEFAULT_CARRIER_CONFIG]
  42. );
  43. self::assertArrayHasKey(SourceInterface::CARRIER_LINKS, $sourceData);
  44. self::assertEquals($expectedData[SourceInterface::CARRIER_LINKS], $sourceData[SourceInterface::CARRIER_LINKS]);
  45. }
  46. /**
  47. * @return array
  48. */
  49. public function dataProviderCarrierLinks(): array
  50. {
  51. return [
  52. 'add_carrier_new_links' => [
  53. [
  54. [
  55. SourceCarrierLinkInterface::CARRIER_CODE => 'ups',
  56. SourceCarrierLinkInterface::POSITION => 100,
  57. ],
  58. [
  59. SourceCarrierLinkInterface::CARRIER_CODE => 'usps',
  60. SourceCarrierLinkInterface::POSITION => 200,
  61. ],
  62. [
  63. SourceCarrierLinkInterface::CARRIER_CODE => 'dhl',
  64. SourceCarrierLinkInterface::POSITION => 300,
  65. ],
  66. [
  67. SourceCarrierLinkInterface::CARRIER_CODE => 'fedex',
  68. SourceCarrierLinkInterface::POSITION => 400,
  69. ],
  70. ],
  71. ],
  72. 'replace_carrier_links' => [
  73. [
  74. [
  75. SourceCarrierLinkInterface::CARRIER_CODE => 'dhl',
  76. SourceCarrierLinkInterface::POSITION => 100,
  77. ],
  78. [
  79. SourceCarrierLinkInterface::CARRIER_CODE => 'fedex',
  80. SourceCarrierLinkInterface::POSITION => 200,
  81. ],
  82. ],
  83. ],
  84. 'delete_carrier_links' => [
  85. [],
  86. ],
  87. ];
  88. }
  89. /**
  90. * @param string $sourceCode
  91. * @param array $data
  92. * @return void
  93. */
  94. private function saveSource(string $sourceCode, array $data)
  95. {
  96. $serviceInfo = [
  97. 'rest' => [
  98. 'resourcePath' => self::RESOURCE_PATH . '/' . $sourceCode,
  99. 'httpMethod' => Request::HTTP_METHOD_PUT,
  100. ],
  101. 'soap' => [
  102. 'service' => self::SERVICE_NAME,
  103. 'operation' => self::SERVICE_NAME . 'Save',
  104. ],
  105. ];
  106. if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
  107. $this->_webApiCall($serviceInfo, ['source' => $data]);
  108. } else {
  109. $requestData = $data;
  110. $requestData['sourceCode'] = $sourceCode;
  111. $this->_webApiCall($serviceInfo, ['source' => $requestData]);
  112. }
  113. }
  114. /**
  115. * @param string $sourceCode
  116. * @return array
  117. */
  118. private function getSourceDataByCode(string $sourceCode): array
  119. {
  120. $serviceInfo = [
  121. 'rest' => [
  122. 'resourcePath' => self::RESOURCE_PATH . '/' . $sourceCode,
  123. 'httpMethod' => Request::HTTP_METHOD_GET,
  124. ],
  125. 'soap' => [
  126. 'service' => self::SERVICE_NAME,
  127. 'operation' => self::SERVICE_NAME . 'Get',
  128. ],
  129. ];
  130. $response = (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
  131. ? $this->_webApiCall($serviceInfo)
  132. : $this->_webApiCall($serviceInfo, ['sourceCode' => $sourceCode]);
  133. self::assertArrayHasKey(SourceInterface::SOURCE_CODE, $response);
  134. return $response;
  135. }
  136. /**
  137. * @param array $carrierData
  138. * @param array $expectedErrorData
  139. */
  140. public function testCarrierLinksValidationUseGlobalConfiguration()
  141. {
  142. $carrierData = [
  143. SourceInterface::SOURCE_CODE => 'source-code-1',
  144. SourceInterface::NAME => 'source-name-1',
  145. SourceInterface::POSTCODE => 'source-postcode',
  146. SourceInterface::COUNTRY_ID => 'US',
  147. SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 1,
  148. SourceInterface::CARRIER_LINKS => [
  149. [
  150. SourceCarrierLinkInterface::CARRIER_CODE => 'ups',
  151. SourceCarrierLinkInterface::POSITION => 100,
  152. ],
  153. [
  154. SourceCarrierLinkInterface::CARRIER_CODE => 'usps',
  155. SourceCarrierLinkInterface::POSITION => 200,
  156. ],
  157. ],
  158. ];
  159. $expectedErrorData = [
  160. 'message' => 'Validation Failed',
  161. 'errors' => [
  162. [
  163. 'message' =>
  164. 'You can\'t configure "%field" because you have chosen Global Shipping configuration.',
  165. 'parameters' => [
  166. 'field' => SourceInterface::CARRIER_LINKS,
  167. ],
  168. ],
  169. ],
  170. ];
  171. $this->validate($carrierData, $expectedErrorData);
  172. }
  173. /**
  174. * @param array $carrierData
  175. * @param array $expectedErrorData
  176. */
  177. public function testCarrierLinksValidationWithNonExistedCarrierCode()
  178. {
  179. $this->markTestSkipped('Binding carriers to individual sources is not implemented in MSI MVP');
  180. $carrierData = [
  181. SourceInterface::SOURCE_CODE => 'source-code-1',
  182. SourceInterface::NAME => 'source-name-1',
  183. SourceInterface::POSTCODE => 'source-postcode',
  184. SourceInterface::COUNTRY_ID => 'US',
  185. SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 0,
  186. SourceInterface::CARRIER_LINKS => [
  187. [
  188. SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_1',
  189. SourceCarrierLinkInterface::POSITION => 100,
  190. ],
  191. [
  192. SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_2',
  193. SourceCarrierLinkInterface::POSITION => 200,
  194. ],
  195. ],
  196. ];
  197. $expectedErrorData = [
  198. 'message' => 'Validation Failed',
  199. 'errors' => [
  200. [
  201. 'message' => 'Carrier with code: "%carrier" don\'t exists.',
  202. 'parameters' => [
  203. 'carrier' => 'no_exists_1',
  204. ],
  205. ],
  206. [
  207. 'message' => 'Carrier with code: "%carrier" don\'t exists.',
  208. 'parameters' => [
  209. 'carrier' => 'no_exists_2',
  210. ],
  211. ],
  212. ],
  213. ];
  214. $this->validate($carrierData, $expectedErrorData);
  215. }
  216. /**
  217. * @param array $carrierData
  218. * @param array $expectedErrorData
  219. * @return void
  220. */
  221. private function validate(array $carrierData, array $expectedErrorData): void
  222. {
  223. $serviceInfo = [
  224. 'rest' => [
  225. 'resourcePath' => self::RESOURCE_PATH,
  226. 'httpMethod' => Request::HTTP_METHOD_POST,
  227. ],
  228. 'soap' => [
  229. 'service' => self::SERVICE_NAME,
  230. 'operation' => self::SERVICE_NAME . 'Save',
  231. ],
  232. ];
  233. try {
  234. $this->_webApiCall($serviceInfo, ['source' => $carrierData]);
  235. $this->fail('Expected throwing exception');
  236. } catch (\Exception $e) {
  237. if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
  238. self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
  239. self::assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
  240. } elseif (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
  241. $this->assertInstanceOf('SoapFault', $e);
  242. $expectedWrappedErrors = [];
  243. foreach ($expectedErrorData['errors'] as $error) {
  244. // @see \Magento\TestFramework\TestCase\WebapiAbstract::getActualWrappedErrors()
  245. $expectedWrappedErrors[] = [
  246. 'message' => $error['message'],
  247. 'params' => $error['parameters'],
  248. ];
  249. }
  250. $this->checkSoapFault(
  251. $e,
  252. $expectedErrorData['message'],
  253. 'env:Sender',
  254. [],
  255. $expectedWrappedErrors
  256. );
  257. } else {
  258. throw $e;
  259. }
  260. }
  261. }
  262. }