Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PreventAssignSourcesToDefaultStockTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
18 {
22  const RESOURCE_PATH_ASSIGN_SOURCES_TO_STOCK = '/V1/inventory/stock-source-links';
23  const SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK = 'inventoryApiStockSourceLinksSaveV1';
36  array $sourceCodes,
37  int $stockId,
38  array $expectedErrorData
39  ) {
40  $serviceInfo = [
41  'rest' => [
43  'httpMethod' => Request::HTTP_METHOD_POST,
44  ],
45  'soap' => [
47  'operation' => self::SERVICE_NAME_ASSIGN_SOURCES_TO_STOCK . 'Execute',
48  ],
49  ];
50 
51  $links = [];
52  foreach ($sourceCodes as $sourceCode) {
53  $links['links'][] = ['stock_id' => $stockId, 'source_code' => $sourceCode, 'priority' => 1];
54  }
55  try {
56  (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
57  ? $this->_webApiCall($serviceInfo, $links)
58  : $this->_webApiCall($serviceInfo, $links);
59  $this->fail('Expected throwing exception');
60  } catch (\Exception $e) {
61  if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
62  self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
63  self::assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
64  } elseif (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
65  $this->assertInstanceOf('SoapFault', $e);
66  $expectedWrappedErrors = [];
67  foreach ($expectedErrorData['errors'] as $error) {
68  // @see \Magento\TestFramework\TestCase\WebapiAbstract::getActualWrappedErrors()
69  $expectedWrappedErrors[] = [
70  'message' => $error['message'],
71  'params' => $error['parameters'],
72  ];
73  }
74  $this->checkSoapFault(
75  $e,
76  $expectedErrorData['message'],
77  'env:Sender',
78  [],
79  $expectedWrappedErrors
80  );
81  } else {
82  throw $e;
83  }
84  }
85  }
86 
90  public function dataProviderWrongParameters(): array
91  {
92  $defaultSourceProvider = Bootstrap::getObjectManager()->get(DefaultSourceProviderInterface::class);
93  $defaultStockProvider = Bootstrap::getObjectManager()->get(DefaultStockProviderInterface::class);
94  return [
95  'multiple_sources_assigned_to_default_stock' => [
96  [$defaultSourceProvider->getCode(), 'eu-2'],
97  $defaultStockProvider->getId(),
98  [
99  'message' => 'Validation Failed',
100  'errors' => [
101  [
102  'message' => 'Can not save link related to Default Source or Default Stock',
103  'parameters' => [],
104  ],
105  ],
106  ],
107  ],
108  'not_default_source_assigned_to_default_stock' => [
109  ['eu-1'],
110  $defaultStockProvider->getId(),
111  [
112  'message' => 'Validation Failed',
113  'errors' => [
114  [
115  'message' => 'Can not save link related to Default Source or Default Stock',
116  'parameters' => [],
117  ],
118  ],
119  ],
120  ],
121  ];
122  }
123 
127  protected function _checkWrappedErrors($expectedWrappedErrors, $errorDetails)
128  {
129  $expectedErrors = [];
130  $wrappedErrorsNode = Fault::NODE_DETAIL_WRAPPED_ERRORS;
131  $wrappedErrorNode = Fault::NODE_DETAIL_WRAPPED_ERROR;
132  foreach ($expectedWrappedErrors as $expectedError) {
133  $expectedErrors[] = $expectedError['message'];
134  }
135  $actualErrors = [];
136  foreach ($errorDetails->$wrappedErrorsNode->$wrappedErrorNode as $error) {
137  if (is_object($error)) {
138  $actualErrors[] = $error->message;
139  } else {
140  $actualErrors[] = $error;
141  }
142  }
143  $this->assertEquals(
144  $expectedErrors,
145  $actualErrors,
146  'Wrapped errors in fault details are invalid.'
147  );
148  }
149 }
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)