CouponRepositoryTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 CouponRepositoryTest extends WebapiAbstract
  11. {
  12. const SERVICE_NAME = 'salesRuleCouponRepositoryV1';
  13. const RESOURCE_PATH = '/V1/coupons';
  14. const SERVICE_VERSION = "V1";
  15. /**
  16. * @var \Magento\Framework\ObjectManagerInterface
  17. */
  18. private $objectManager;
  19. protected function setUp()
  20. {
  21. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  22. }
  23. protected function getCouponData()
  24. {
  25. $data = [
  26. 'rule_id' => '1',
  27. 'code' => 'mycouponcode1',
  28. 'times_used' => 0,
  29. 'is_primary' => null,
  30. 'created_at' => '2015-07-20 00:00:00',
  31. 'type' => 1,
  32. ];
  33. return $data;
  34. }
  35. /**
  36. * @magentoApiDataFixture Magento/SalesRule/_files/rules_autogeneration.php
  37. */
  38. public function testCrud()
  39. {
  40. //test create
  41. $inputData = $this->getCouponData();
  42. /** @var $registry \Magento\Framework\Registry */
  43. $registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
  44. /** @var $salesRule \Magento\SalesRule\Model\Rule */
  45. $salesRule = $registry->registry('_fixture/Magento_SalesRule_Api_RuleRepository');
  46. $ruleId = $salesRule->getRuleId();
  47. $inputData['rule_id'] = $ruleId;
  48. $result = $this->createCoupon($inputData);
  49. $this->assertArrayHasKey('coupon_id', $result);
  50. $couponId = $result['coupon_id'];
  51. unset($result['coupon_id']);
  52. $result = $this->verifySalesRuleInfluence($result);
  53. $this->assertEquals($inputData, $result);
  54. //test getList
  55. $result = $this->verifyGetList($couponId);
  56. $inputData = array_merge(['coupon_id' => $couponId], $inputData);
  57. $result = $this->verifySalesRuleInfluence($result);
  58. $this->assertEquals($inputData, $result);
  59. //test update
  60. $inputData['times_used'] = 2;
  61. $inputData['code'] = 'mycouponcode2';
  62. $result = $this->updateCoupon($couponId, $inputData);
  63. $result = $this->verifySalesRuleInfluence($result);
  64. $this->assertEquals($inputData, $result);
  65. //test get
  66. $result = $this->getCoupon($couponId);
  67. $result = $this->verifySalesRuleInfluence($result);
  68. $this->assertEquals($inputData, $result);
  69. //test delete
  70. $this->assertEquals(true, $this->deleteCoupon($couponId));
  71. }
  72. // verify (and remove) the fields that are set by the Sales Rule
  73. protected function verifySalesRuleInfluence($result)
  74. {
  75. //optional
  76. unset($result['expiration_date']);
  77. $this->assertArrayHasKey('usage_per_customer', $result);
  78. unset($result['usage_per_customer']);
  79. $this->assertArrayHasKey('usage_limit', $result);
  80. unset($result['usage_limit']);
  81. return $result;
  82. }
  83. /**
  84. * @magentoApiDataFixture Magento/SalesRule/_files/coupons_advanced.php
  85. */
  86. public function testGetListWithMultipleFiltersAndSorting()
  87. {
  88. /** @var $searchCriteriaBuilder \Magento\Framework\Api\SearchCriteriaBuilder */
  89. $searchCriteriaBuilder = $this->objectManager->create(
  90. \Magento\Framework\Api\SearchCriteriaBuilder::class
  91. );
  92. /** @var $filterBuilder \Magento\Framework\Api\FilterBuilder */
  93. $filterBuilder = $this->objectManager->create(
  94. \Magento\Framework\Api\FilterBuilder::class
  95. );
  96. /** @var \Magento\Framework\Api\SortOrderBuilder $sortOrderBuilder */
  97. $sortOrderBuilder = $this->objectManager->create(
  98. \Magento\Framework\Api\SortOrderBuilder::class
  99. );
  100. $filter1 = $filterBuilder->setField('type')
  101. ->setValue(1)
  102. ->setConditionType('eq')
  103. ->create();
  104. $filter2 = $filterBuilder->setField('code')
  105. ->setValue('coupon_code_auto')
  106. ->setConditionType('eq')
  107. ->create();
  108. $filter3 = $filterBuilder->setField('is_primary')
  109. ->setValue(1)
  110. ->setConditionType('eq')
  111. ->create();
  112. $sortOrder = $sortOrderBuilder->setField('code')
  113. ->setDirection('DESC')
  114. ->create();
  115. $searchCriteriaBuilder->addFilters([$filter1, $filter2]);
  116. $searchCriteriaBuilder->addFilters([$filter3]);
  117. $searchCriteriaBuilder->addSortOrder($sortOrder);
  118. $searchData = $searchCriteriaBuilder->create()->__toArray();
  119. $requestData = ['searchCriteria' => $searchData];
  120. $serviceInfo = [
  121. 'rest' => [
  122. 'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($requestData),
  123. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  124. ],
  125. 'soap' => [
  126. 'service' => self::SERVICE_NAME,
  127. 'serviceVersion' => self::SERVICE_VERSION,
  128. 'operation' => self::SERVICE_NAME . 'GetList',
  129. ],
  130. ];
  131. $result = $this->_webApiCall($serviceInfo, $requestData);
  132. $this->assertArrayHasKey('items', $result);
  133. $this->assertArrayHasKey('search_criteria', $result);
  134. $this->assertCount(2, $result['items']);
  135. $this->assertEquals('autogenerated_3_2', $result['items'][0]['code']);
  136. $this->assertEquals('autogenerated_2_1', $result['items'][1]['code']);
  137. $this->assertEquals($searchData, $result['search_criteria']);
  138. }
  139. public function verifyGetList($couponId)
  140. {
  141. $searchCriteria = [
  142. 'searchCriteria' => [
  143. 'filter_groups' => [
  144. [
  145. 'filters' => [
  146. [
  147. 'field' => 'coupon_id',
  148. 'value' => $couponId,
  149. 'condition_type' => 'eq',
  150. ],
  151. ],
  152. ],
  153. ],
  154. 'current_page' => 1,
  155. 'page_size' => 2,
  156. ],
  157. ];
  158. $serviceInfo = [
  159. 'rest' => [
  160. 'resourcePath' => self::RESOURCE_PATH . '/search' . '?' . http_build_query($searchCriteria),
  161. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  162. ],
  163. 'soap' => [
  164. 'service' => self::SERVICE_NAME,
  165. 'serviceVersion' => self::SERVICE_VERSION,
  166. 'operation' => self::SERVICE_NAME . 'GetList',
  167. ],
  168. ];
  169. $response = $this->_webApiCall($serviceInfo, $searchCriteria);
  170. $this->assertArrayHasKey('search_criteria', $response);
  171. $this->assertArrayHasKey('total_count', $response);
  172. $this->assertArrayHasKey('items', $response);
  173. $this->assertEquals($searchCriteria['searchCriteria'], $response['search_criteria']);
  174. $this->assertTrue($response['total_count'] > 0);
  175. $this->assertTrue(count($response['items']) > 0);
  176. $this->assertNotNull($response['items'][0]['rule_id']);
  177. $this->assertEquals($couponId, $response['items'][0]['coupon_id']);
  178. return $response['items'][0];
  179. }
  180. protected function createCoupon($coupon)
  181. {
  182. $serviceInfo = [
  183. 'rest' => [
  184. 'resourcePath' => self::RESOURCE_PATH,
  185. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
  186. ],
  187. 'soap' => [
  188. 'service' => self::SERVICE_NAME,
  189. 'serviceVersion' => self::SERVICE_VERSION,
  190. 'operation' => self::SERVICE_NAME . 'Save',
  191. ],
  192. ];
  193. $requestData = ['coupon' => $coupon];
  194. return $this->_webApiCall($serviceInfo, $requestData);
  195. }
  196. protected function deleteCoupon($couponId)
  197. {
  198. $serviceInfo = [
  199. 'rest' => [
  200. 'resourcePath' => self::RESOURCE_PATH . '/' . $couponId,
  201. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
  202. ],
  203. 'soap' => [
  204. 'service' => self::SERVICE_NAME,
  205. 'serviceVersion' => self::SERVICE_VERSION,
  206. 'operation' => self::SERVICE_NAME . 'DeleteById',
  207. ],
  208. ];
  209. return $this->_webApiCall($serviceInfo, ['coupon_id' => $couponId]);
  210. }
  211. protected function updateCoupon($couponId, $data)
  212. {
  213. $serviceInfo = [
  214. 'rest' => [
  215. 'resourcePath' => self::RESOURCE_PATH . '/' . $couponId,
  216. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
  217. ],
  218. 'soap' => [
  219. 'service' => self::SERVICE_NAME,
  220. 'serviceVersion' => self::SERVICE_VERSION,
  221. 'operation' => self::SERVICE_NAME . 'Save',
  222. ],
  223. ];
  224. $data['coupon_id'] = $couponId;
  225. return $this->_webApiCall($serviceInfo, ['coupon_id' => $couponId, 'coupon' => $data]);
  226. }
  227. /**
  228. * Retrieve an existing coupon
  229. *
  230. * @param int $couponId
  231. * @return \Magento\SalesRule\Api\Data\CouponInterface
  232. */
  233. protected function getCoupon($couponId)
  234. {
  235. $serviceInfo = [
  236. 'rest' => [
  237. 'resourcePath' => self::RESOURCE_PATH . '/' . $couponId,
  238. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  239. ],
  240. 'soap' => [
  241. 'service' => self::SERVICE_NAME,
  242. 'serviceVersion' => self::SERVICE_VERSION,
  243. 'operation' => self::SERVICE_NAME . 'GetById',
  244. ],
  245. ];
  246. return $this->_webApiCall($serviceInfo, ['coupon_id' => $couponId]);
  247. }
  248. }