/dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/OptionRepositoryTest.php

https://gitlab.com/axeltizon/magento-demopoweraccess · PHP · 288 lines · 202 code · 36 blank · 50 comment · 2 complexity · db80867047762e33408f76ab282e9341 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Copyright © 2016 Magento. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. // @codingStandardsIgnoreFile
  8. namespace Magento\ConfigurableProduct\Api;
  9. class OptionRepositoryTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  10. {
  11. const SERVICE_NAME = 'configurableProductOptionRepositoryV1';
  12. const SERVICE_VERSION = 'V1';
  13. const RESOURCE_PATH = '/V1/configurable-products';
  14. /**
  15. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  16. */
  17. public function testGet()
  18. {
  19. $productSku = 'configurable';
  20. $options = $this->getList($productSku);
  21. $this->assertTrue(is_array($options));
  22. $this->assertNotEmpty($options);
  23. foreach ($options as $option) {
  24. /** @var array $result */
  25. $result = $this->get($productSku, $option['id']);
  26. $this->assertTrue(is_array($result));
  27. $this->assertNotEmpty($result);
  28. $this->assertArrayHasKey('id', $result);
  29. $this->assertEquals($option['id'], $result['id']);
  30. $this->assertArrayHasKey('attribute_id', $result);
  31. $this->assertEquals($option['attribute_id'], $result['attribute_id']);
  32. $this->assertArrayHasKey('label', $result);
  33. $this->assertEquals($option['label'], $result['label']);
  34. $this->assertArrayHasKey('values', $result);
  35. $this->assertTrue(is_array($result['values']));
  36. $this->assertEquals($option['values'], $result['values']);
  37. }
  38. }
  39. /**
  40. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  41. */
  42. public function testGetList()
  43. {
  44. $productSku = 'configurable';
  45. /** @var array $result */
  46. $result = $this->getList($productSku);
  47. $this->assertNotEmpty($result);
  48. $this->assertTrue(is_array($result));
  49. $this->assertArrayHasKey(0, $result);
  50. $option = $result[0];
  51. $this->assertNotEmpty($option);
  52. $this->assertTrue(is_array($option));
  53. $this->assertArrayHasKey('id', $option);
  54. $this->assertArrayHasKey('label', $option);
  55. $this->assertEquals($option['label'], 'Test Configurable');
  56. $this->assertArrayHasKey('values', $option);
  57. $this->assertTrue(is_array($option));
  58. $this->assertNotEmpty($option);
  59. $this->assertCount(2, $option['values']);
  60. foreach ($option['values'] as $value) {
  61. $this->assertTrue(is_array($value));
  62. $this->assertNotEmpty($value);
  63. $this->assertArrayHasKey('value_index', $value);
  64. }
  65. }
  66. /**
  67. * @expectedException \Exception
  68. * @expectedExceptionMessage Requested product doesn't exist
  69. */
  70. public function testGetUndefinedProduct()
  71. {
  72. $productSku = 'product_not_exist';
  73. $this->getList($productSku);
  74. }
  75. /**
  76. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  77. */
  78. public function testGetUndefinedOption()
  79. {
  80. $expectedMessage = 'Requested option doesn\'t exist: %1';
  81. $productSku = 'configurable';
  82. $attributeId = -42;
  83. try {
  84. $this->get($productSku, $attributeId);
  85. } catch (\SoapFault $e) {
  86. $this->assertContains(
  87. $expectedMessage,
  88. $e->getMessage(),
  89. 'SoapFault does not contain expected message.'
  90. );
  91. } catch (\Exception $e) {
  92. $errorObj = $this->processRestExceptionResult($e);
  93. $this->assertEquals($expectedMessage, $errorObj['message']);
  94. $this->assertEquals([$attributeId], $errorObj['parameters']);
  95. }
  96. }
  97. /**
  98. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  99. */
  100. public function testDelete()
  101. {
  102. $productSku = 'configurable';
  103. $optionList = $this->getList($productSku);
  104. $optionId = $optionList[0]['id'];
  105. $resultRemove = $this->delete($productSku, $optionId);
  106. $optionListRemoved = $this->getList($productSku);
  107. $this->assertTrue($resultRemove);
  108. $this->assertEquals(count($optionList) - 1, count($optionListRemoved));
  109. }
  110. /**
  111. * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
  112. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
  113. */
  114. public function testAdd()
  115. {
  116. $productSku = 'simple';
  117. $serviceInfo = [
  118. 'rest' => [
  119. 'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/options',
  120. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  121. ],
  122. 'soap' => [
  123. 'service' => self::SERVICE_NAME,
  124. 'serviceVersion' => self::SERVICE_VERSION,
  125. 'operation' => self::SERVICE_NAME . 'Save'
  126. ]
  127. ];
  128. $option = [
  129. 'attribute_id' => 'test_configurable',
  130. 'label' => 'Test',
  131. 'values' => [
  132. [
  133. 'value_index' => 1,
  134. ]
  135. ],
  136. ];
  137. /** @var int $result */
  138. $result = $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'option' => $option]);
  139. $this->assertGreaterThan(0, $result);
  140. }
  141. /**
  142. * @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
  143. */
  144. public function testUpdate()
  145. {
  146. $productSku = 'configurable';
  147. $configurableAttribute = $this->getConfigurableAttribute($productSku);
  148. $optionId = $configurableAttribute[0]['id'];
  149. $serviceInfo = [
  150. 'rest' => [
  151. 'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/options' . '/' . $optionId,
  152. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT
  153. ],
  154. 'soap' => [
  155. 'service' => self::SERVICE_NAME,
  156. 'serviceVersion' => self::SERVICE_VERSION,
  157. 'operation' => self::SERVICE_NAME . 'Save'
  158. ]
  159. ];
  160. $requestBody = [
  161. 'option' => [
  162. 'label' => 'Update Test Configurable',
  163. ]
  164. ];
  165. if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
  166. $requestBody['sku'] = $productSku;
  167. $requestBody['option']['id'] = $optionId;
  168. }
  169. $result = $this->_webApiCall($serviceInfo, $requestBody);
  170. $this->assertGreaterThan(0, $result);
  171. $configurableAttribute = $this->getConfigurableAttribute($productSku);
  172. $this->assertEquals($requestBody['option']['label'], $configurableAttribute[0]['label']);
  173. }
  174. /**
  175. * @param string $productSku
  176. * @return array
  177. */
  178. protected function getConfigurableAttribute($productSku)
  179. {
  180. $serviceInfo = [
  181. 'rest' => [
  182. 'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/options/all',
  183. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET
  184. ],
  185. 'soap' => [
  186. 'service' => self::SERVICE_NAME,
  187. 'serviceVersion' => self::SERVICE_VERSION,
  188. 'operation' => self::SERVICE_NAME . 'GetList'
  189. ]
  190. ];
  191. return $this->_webApiCall($serviceInfo, ['sku' => $productSku]);
  192. }
  193. /**
  194. * @param string $productSku
  195. * @param int $optionId
  196. * @return bool
  197. */
  198. private function delete($productSku, $optionId)
  199. {
  200. $serviceInfo = [
  201. 'rest' => [
  202. 'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/options/' . $optionId,
  203. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE
  204. ],
  205. 'soap' => [
  206. 'service' => self::SERVICE_NAME,
  207. 'serviceVersion' => self::SERVICE_VERSION,
  208. 'operation' => self::SERVICE_NAME . 'DeleteById'
  209. ]
  210. ];
  211. return $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'id' => $optionId]);
  212. }
  213. /**
  214. * @param $productSku
  215. * @param $optionId
  216. * @return array
  217. */
  218. protected function get($productSku, $optionId)
  219. {
  220. $serviceInfo = [
  221. 'rest' => [
  222. 'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/options/' . $optionId,
  223. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET
  224. ],
  225. 'soap' => [
  226. 'service' => self::SERVICE_NAME,
  227. 'serviceVersion' => self::SERVICE_VERSION,
  228. 'operation' => self::SERVICE_NAME . 'Get'
  229. ]
  230. ];
  231. return $this->_webApiCall($serviceInfo, ['sku' => $productSku, 'id' => $optionId]);
  232. }
  233. /**
  234. * @param $productSku
  235. * @return array
  236. */
  237. protected function getList($productSku)
  238. {
  239. $serviceInfo = [
  240. 'rest' => [
  241. 'resourcePath' => self::RESOURCE_PATH . '/' . $productSku . '/options/all',
  242. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET
  243. ],
  244. 'soap' => [
  245. 'service' => self::SERVICE_NAME,
  246. 'serviceVersion' => self::SERVICE_VERSION,
  247. 'operation' => self::SERVICE_NAME . 'GetList'
  248. ]
  249. ];
  250. return $this->_webApiCall($serviceInfo, ['sku' => $productSku]);
  251. }
  252. }