Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RuleTest.php
Go to the documentation of this file.
1 <?php
7 
8 class RuleTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
18  protected $coupon;
19 
24 
29 
30  protected function setUp()
31  {
32  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
33 
34  $this->coupon = $this->getMockBuilder(\Magento\SalesRule\Model\Coupon::class)
35  ->disableOriginalConstructor()
36  ->setMethods(['loadPrimaryByRule', 'setRule', 'setIsPrimary', 'getCode', 'getUsageLimit'])
37  ->getMock();
38 
39  $couponFactory = $this->getMockBuilder(\Magento\SalesRule\Model\CouponFactory::class)
40  ->disableOriginalConstructor()
41  ->setMethods(['create'])
42  ->getMock();
43  $couponFactory->expects($this->any())
44  ->method('create')
45  ->willReturn($this->coupon);
46 
47  $this->conditionCombineFactoryMock = $this->getMockBuilder(
48  \Magento\SalesRule\Model\Rule\Condition\CombineFactory::class
49  )->disableOriginalConstructor()
50  ->setMethods(['create'])
51  ->getMock();
52 
53  $this->condProdCombineFactoryMock = $this->getMockBuilder(
54  \Magento\SalesRule\Model\Rule\Condition\Product\CombineFactory::class
55  )->disableOriginalConstructor()
56  ->setMethods(['create'])
57  ->getMock();
58 
59  $this->model = $objectManager->getObject(
60  \Magento\SalesRule\Model\Rule::class,
61  [
62  'couponFactory' => $couponFactory,
63  'condCombineFactory' => $this->conditionCombineFactoryMock,
64  'condProdCombineF' => $this->condProdCombineFactoryMock,
65  ]
66  );
67  }
68 
69  public function testLoadCouponCode()
70  {
71  $this->coupon->expects($this->once())
72  ->method('loadPrimaryByRule')
73  ->with(1);
74  $this->coupon->expects($this->once())
75  ->method('setRule')
76  ->with($this->model)
77  ->willReturnSelf();
78  $this->coupon->expects($this->once())
79  ->method('setIsPrimary')
80  ->with(true)
81  ->willReturnSelf();
82  $this->coupon->expects($this->once())
83  ->method('getCode')
84  ->willReturn('test_code');
85  $this->coupon->expects($this->once())
86  ->method('getUsageLimit')
87  ->willReturn(1);
88 
89  $this->model->setId(1);
90  $this->model->setUsesPerCoupon(null);
91  $this->model->setUseAutoGeneration(false);
92 
93  $this->model->loadCouponCode();
94  $this->assertEquals(1, $this->model->getUsesPerCoupon());
95  }
96 
98  {
99  $conditionMock = $this->setupConditionMock();
100 
101  //Make sure that we reset _condition in beforeSave method
102  $this->conditionCombineFactoryMock->expects($this->exactly(2))
103  ->method('create')
104  ->willReturn($conditionMock);
105 
106  $prodConditionMock = $this->setupProdConditionMock();
107  $this->condProdCombineFactoryMock->expects($this->exactly(2))
108  ->method('create')
109  ->willReturn($prodConditionMock);
110 
111  $this->model->beforeSave();
112  $this->model->getConditions();
113  $this->model->getActions();
114  }
115 
119  protected function setupProdConditionMock()
120  {
121  $prodConditionMock = $this->getMockBuilder(\Magento\SalesRule\Model\Rule\Condition\Product\Combine::class)
122  ->disableOriginalConstructor()
123  ->setMethods(['setRule', 'setId', 'loadArray', 'getConditions'])
124  ->getMock();
125 
126  $prodConditionMock->expects($this->any())
127  ->method('setRule')
128  ->willReturnSelf();
129  $prodConditionMock->expects($this->any())
130  ->method('setId')
131  ->willReturnSelf();
132  $prodConditionMock->expects($this->any())
133  ->method('getConditions')
134  ->willReturn([]);
135 
136  return $prodConditionMock;
137  }
138 
142  protected function setupConditionMock()
143  {
144  $conditionMock = $this->getMockBuilder(\Magento\SalesRule\Model\Rule\Condition\Combine::class)
145  ->disableOriginalConstructor()
146  ->setMethods(['setRule', 'setId', 'loadArray', 'getConditions'])
147  ->getMock();
148  $conditionMock->expects($this->any())
149  ->method('setRule')
150  ->willReturnSelf();
151  $conditionMock->expects($this->any())
152  ->method('setId')
153  ->willReturnSelf();
154  $conditionMock->expects($this->any())
155  ->method('getConditions')
156  ->willReturn([]);
157 
158  return $conditionMock;
159  }
160 
161  public function testGetConditionsFieldSetId()
162  {
163  $formName = 'form_name';
164  $this->model->setId(100);
165  $expectedResult = 'form_namerule_conditions_fieldset_100';
166  $this->assertEquals($expectedResult, $this->model->getConditionsFieldSetId($formName));
167  }
168 
169  public function testGetActionsFieldSetId()
170  {
171  $formName = 'form_name';
172  $this->model->setId(100);
173  $expectedResult = 'form_namerule_actions_fieldset_100';
174  $this->assertEquals($expectedResult, $this->model->getActionsFieldSetId($formName));
175  }
176 }
$objectManager
Definition: bootstrap.php:17
$formName
Definition: gallery.phtml:11