PreventDefaultSourceDisablingTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\InventoryCatalogApi\Test\Api;
  8. use Magento\Framework\Webapi\Exception;
  9. use Magento\Framework\Webapi\Rest\Request;
  10. use Magento\InventoryApi\Api\Data\SourceInterface;
  11. use Magento\TestFramework\TestCase\WebapiAbstract;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
  14. class PreventDefaultSourceDisablingTest extends WebapiAbstract
  15. {
  16. /**#@+
  17. * Service constants
  18. */
  19. const RESOURCE_PATH = '/V1/inventory/sources';
  20. const SERVICE_NAME = 'inventoryApiSourceRepositoryV1';
  21. /**#@-*/
  22. /**
  23. * @var DefaultSourceProviderInterface
  24. */
  25. private $defaultSourceProvider;
  26. protected function setUp(): void
  27. {
  28. parent::setUp();
  29. $this->defaultSourceProvider = Bootstrap::getObjectManager()->get(DefaultSourceProviderInterface::class);
  30. }
  31. /**
  32. * @throws \Exception
  33. */
  34. public function testPreventDefaultSourceDisabling(): void
  35. {
  36. $defaultSourceCode = $this->defaultSourceProvider->getCode();
  37. $data = [
  38. SourceInterface::SOURCE_CODE => $defaultSourceCode, // needed for SOAP mode
  39. SourceInterface::NAME => 'source-name-1',
  40. SourceInterface::POSTCODE => 'source-postcode',
  41. SourceInterface::COUNTRY_ID => 'US',
  42. SourceInterface::ENABLED => false,
  43. ];
  44. $serviceInfo = [
  45. 'rest' => [
  46. 'resourcePath' => self::RESOURCE_PATH . '/' . $defaultSourceCode,
  47. 'httpMethod' => Request::HTTP_METHOD_PUT,
  48. ],
  49. 'soap' => [
  50. 'service' => self::SERVICE_NAME,
  51. 'operation' => self::SERVICE_NAME . 'Save',
  52. ],
  53. ];
  54. $expectedErrorData = [
  55. 'message' => 'Validation Failed',
  56. 'errors' => [
  57. [
  58. 'message' => 'Default source can not be disabled.',
  59. 'parameters' => [],
  60. ],
  61. ],
  62. ];
  63. $this->webApiCall($serviceInfo, $data, $expectedErrorData);
  64. }
  65. /**
  66. * @param array $serviceInfo
  67. * @param array $data
  68. * @param array $expectedErrorData
  69. * @return void
  70. * @throws \Exception
  71. */
  72. private function webApiCall(array $serviceInfo, array $data, array $expectedErrorData)
  73. {
  74. try {
  75. $this->_webApiCall($serviceInfo, ['source' => $data]);
  76. $this->fail('Expected throwing exception');
  77. } catch (\Exception $e) {
  78. if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
  79. self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
  80. self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode());
  81. } elseif (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
  82. $this->assertInstanceOf('SoapFault', $e);
  83. $expectedWrappedErrors = [];
  84. foreach ($expectedErrorData['errors'] as $error) {
  85. // @see \Magento\TestFramework\TestCase\WebapiAbstract::getActualWrappedErrors()
  86. $expectedWrappedErrors[] = [
  87. 'message' => $error['message'],
  88. 'params' => $error['parameters'],
  89. ];
  90. }
  91. $this->checkSoapFault($e, $expectedErrorData['message'], 'env:Sender', [], $expectedWrappedErrors);
  92. } else {
  93. throw $e;
  94. }
  95. }
  96. }
  97. }