/app/code/Magento/SalesRule/Test/Unit/Model/CouponRepositoryTest.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 271 lines · 178 code · 32 blank · 61 comment · 1 complexity · e81576b4205d2f35c4bfcbaa7f787359 MD5 · raw file

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