Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CarrierLinkManagementTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
16 {
20  const RESOURCE_PATH = '/V1/inventory/sources';
21  const SERVICE_NAME = 'inventoryApiSourceRepositoryV1';
29  public function testCarrierLinksManagement(array $carrierLinks)
30  {
31  $this->markTestSkipped('Binding carriers to individual sources is not implemented in MSI MVP');
32  $sourceCode = 'source-code-1';
33  $expectedData = [
34  SourceInterface::NAME => 'source-name-1',
35  SourceInterface::POSTCODE => 'source-postcode',
38  SourceInterface::CARRIER_LINKS => $carrierLinks,
39  ];
40 
41  $this->saveSource($sourceCode, $expectedData);
42  $sourceData = $this->getSourceDataByCode($sourceCode);
43 
44  self::assertArrayHasKey(SourceInterface::USE_DEFAULT_CARRIER_CONFIG, $sourceData);
45  self::assertEquals(
48  );
49 
50  self::assertArrayHasKey(SourceInterface::CARRIER_LINKS, $sourceData);
51  self::assertEquals($expectedData[SourceInterface::CARRIER_LINKS], $sourceData[SourceInterface::CARRIER_LINKS]);
52  }
53 
57  public function dataProviderCarrierLinks(): array
58  {
59  return [
60  'add_carrier_new_links' => [
61  [
62  [
65  ],
66  [
69  ],
70  [
73  ],
74  [
77  ],
78  ],
79  ],
80  'replace_carrier_links' => [
81  [
82  [
85  ],
86  [
89  ],
90  ],
91  ],
92  'delete_carrier_links' => [
93  [],
94  ],
95  ];
96  }
97 
103  private function saveSource(string $sourceCode, array $data)
104  {
105  $serviceInfo = [
106  'rest' => [
107  'resourcePath' => self::RESOURCE_PATH . '/' . $sourceCode,
108  'httpMethod' => Request::HTTP_METHOD_PUT,
109  ],
110  'soap' => [
111  'service' => self::SERVICE_NAME,
112  'operation' => self::SERVICE_NAME . 'Save',
113  ],
114  ];
115  if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
116  $this->_webApiCall($serviceInfo, ['source' => $data]);
117  } else {
119  $requestData['sourceCode'] = $sourceCode;
120  $this->_webApiCall($serviceInfo, ['source' => $requestData]);
121  }
122  }
123 
128  private function getSourceDataByCode(string $sourceCode): array
129  {
130  $serviceInfo = [
131  'rest' => [
132  'resourcePath' => self::RESOURCE_PATH . '/' . $sourceCode,
133  'httpMethod' => Request::HTTP_METHOD_GET,
134  ],
135  'soap' => [
136  'service' => self::SERVICE_NAME,
137  'operation' => self::SERVICE_NAME . 'Get',
138  ],
139  ];
140  $response = (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
141  ? $this->_webApiCall($serviceInfo)
142  : $this->_webApiCall($serviceInfo, ['sourceCode' => $sourceCode]);
143  self::assertArrayHasKey(SourceInterface::SOURCE_CODE, $response);
144  return $response;
145  }
146 
152  {
153  $carrierData = [
154  SourceInterface::SOURCE_CODE => 'source-code-1',
155  SourceInterface::NAME => 'source-name-1',
156  SourceInterface::POSTCODE => 'source-postcode',
160  [
163  ],
164  [
167  ],
168  ],
169  ];
170  $expectedErrorData = [
171  'message' => 'Validation Failed',
172  'errors' => [
173  [
174  'message' =>
175  'You can\'t configure "%field" because you have chosen Global Shipping configuration.',
176  'parameters' => [
178  ],
179  ],
180  ],
181  ];
182 
183  $this->validate($carrierData, $expectedErrorData);
184  }
185 
191  {
192  $this->markTestSkipped('Binding carriers to individual sources is not implemented in MSI MVP');
193  $carrierData = [
194  SourceInterface::SOURCE_CODE => 'source-code-1',
195  SourceInterface::NAME => 'source-name-1',
196  SourceInterface::POSTCODE => 'source-postcode',
200  [
203  ],
204  [
207  ],
208  ],
209  ];
210  $expectedErrorData = [
211  'message' => 'Validation Failed',
212  'errors' => [
213  [
214  'message' => 'Carrier with code: "%carrier" don\'t exists.',
215  'parameters' => [
216  'carrier' => 'no_exists_1',
217  ],
218  ],
219  [
220  'message' => 'Carrier with code: "%carrier" don\'t exists.',
221  'parameters' => [
222  'carrier' => 'no_exists_2',
223  ],
224  ],
225  ],
226  ];
227 
228  $this->validate($carrierData, $expectedErrorData);
229  }
230 
236  private function validate(array $carrierData, array $expectedErrorData): void
237  {
238  $serviceInfo = [
239  'rest' => [
240  'resourcePath' => self::RESOURCE_PATH,
241  'httpMethod' => Request::HTTP_METHOD_POST,
242  ],
243  'soap' => [
244  'service' => self::SERVICE_NAME,
245  'operation' => self::SERVICE_NAME . 'Save',
246  ],
247  ];
248 
249  try {
250  $this->_webApiCall($serviceInfo, ['source' => $carrierData]);
251  $this->fail('Expected throwing exception');
252  } catch (\Exception $e) {
253  if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
254  self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
255  self::assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
256  } elseif (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
257  $this->assertInstanceOf('SoapFault', $e);
258  $expectedWrappedErrors = [];
259  foreach ($expectedErrorData['errors'] as $error) {
260  // @see \Magento\TestFramework\TestCase\WebapiAbstract::getActualWrappedErrors()
261  $expectedWrappedErrors[] = [
262  'message' => $error['message'],
263  'params' => $error['parameters'],
264  ];
265  }
266  $this->checkSoapFault(
267  $e,
268  $expectedErrorData['message'],
269  'env:Sender',
270  [],
271  $expectedWrappedErrors
272  );
273  } else {
274  throw $e;
275  }
276  }
277  }
278 }
$response
Definition: 404.php:11
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$sourceCode
Definition: inventory.phtml:11
checkSoapFault( $soapFault, $expectedMessage, $expectedFaultCode, $expectedErrorParams=[], $expectedWrappedErrors=[], $traceString=null)