ValidationTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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\StockRepository;
  8. use Magento\Framework\Webapi\Exception;
  9. use Magento\Framework\Webapi\Rest\Request;
  10. use Magento\InventoryApi\Api\Data\StockInterface;
  11. use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
  12. use Magento\TestFramework\TestCase\WebapiAbstract;
  13. class ValidationTest extends WebapiAbstract
  14. {
  15. /**#@+
  16. * Service constants
  17. */
  18. const RESOURCE_PATH = '/V1/inventory/stocks';
  19. const SERVICE_NAME = 'inventoryApiStockRepositoryV1';
  20. /**#@-*/
  21. /**
  22. * @var array
  23. */
  24. private $validData = [
  25. StockInterface::NAME => 'stock-name',
  26. ];
  27. /**
  28. * @param string $field
  29. * @param array $expectedErrorData
  30. * @throws \Exception
  31. * @dataProvider dataProviderRequiredFields
  32. */
  33. public function testCreateWithMissedRequiredFields(string $field, array $expectedErrorData)
  34. {
  35. $data = $this->validData;
  36. unset($data[$field]);
  37. $serviceInfo = [
  38. 'rest' => [
  39. 'resourcePath' => self::RESOURCE_PATH,
  40. 'httpMethod' => Request::HTTP_METHOD_POST,
  41. ],
  42. 'soap' => [
  43. 'service' => self::SERVICE_NAME,
  44. 'operation' => self::SERVICE_NAME . 'Save',
  45. ],
  46. ];
  47. $this->webApiCall($serviceInfo, $data, $expectedErrorData);
  48. }
  49. /**
  50. * @return array
  51. */
  52. public function dataProviderRequiredFields(): array
  53. {
  54. return [
  55. 'without_' . StockInterface::NAME => [
  56. StockInterface::NAME,
  57. [
  58. 'message' => 'Validation Failed',
  59. 'errors' => [
  60. [
  61. 'message' => '"%field" can not be empty.',
  62. 'parameters' => [
  63. 'field' => StockInterface::NAME,
  64. ],
  65. ],
  66. ],
  67. ],
  68. ],
  69. ];
  70. }
  71. /**
  72. * @param string $field
  73. * @param string|null $value
  74. * @param array $expectedErrorData
  75. * @dataProvider failedValidationDataProvider
  76. */
  77. public function testFailedValidationOnCreate(string $field, $value, array $expectedErrorData)
  78. {
  79. $data = $this->validData;
  80. $data[$field] = $value;
  81. $serviceInfo = [
  82. 'rest' => [
  83. 'resourcePath' => self::RESOURCE_PATH,
  84. 'httpMethod' => Request::HTTP_METHOD_POST,
  85. ],
  86. 'soap' => [
  87. 'service' => self::SERVICE_NAME,
  88. 'operation' => self::SERVICE_NAME . 'Save',
  89. ],
  90. ];
  91. $this->webApiCall($serviceInfo, $data, $expectedErrorData);
  92. }
  93. public function testFailedValidationWhenCreateOnNotExistingWebsite()
  94. {
  95. $notExistingWebsiteCode = 'NotExistingWebsite';
  96. $data = [
  97. StockInterface::NAME => 'stock-name',
  98. StockInterface::EXTENSION_ATTRIBUTES_KEY => [
  99. 'sales_channels' => [[
  100. 'type' => SalesChannelInterface::TYPE_WEBSITE,
  101. 'code' => $notExistingWebsiteCode
  102. ]]
  103. ]
  104. ];
  105. $expectedErrorData = [
  106. 'message' => 'Validation Failed',
  107. 'errors' => [
  108. [
  109. 'message' => 'The website with code "%code" does not exist.',
  110. 'parameters' => [
  111. 'code' => $notExistingWebsiteCode,
  112. ],
  113. ],
  114. ],
  115. ];
  116. $serviceInfo = [
  117. 'rest' => [
  118. 'resourcePath' => self::RESOURCE_PATH,
  119. 'httpMethod' => Request::HTTP_METHOD_POST,
  120. ],
  121. 'soap' => [
  122. 'service' => self::SERVICE_NAME,
  123. 'operation' => self::SERVICE_NAME . 'Save',
  124. ],
  125. ];
  126. $this->webApiCall($serviceInfo, $data, $expectedErrorData);
  127. }
  128. /**
  129. * @param string $field
  130. * @param string|null $value
  131. * @param array $expectedErrorData
  132. * @dataProvider failedValidationDataProvider
  133. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock.php
  134. */
  135. public function testFailedValidationOnUpdate(string $field, $value, array $expectedErrorData)
  136. {
  137. $data = $this->validData;
  138. $data[$field] = $value;
  139. $stockId = 10;
  140. $serviceInfo = [
  141. 'rest' => [
  142. 'resourcePath' => self::RESOURCE_PATH . '/' . $stockId,
  143. 'httpMethod' => Request::HTTP_METHOD_PUT,
  144. ],
  145. 'soap' => [
  146. 'service' => self::SERVICE_NAME,
  147. 'operation' => self::SERVICE_NAME . 'Save',
  148. ],
  149. ];
  150. $this->webApiCall($serviceInfo, $data, $expectedErrorData);
  151. }
  152. /**
  153. * @return array
  154. */
  155. public function failedValidationDataProvider(): array
  156. {
  157. return [
  158. 'empty_' . StockInterface::NAME => [
  159. StockInterface::NAME,
  160. '',
  161. [
  162. 'message' => 'Validation Failed',
  163. 'errors' => [
  164. [
  165. 'message' => '"%field" can not be empty.',
  166. 'parameters' => [
  167. 'field' => StockInterface::NAME,
  168. ],
  169. ],
  170. ],
  171. ],
  172. ],
  173. 'whitespaces_' . StockInterface::NAME => [
  174. StockInterface::NAME,
  175. ' ',
  176. [
  177. 'message' => 'Validation Failed',
  178. 'errors' => [
  179. [
  180. 'message' => '"%field" can not be empty.',
  181. 'parameters' => [
  182. 'field' => StockInterface::NAME,
  183. ],
  184. ],
  185. ],
  186. ],
  187. ],
  188. 'null_' . StockInterface::NAME => [
  189. StockInterface::NAME,
  190. null,
  191. [
  192. 'message' => 'Validation Failed',
  193. 'errors' => [
  194. [
  195. 'message' => '"%field" can not be empty.',
  196. 'parameters' => [
  197. 'field' => StockInterface::NAME,
  198. ],
  199. ],
  200. ],
  201. ],
  202. ],
  203. ];
  204. }
  205. /**
  206. * @param array $serviceInfo
  207. * @param array $data
  208. * @param array $expectedErrorData
  209. * @return void
  210. * @throws \Exception
  211. */
  212. private function webApiCall(array $serviceInfo, array $data, array $expectedErrorData)
  213. {
  214. try {
  215. $this->_webApiCall($serviceInfo, ['stock' => $data]);
  216. $this->fail('Expected throwing exception');
  217. } catch (\Exception $e) {
  218. if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
  219. self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
  220. self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode());
  221. } elseif (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
  222. $this->assertInstanceOf('SoapFault', $e);
  223. $expectedWrappedErrors = [];
  224. foreach ($expectedErrorData['errors'] as $error) {
  225. // @see \Magento\TestFramework\TestCase\WebapiAbstract::getActualWrappedErrors()
  226. $expectedWrappedErrors[] = [
  227. 'message' => $error['message'],
  228. 'params' => $error['parameters'],
  229. ];
  230. }
  231. $this->checkSoapFault($e, $expectedErrorData['message'], 'env:Sender', [], $expectedWrappedErrors);
  232. } else {
  233. throw $e;
  234. }
  235. }
  236. }
  237. }