DeleteTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Rest\Request;
  9. use Magento\TestFramework\TestCase\WebapiAbstract;
  10. use Magento\Framework\Webapi\Exception;
  11. class DeleteTest extends WebapiAbstract
  12. {
  13. /**#@+
  14. * Service constants
  15. */
  16. const RESOURCE_PATH = '/V1/inventory/stocks';
  17. const SERVICE_NAME = 'inventoryApiStockRepositoryV1';
  18. /**#@-*/
  19. /**
  20. * @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
  21. */
  22. public function testDeleteById()
  23. {
  24. $stockIdToDelete = 10;
  25. $serviceInfo = [
  26. 'rest' => [
  27. 'resourcePath' => self::RESOURCE_PATH . '/' . $stockIdToDelete,
  28. 'httpMethod' => Request::HTTP_METHOD_DELETE,
  29. ],
  30. 'soap' => [
  31. 'service' => self::SERVICE_NAME,
  32. 'operation' => self::SERVICE_NAME . 'DeleteById',
  33. ],
  34. ];
  35. (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
  36. ? $this->_webApiCall($serviceInfo)
  37. : $this->_webApiCall($serviceInfo, ['stockId' => $stockIdToDelete]);
  38. $this->checkIsStockDeleted($stockIdToDelete);
  39. }
  40. /**
  41. * @param int $deletedStockId
  42. * @throws \Exception
  43. */
  44. private function checkIsStockDeleted($deletedStockId)
  45. {
  46. $serviceInfo = [
  47. 'rest' => [
  48. 'resourcePath' => self::RESOURCE_PATH . '/' . $deletedStockId,
  49. 'httpMethod' => Request::HTTP_METHOD_GET,
  50. ],
  51. 'soap' => [
  52. 'service' => self::SERVICE_NAME,
  53. 'operation' => self::SERVICE_NAME . 'Get',
  54. ],
  55. ];
  56. $expectedMessage = 'Stock with id "%value" does not exist.';
  57. try {
  58. (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
  59. ? $this->_webApiCall($serviceInfo)
  60. : $this->_webApiCall($serviceInfo, ['stockId' => $deletedStockId]);
  61. $this->fail('Expected throwing exception');
  62. } catch (\Exception $e) {
  63. if (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST) {
  64. $errorData = $this->processRestExceptionResult($e);
  65. self::assertEquals($expectedMessage, $errorData['message']);
  66. self::assertEquals($deletedStockId, $errorData['parameters']['value']);
  67. self::assertEquals(Exception::HTTP_NOT_FOUND, $e->getCode());
  68. } elseif (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
  69. $this->assertInstanceOf('SoapFault', $e);
  70. $this->checkSoapFault($e, $expectedMessage, 'env:Sender', ['value' => $deletedStockId]);
  71. } else {
  72. throw $e;
  73. }
  74. }
  75. }
  76. }