CouponManagementTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\SalesRule\Api;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Magento\TestFramework\TestCase\WebapiAbstract;
  10. class CouponManagementTest extends WebapiAbstract
  11. {
  12. const SERVICE_NAME = 'salesRuleCouponManagementV1';
  13. const RESOURCE_PATH = '/V1/coupons';
  14. const SERVICE_VERSION = "V1";
  15. const SERVICE_NAME_COUPON = 'salesRuleCouponRepositoryV1';
  16. const RESOURCE_PATH_COUPON = '/V1/coupons';
  17. const SERVICE_VERSION_COUPON = "V1";
  18. /**
  19. * @param int $count
  20. * @param int $length
  21. * @param string $format
  22. * @param string $regex
  23. * @dataProvider dataProviderForTestGenerate
  24. * @magentoApiDataFixture Magento/SalesRule/_files/rules_autogeneration.php
  25. */
  26. public function testManagement($count, $length, $format, $regex)
  27. {
  28. /** @var $registry \Magento\Framework\Registry */
  29. $registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
  30. /** @var $salesRule \Magento\SalesRule\Model\Rule */
  31. $salesRule = $registry->registry('_fixture/Magento_SalesRule_Api_RuleRepository');
  32. $ruleId = $salesRule->getRuleId();
  33. $result = $this->generate($ruleId, $count, $length, $format);
  34. $this->assertTrue(is_array($result));
  35. $this->assertTrue(count($result) == $count);
  36. foreach ($result as $code) {
  37. $this->assertRegExp($regex, $code);
  38. }
  39. $couponList = $this->getList($ruleId);
  40. $couponIds = [];
  41. $couponCodes = [];
  42. $cnt = 0;
  43. if (is_array($couponList)) {
  44. foreach ($couponList as $coupon) {
  45. if ($cnt < $count / 2) {
  46. $couponIds[] = $coupon['coupon_id'];
  47. }
  48. $cnt++;
  49. }
  50. $cnt=0;
  51. foreach ($couponList as $coupon) {
  52. if ($cnt >= $count / 2) {
  53. $couponCodes[] = $coupon['code'];
  54. }
  55. $cnt++;
  56. }
  57. }
  58. $couponMassDeleteResult = $this->deleteCouponsByCodes($couponCodes);
  59. $this->assertEmpty($couponMassDeleteResult['failed_items']);
  60. $couponList = $this->getList($ruleId);
  61. $this->assertTrue(count($couponList) == $cnt / 2);
  62. $couponMassDeleteResult = $this->deleteCouponsById($couponIds);
  63. $this->assertEmpty($couponMassDeleteResult['failed_items']);
  64. $couponList = $this->getList($ruleId);
  65. $this->assertTrue(count($couponList) == 0);
  66. }
  67. /**
  68. * @return array
  69. */
  70. public function dataProviderForTestGenerate()
  71. {
  72. return [
  73. [
  74. 10,
  75. 12,
  76. 'alphanum',
  77. '/[a-zA-Z0-9]{12}/',
  78. ],
  79. [
  80. 10,
  81. 10,
  82. 'num',
  83. '/[0-9]{10}/',
  84. ],
  85. [
  86. 10,
  87. 8,
  88. 'alpha',
  89. '/[a-zA-Z]{8}/',
  90. ],
  91. ];
  92. }
  93. /**
  94. * @param int $ruleId
  95. * @param int $count
  96. * @param int $length
  97. * @param string $format
  98. * @return array
  99. */
  100. public function generate($ruleId, $count, $length, $format)
  101. {
  102. $serviceInfo = [
  103. 'rest' => [
  104. 'resourcePath' => self::RESOURCE_PATH . "/generate",
  105. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  106. ],
  107. 'soap' => [
  108. 'service' => self::SERVICE_NAME,
  109. 'serviceVersion' => self::SERVICE_VERSION,
  110. 'operation' => self::SERVICE_NAME . 'generate',
  111. ],
  112. ];
  113. $requestData = [ "couponSpec"=>
  114. [
  115. "rule_id" => $ruleId,
  116. "quantity" => $count,
  117. "length" => $length,
  118. "format" => $format
  119. ]
  120. ];
  121. $result = $this->_webApiCall($serviceInfo, $requestData);
  122. return $result;
  123. }
  124. /**
  125. * @param int $ruleId
  126. * @return array
  127. */
  128. protected function getList($ruleId)
  129. {
  130. $searchCriteria = [
  131. 'searchCriteria' => [
  132. 'filter_groups' => [
  133. [
  134. 'filters' => [
  135. [
  136. 'field' => 'rule_id',
  137. 'value' => $ruleId,
  138. 'condition_type' => 'eq',
  139. ],
  140. ],
  141. ],
  142. ],
  143. 'current_page' => 1,
  144. 'page_size' => 9999,
  145. ],
  146. ];
  147. $serviceInfo = [
  148. 'rest' => [
  149. 'resourcePath' => self::RESOURCE_PATH_COUPON . '/search' . '?' . http_build_query($searchCriteria),
  150. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  151. ],
  152. 'soap' => [
  153. 'service' => self::SERVICE_NAME_COUPON,
  154. 'serviceVersion' => self::SERVICE_VERSION_COUPON,
  155. 'operation' => self::SERVICE_NAME_COUPON . 'GetList',
  156. ],
  157. ];
  158. $response = $this->_webApiCall($serviceInfo, $searchCriteria);
  159. return $response['items'];
  160. }
  161. /**
  162. * @param array $couponArray
  163. * @return array
  164. */
  165. protected function deleteCouponsById($couponArray)
  166. {
  167. $serviceInfo = [
  168. 'rest' => [
  169. 'resourcePath' => self::RESOURCE_PATH . '/deleteByIds' ,
  170. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  171. ],
  172. 'soap' => [
  173. 'service' => self::SERVICE_NAME,
  174. 'serviceVersion' => self::SERVICE_VERSION,
  175. 'operation' => self::SERVICE_NAME . 'deleteByIds',
  176. ],
  177. ];
  178. return $this->_webApiCall($serviceInfo, ['ids' => $couponArray]);
  179. }
  180. /**
  181. * @param array $couponArray
  182. * @return array
  183. */
  184. protected function deleteCouponsByCodes($couponArray)
  185. {
  186. $serviceInfo = [
  187. 'rest' => [
  188. 'resourcePath' => self::RESOURCE_PATH . '/deleteByCodes' ,
  189. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  190. ],
  191. 'soap' => [
  192. 'service' => self::SERVICE_NAME,
  193. 'serviceVersion' => self::SERVICE_VERSION,
  194. 'operation' => self::SERVICE_NAME . 'deleteByCodes',
  195. ],
  196. ];
  197. return $this->_webApiCall($serviceInfo, ['codes' => $couponArray]);
  198. }
  199. }