RuleTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. class RuleTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\SalesRule\Model\Rule
  11. */
  12. protected $model;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $coupon;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\SalesRule\Model\Rule\Condition\CombineFactory
  19. */
  20. protected $conditionCombineFactoryMock;
  21. /**
  22. * @var \Magento\SalesRule\Model\Rule\Condition\Product\CombineFactory|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $condProdCombineFactoryMock;
  25. protected function setUp()
  26. {
  27. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  28. $this->coupon = $this->getMockBuilder(\Magento\SalesRule\Model\Coupon::class)
  29. ->disableOriginalConstructor()
  30. ->setMethods(['loadPrimaryByRule', 'setRule', 'setIsPrimary', 'getCode', 'getUsageLimit'])
  31. ->getMock();
  32. $couponFactory = $this->getMockBuilder(\Magento\SalesRule\Model\CouponFactory::class)
  33. ->disableOriginalConstructor()
  34. ->setMethods(['create'])
  35. ->getMock();
  36. $couponFactory->expects($this->any())
  37. ->method('create')
  38. ->willReturn($this->coupon);
  39. $this->conditionCombineFactoryMock = $this->getMockBuilder(
  40. \Magento\SalesRule\Model\Rule\Condition\CombineFactory::class
  41. )->disableOriginalConstructor()
  42. ->setMethods(['create'])
  43. ->getMock();
  44. $this->condProdCombineFactoryMock = $this->getMockBuilder(
  45. \Magento\SalesRule\Model\Rule\Condition\Product\CombineFactory::class
  46. )->disableOriginalConstructor()
  47. ->setMethods(['create'])
  48. ->getMock();
  49. $this->model = $objectManager->getObject(
  50. \Magento\SalesRule\Model\Rule::class,
  51. [
  52. 'couponFactory' => $couponFactory,
  53. 'condCombineFactory' => $this->conditionCombineFactoryMock,
  54. 'condProdCombineF' => $this->condProdCombineFactoryMock,
  55. ]
  56. );
  57. }
  58. public function testLoadCouponCode()
  59. {
  60. $this->coupon->expects($this->once())
  61. ->method('loadPrimaryByRule')
  62. ->with(1);
  63. $this->coupon->expects($this->once())
  64. ->method('setRule')
  65. ->with($this->model)
  66. ->willReturnSelf();
  67. $this->coupon->expects($this->once())
  68. ->method('setIsPrimary')
  69. ->with(true)
  70. ->willReturnSelf();
  71. $this->coupon->expects($this->once())
  72. ->method('getCode')
  73. ->willReturn('test_code');
  74. $this->coupon->expects($this->once())
  75. ->method('getUsageLimit')
  76. ->willReturn(1);
  77. $this->model->setId(1);
  78. $this->model->setUsesPerCoupon(null);
  79. $this->model->setUseAutoGeneration(false);
  80. $this->model->loadCouponCode();
  81. $this->assertEquals(1, $this->model->getUsesPerCoupon());
  82. }
  83. public function testBeforeSaveResetConditionToNull()
  84. {
  85. $conditionMock = $this->setupConditionMock();
  86. //Make sure that we reset _condition in beforeSave method
  87. $this->conditionCombineFactoryMock->expects($this->exactly(2))
  88. ->method('create')
  89. ->willReturn($conditionMock);
  90. $prodConditionMock = $this->setupProdConditionMock();
  91. $this->condProdCombineFactoryMock->expects($this->exactly(2))
  92. ->method('create')
  93. ->willReturn($prodConditionMock);
  94. $this->model->beforeSave();
  95. $this->model->getConditions();
  96. $this->model->getActions();
  97. }
  98. /**
  99. * @return \PHPUnit_Framework_MockObject_MockObject
  100. */
  101. protected function setupProdConditionMock()
  102. {
  103. $prodConditionMock = $this->getMockBuilder(\Magento\SalesRule\Model\Rule\Condition\Product\Combine::class)
  104. ->disableOriginalConstructor()
  105. ->setMethods(['setRule', 'setId', 'loadArray', 'getConditions'])
  106. ->getMock();
  107. $prodConditionMock->expects($this->any())
  108. ->method('setRule')
  109. ->willReturnSelf();
  110. $prodConditionMock->expects($this->any())
  111. ->method('setId')
  112. ->willReturnSelf();
  113. $prodConditionMock->expects($this->any())
  114. ->method('getConditions')
  115. ->willReturn([]);
  116. return $prodConditionMock;
  117. }
  118. /**
  119. * @return \PHPUnit_Framework_MockObject_MockObject
  120. */
  121. protected function setupConditionMock()
  122. {
  123. $conditionMock = $this->getMockBuilder(\Magento\SalesRule\Model\Rule\Condition\Combine::class)
  124. ->disableOriginalConstructor()
  125. ->setMethods(['setRule', 'setId', 'loadArray', 'getConditions'])
  126. ->getMock();
  127. $conditionMock->expects($this->any())
  128. ->method('setRule')
  129. ->willReturnSelf();
  130. $conditionMock->expects($this->any())
  131. ->method('setId')
  132. ->willReturnSelf();
  133. $conditionMock->expects($this->any())
  134. ->method('getConditions')
  135. ->willReturn([]);
  136. return $conditionMock;
  137. }
  138. public function testGetConditionsFieldSetId()
  139. {
  140. $formName = 'form_name';
  141. $this->model->setId(100);
  142. $expectedResult = 'form_namerule_conditions_fieldset_100';
  143. $this->assertEquals($expectedResult, $this->model->getConditionsFieldSetId($formName));
  144. }
  145. public function testGetActionsFieldSetId()
  146. {
  147. $formName = 'form_name';
  148. $this->model->setId(100);
  149. $expectedResult = 'form_namerule_actions_fieldset_100';
  150. $this->assertEquals($expectedResult, $this->model->getActionsFieldSetId($formName));
  151. }
  152. }