CouponRepositoryTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Test\Unit\Model;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class CouponRepositoryTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\SalesRule\Model\CouponRepository
  14. */
  15. protected $model;
  16. /**
  17. * @var \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $searchResultFactory;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $searchResultsMock;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $couponFactory;
  28. /**
  29. * @var \PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $ruleFactory;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $collectionFactory;
  36. /**
  37. * @var \PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $resource;
  40. /**
  41. * @var \PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $extensionAttributesJoinProcessorMock;
  44. /**
  45. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  46. */
  47. protected $objectManager;
  48. /**
  49. * @var \PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $collectionProcessor;
  52. protected function setUp()
  53. {
  54. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  55. $this->searchResultFactory = $this->createPartialMock(
  56. \Magento\SalesRule\Api\Data\CouponSearchResultInterfaceFactory::class,
  57. ['create']
  58. );
  59. $this->searchResultsMock = $this->createMock(\Magento\SalesRule\Api\Data\CouponSearchResultInterface::class);
  60. $this->couponFactory = $this->createPartialMock(\Magento\SalesRule\Model\CouponFactory::class, ['create']);
  61. $this->ruleFactory = $this->createPartialMock(\Magento\SalesRule\Model\RuleFactory::class, ['create']);
  62. $this->collectionFactory = $this->createPartialMock(
  63. \Magento\SalesRule\Model\ResourceModel\Coupon\CollectionFactory::class,
  64. ['create']
  65. );
  66. $this->resource = $this->createMock(\Magento\SalesRule\Model\ResourceModel\Coupon::class);
  67. $this->extensionAttributesJoinProcessorMock = $this->createPartialMock(
  68. \Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class,
  69. ['process']
  70. );
  71. $this->collectionProcessor = $this->createMock(
  72. \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
  73. );
  74. $this->model = $this->objectManager->getObject(
  75. \Magento\SalesRule\Model\CouponRepository::class,
  76. [
  77. 'couponFactory' => $this->couponFactory,
  78. 'ruleFactory' => $this->ruleFactory,
  79. 'searchResultFactory' => $this->searchResultFactory,
  80. 'collectionFactory' => $this->collectionFactory,
  81. 'resourceModel' => $this->resource,
  82. 'extensionAttributesJoinProcessor' => $this->extensionAttributesJoinProcessorMock,
  83. 'collectionProcessor' => $this->collectionProcessor,
  84. ]
  85. );
  86. }
  87. public function testSave()
  88. {
  89. $id = 1;
  90. $coupon = $this->createPartialMock(\Magento\SalesRule\Model\Coupon::class, ['load', 'getCouponId', 'getById']);
  91. $coupon->expects($this->any())->method('load')->with($id)->willReturnSelf();
  92. $coupon->expects($this->any())->method('getCouponId')->willReturn($id);
  93. $this->couponFactory->expects($this->once())->method('create')->willReturn($coupon);
  94. /**
  95. * @var \Magento\SalesRule\Model\Rule $rule
  96. */
  97. $rule = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, ['load', 'getRuleId']);
  98. $rule->expects($this->any())->method('load')->willReturnSelf();
  99. $rule->expects($this->any())->method('getRuleId')->willReturn($id);
  100. $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
  101. $this->resource->expects($this->once())->method('save')->with($coupon);
  102. $this->assertEquals($coupon, $this->model->save($coupon));
  103. }
  104. /**
  105. * @dataProvider saveExceptionsDataProvider
  106. * @param $exceptionObject
  107. * @param $exceptionName
  108. * @param $exceptionMessage
  109. * @param $id
  110. * @throws \Exception
  111. * @throws \Magento\Framework\Exception\LocalizedException
  112. */
  113. public function testSaveWithExceptions($exceptionObject, $exceptionName, $exceptionMessage, $id)
  114. {
  115. /**
  116. * @var \Magento\SalesRule\Model\Coupon $coupon
  117. */
  118. $coupon = $this->createMock(\Magento\SalesRule\Model\Coupon::class);
  119. /**
  120. * @var \Magento\SalesRule\Model\Rule $rule
  121. */
  122. $rule = $this->createPartialMock(\Magento\SalesRule\Model\Rule::class, ['load', 'getRuleId']);
  123. $rule->expects($this->any())->method('load')->willReturnSelf();
  124. $rule->expects($this->any())->method('getRuleId')->willReturn($id);
  125. $this->ruleFactory->expects($this->any())->method('create')->willReturn($rule);
  126. if ($id) {
  127. $this->resource->expects($this->once())->method('save')->with($coupon)
  128. ->willThrowException($exceptionObject);
  129. }
  130. $this->expectException($exceptionName);
  131. $this->expectExceptionMessage($exceptionMessage);
  132. $this->model->save($coupon);
  133. }
  134. /**
  135. * @return array
  136. */
  137. public function saveExceptionsDataProvider()
  138. {
  139. $msg = 'kiwis';
  140. $phrase = new \Magento\Framework\Phrase($msg);
  141. return [
  142. [
  143. new \Magento\Framework\Exception\LocalizedException($phrase),
  144. \Magento\Framework\Exception\LocalizedException::class,
  145. $msg,
  146. 1
  147. ],
  148. [
  149. null, \Magento\Framework\Exception\LocalizedException::class,
  150. 'Error occurred when saving coupon: No such entity with rule_id = ',
  151. false
  152. ]
  153. ];
  154. }
  155. public function testGetById()
  156. {
  157. $id = 10;
  158. /**
  159. * @var \Magento\SalesRule\Model\Coupon $coupon
  160. */
  161. $coupon = $this->createPartialMock(\Magento\SalesRule\Model\Coupon::class, ['load', 'getCouponId']);
  162. $coupon->expects($this->any())->method('load')->with($id)->willReturnSelf();
  163. $coupon->expects($this->any())->method('getCouponId')->willReturn($id);
  164. $this->couponFactory->expects($this->once())->method('create')->willReturn($coupon);
  165. $this->assertEquals($coupon, $this->model->getById($id));
  166. }
  167. public function testDeleteById()
  168. {
  169. $id = 10;
  170. /**
  171. * @var \Magento\SalesRule\Model\Coupon $coupon
  172. */
  173. $coupon = $this->createPartialMock(\Magento\SalesRule\Model\Coupon::class, ['load', 'getCouponId']);
  174. $coupon->expects($this->any())->method('load')->with($id)->willReturnSelf();
  175. $coupon->expects($this->any())->method('getCouponId')->willReturn($id);
  176. $this->couponFactory->expects($this->any())->method('create')->willReturn($coupon);
  177. $this->assertEquals($coupon, $this->model->getById($id));
  178. $this->resource->expects($this->once())->method('delete')->with($coupon);
  179. $this->assertTrue($this->model->deleteById($id));
  180. }
  181. public function testGetList()
  182. {
  183. $collectionSize = 1;
  184. $couponMock = $this->createMock(\Magento\SalesRule\Api\Data\CouponInterface::class);
  185. /**
  186. * @var \Magento\Framework\Api\SearchCriteriaInterface $searchCriteriaMock
  187. */
  188. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  189. $collectionMock = $this->createMock(\Magento\SalesRule\Model\ResourceModel\Coupon\Collection::class);
  190. $this->extensionAttributesJoinProcessorMock->expects($this->once())
  191. ->method('process')
  192. ->with($collectionMock, \Magento\SalesRule\Api\Data\CouponInterface::class);
  193. $this->collectionProcessor->expects($this->once())
  194. ->method('process')
  195. ->with($searchCriteriaMock, $collectionMock);
  196. $this->searchResultsMock->expects($this->once())->method('setSearchCriteria')->with($searchCriteriaMock);
  197. $this->collectionFactory->expects($this->once())->method('create')->willReturn($collectionMock);
  198. $collectionMock->expects($this->once())->method('getSize')->willReturn($collectionSize);
  199. $this->searchResultsMock->expects($this->once())->method('setTotalCount')->with($collectionSize);
  200. $collectionMock->expects($this->once())->method('getItems')->willReturn([$couponMock]);
  201. $this->searchResultsMock->expects($this->once())->method('setItems')->with([$couponMock]);
  202. $this->searchResultFactory->expects($this->once())->method('create')->willReturn($this->searchResultsMock);
  203. $this->assertEquals($this->searchResultsMock, $this->model->getList($searchCriteriaMock));
  204. }
  205. }