CreateTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\InventoryApi\Api\Data\StockInterface;
  10. use Magento\InventoryApi\Api\StockRepositoryInterface;
  11. use Magento\TestFramework\Assert\AssertArrayContains;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\TestFramework\TestCase\WebapiAbstract;
  14. class CreateTest extends WebapiAbstract
  15. {
  16. /**#@+
  17. * Service constants
  18. */
  19. const RESOURCE_PATH = '/V1/inventory/stocks';
  20. const SERVICE_NAME = 'inventoryApiStockRepositoryV1';
  21. /**#@-*/
  22. /**
  23. * @var StockRepositoryInterface
  24. */
  25. private $stockRepository;
  26. /**
  27. * @var int
  28. */
  29. private $stockId;
  30. protected function setUp()
  31. {
  32. parent::setUp();
  33. $this->stockRepository = Bootstrap::getObjectManager()->get(StockRepositoryInterface::class);
  34. }
  35. public function testCreate()
  36. {
  37. $expectedData = [
  38. StockInterface::NAME => 'stock-name',
  39. ];
  40. $serviceInfo = [
  41. 'rest' => [
  42. 'resourcePath' => self::RESOURCE_PATH,
  43. 'httpMethod' => Request::HTTP_METHOD_POST,
  44. ],
  45. 'soap' => [
  46. 'service' => self::SERVICE_NAME,
  47. 'operation' => self::SERVICE_NAME . 'Save',
  48. ],
  49. ];
  50. $stockId = $this->_webApiCall($serviceInfo, ['stock' => $expectedData]);
  51. self::assertNotEmpty($stockId);
  52. $this->stockId = $stockId;
  53. AssertArrayContains::assert($expectedData, $this->getStockDataById($stockId));
  54. }
  55. protected function tearDown()
  56. {
  57. if (null !== $this->stockId) {
  58. $this->stockRepository->deleteById($this->stockId);
  59. }
  60. parent::tearDown();
  61. }
  62. /**
  63. * @param int $stockId
  64. * @return array
  65. */
  66. private function getStockDataById(int $stockId): array
  67. {
  68. $serviceInfo = [
  69. 'rest' => [
  70. 'resourcePath' => self::RESOURCE_PATH . '/' . $stockId,
  71. 'httpMethod' => Request::HTTP_METHOD_GET,
  72. ],
  73. 'soap' => [
  74. 'service' => self::SERVICE_NAME,
  75. 'operation' => self::SERVICE_NAME . 'Get',
  76. ],
  77. ];
  78. $response = (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
  79. ? $this->_webApiCall($serviceInfo)
  80. : $this->_webApiCall($serviceInfo, ['stockId' => $stockId]);
  81. self::assertArrayHasKey(StockInterface::STOCK_ID, $response);
  82. return $response;
  83. }
  84. }