123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\InventoryApi\Test\Api\StockRepository;
- use Magento\Framework\Webapi\Exception;
- use Magento\Framework\Webapi\Rest\Request;
- use Magento\InventoryApi\Api\Data\StockInterface;
- use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
- use Magento\TestFramework\TestCase\WebapiAbstract;
- class ValidationTest extends WebapiAbstract
- {
- /**#@+
- * Service constants
- */
- const RESOURCE_PATH = '/V1/inventory/stocks';
- const SERVICE_NAME = 'inventoryApiStockRepositoryV1';
- /**#@-*/
- /**
- * @var array
- */
- private $validData = [
- StockInterface::NAME => 'stock-name',
- ];
- /**
- * @param string $field
- * @param array $expectedErrorData
- * @throws \Exception
- * @dataProvider dataProviderRequiredFields
- */
- public function testCreateWithMissedRequiredFields(string $field, array $expectedErrorData)
- {
- $data = $this->validData;
- unset($data[$field]);
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- $this->webApiCall($serviceInfo, $data, $expectedErrorData);
- }
- /**
- * @return array
- */
- public function dataProviderRequiredFields(): array
- {
- return [
- 'without_' . StockInterface::NAME => [
- StockInterface::NAME,
- [
- 'message' => 'Validation Failed',
- 'errors' => [
- [
- 'message' => '"%field" can not be empty.',
- 'parameters' => [
- 'field' => StockInterface::NAME,
- ],
- ],
- ],
- ],
- ],
- ];
- }
- /**
- * @param string $field
- * @param string|null $value
- * @param array $expectedErrorData
- * @dataProvider failedValidationDataProvider
- */
- public function testFailedValidationOnCreate(string $field, $value, array $expectedErrorData)
- {
- $data = $this->validData;
- $data[$field] = $value;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- $this->webApiCall($serviceInfo, $data, $expectedErrorData);
- }
- public function testFailedValidationWhenCreateOnNotExistingWebsite()
- {
- $notExistingWebsiteCode = 'NotExistingWebsite';
- $data = [
- StockInterface::NAME => 'stock-name',
- StockInterface::EXTENSION_ATTRIBUTES_KEY => [
- 'sales_channels' => [[
- 'type' => SalesChannelInterface::TYPE_WEBSITE,
- 'code' => $notExistingWebsiteCode
- ]]
- ]
- ];
- $expectedErrorData = [
- 'message' => 'Validation Failed',
- 'errors' => [
- [
- 'message' => 'The website with code "%code" does not exist.',
- 'parameters' => [
- 'code' => $notExistingWebsiteCode,
- ],
- ],
- ],
- ];
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH,
- 'httpMethod' => Request::HTTP_METHOD_POST,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- $this->webApiCall($serviceInfo, $data, $expectedErrorData);
- }
- /**
- * @param string $field
- * @param string|null $value
- * @param array $expectedErrorData
- * @dataProvider failedValidationDataProvider
- * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock.php
- */
- public function testFailedValidationOnUpdate(string $field, $value, array $expectedErrorData)
- {
- $data = $this->validData;
- $data[$field] = $value;
- $stockId = 10;
- $serviceInfo = [
- 'rest' => [
- 'resourcePath' => self::RESOURCE_PATH . '/' . $stockId,
- 'httpMethod' => Request::HTTP_METHOD_PUT,
- ],
- 'soap' => [
- 'service' => self::SERVICE_NAME,
- 'operation' => self::SERVICE_NAME . 'Save',
- ],
- ];
- $this->webApiCall($serviceInfo, $data, $expectedErrorData);
- }
- /**
- * @return array
- */
- public function failedValidationDataProvider(): array
- {
- return [
- 'empty_' . StockInterface::NAME => [
- StockInterface::NAME,
- '',
- [
- 'message' => 'Validation Failed',
- 'errors' => [
- [
- 'message' => '"%field" can not be empty.',
- 'parameters' => [
- 'field' => StockInterface::NAME,
- ],
- ],
- ],
- ],
- ],
- 'whitespaces_' . StockInterface::NAME => [
- StockInterface::NAME,
- ' ',
- [
- 'message' => 'Validation Failed',
- 'errors' => [
- [
- 'message' => '"%field" can not be empty.',
- 'parameters' => [
- 'field' => StockInterface::NAME,
- ],
- ],
- ],
- ],
- ],
- 'null_' . StockInterface::NAME => [
- StockInterface::NAME,
- null,
- [
- 'message' => 'Validation Failed',
- 'errors' => [
- [
- 'message' => '"%field" can not be empty.',
- 'parameters' => [
- 'field' => StockInterface::NAME,
- ],
- ],
- ],
- ],
- ],
- ];
- }
- /**
- * @param array $serviceInfo
- * @param array $data
- * @param array $expectedErrorData
- * @return void
- * @throws \Exception
- */
- private function webApiCall(array $serviceInfo, array $data, array $expectedErrorData)
- {
- try {
- $this->_webApiCall($serviceInfo, ['stock' => $data]);
- $this->fail('Expected throwing exception');
- } catch (\Exception $e) {
- if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
- self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
- self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode());
- } elseif (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
- $this->assertInstanceOf('SoapFault', $e);
- $expectedWrappedErrors = [];
- foreach ($expectedErrorData['errors'] as $error) {
- // @see \Magento\TestFramework\TestCase\WebapiAbstract::getActualWrappedErrors()
- $expectedWrappedErrors[] = [
- 'message' => $error['message'],
- 'params' => $error['parameters'],
- ];
- }
- $this->checkSoapFault($e, $expectedErrorData['message'], 'env:Sender', [], $expectedWrappedErrors);
- } else {
- throw $e;
- }
- }
- }
- }
|