Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CartPriceRulesFixtureTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Setup\Fixtures\CartPriceRulesFixture;
10 
14 class CartPriceRulesFixtureTest extends \PHPUnit\Framework\TestCase
15 {
19  private $fixtureModelMock;
20 
24  private $model;
25 
29  private $ruleFactoryMock;
30 
31  public function setUp()
32  {
33  $this->fixtureModelMock = $this->createMock(\Magento\Setup\Fixtures\FixtureModel::class);
34  $this->ruleFactoryMock = $this->createPartialMock(\Magento\SalesRule\Model\RuleFactory::class, ['create']);
35  $this->model = new CartPriceRulesFixture($this->fixtureModelMock, $this->ruleFactoryMock);
36  }
37 
38  public function testExecute()
39  {
40  $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
41  $storeMock->expects($this->once())
42  ->method('getRootCategoryId')
43  ->will($this->returnValue(2));
44 
45  $websiteMock = $this->createMock(\Magento\Store\Model\Website::class);
46  $websiteMock->expects($this->once())
47  ->method('getGroups')
48  ->will($this->returnValue([$storeMock]));
49  $websiteMock->expects($this->once())
50  ->method('getId')
51  ->will($this->returnValue('website_id'));
52 
53  $contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
54  $abstractDbMock = $this->getMockForAbstractClass(
55  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
56  [$contextMock],
57  '',
58  true,
59  true,
60  true,
61  ['getAllChildren']
62  );
63  $abstractDbMock->expects($this->once())
64  ->method('getAllChildren')
65  ->will($this->returnValue([1]));
66 
67  $storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
68  $storeManagerMock->expects($this->once())
69  ->method('getWebsites')
70  ->will($this->returnValue([$websiteMock]));
71 
72  $categoryMock = $this->createMock(\Magento\Catalog\Model\Category::class);
73  $categoryMock->expects($this->once())
74  ->method('getResource')
75  ->will($this->returnValue($abstractDbMock));
76  $categoryMock->expects($this->once())
77  ->method('getPath')
78  ->will($this->returnValue('path/to/file'));
79  $categoryMock->expects($this->once())
80  ->method('getId')
81  ->will($this->returnValue('category_id'));
82 
83  $objectValueMap = [
84  [\Magento\Catalog\Model\Category::class, $categoryMock]
85  ];
86 
87  $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManager\ObjectManager::class);
88  $objectManagerMock->expects($this->once())
89  ->method('create')
90  ->will($this->returnValue($storeManagerMock));
91  $objectManagerMock->expects($this->once())
92  ->method('get')
93  ->will($this->returnValueMap($objectValueMap));
94 
95  $valueMap = [
96  ['cart_price_rules', 0, 1],
97  ['cart_price_rules_floor', 3, 3],
98  ['cart_price_rules_advanced_type', false, false]
99  ];
100 
101  $this->fixtureModelMock
102  ->expects($this->exactly(3))
103  ->method('getValue')
104  ->will($this->returnValueMap($valueMap));
105  $this->fixtureModelMock
106  ->expects($this->exactly(2))
107  ->method('getObjectManager')
108  ->will($this->returnValue($objectManagerMock));
109 
110  $ruleMock = $this->createMock(\Magento\SalesRule\Model\Rule::class);
111  $this->ruleFactoryMock->expects($this->once())
112  ->method('create')
113  ->willReturn($ruleMock);
114 
115  $this->model->execute();
116  }
117 
118  public function testNoFixtureConfigValue()
119  {
120  $ruleMock = $this->createMock(\Magento\SalesRule\Model\Rule::class);
121  $ruleMock->expects($this->never())->method('save');
122 
123  $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManager\ObjectManager::class);
124  $objectManagerMock->expects($this->never())
125  ->method('get')
126  ->with($this->equalTo(\Magento\SalesRule\Model\Rule::class))
127  ->willReturn($ruleMock);
128 
129  $this->fixtureModelMock
130  ->expects($this->never())
131  ->method('getObjectManager')
132  ->willReturn($objectManagerMock);
133  $this->fixtureModelMock
134  ->expects($this->once())
135  ->method('getValue')
136  ->willReturn(false);
137 
138  $this->model->execute();
139  }
140 
147  public function testGenerateAdvancedCondition($ruleId, $categoriesArray, $ruleCount)
148  {
149  $reflection = new \ReflectionClass($this->model);
150  $reflectionProperty = $reflection->getProperty('cartPriceRulesCount');
151  $reflectionProperty->setAccessible(true);
152  $reflectionProperty->setValue($this->model, $ruleCount);
153 
154  $result = $this->model->generateAdvancedCondition($ruleId, $categoriesArray);
155  if ($ruleId < ($ruleCount - 200)) {
156  $firstCondition = [
157  'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
158  'attribute' => 'category_ids',
159  'operator' => '==',
160  'value' => null,
161  ];
162 
163  $secondCondition = [
164  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
165  'attribute' => 'base_subtotal',
166  'operator' => '>=',
167  'value' => 5,
168  ];
169  $expected = [
170  'conditions' => [
171  1 => [
172  'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
173  'aggregator' => 'all',
174  'value' => '1',
175  'new_child' => '',
176  ],
177  '1--1'=> [
178  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
179  'aggregator' => 'all',
180  'value' => '1',
181  'new_child' => '',
182  ],
183  '1--1--1' => $firstCondition,
184  '1--2' => $secondCondition
185  ],
186  'actions' => [
187  1 => [
188  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Combine::class,
189  'aggregator' => 'all',
190  'value' => '1',
191  'new_child' => '',
192  ],
193  ]
194  ];
195  } else {
196  // Shipping Region
197  $regions = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',
198  'Delaware', 'District of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois',
199  'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts',
200  'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
201  'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
202  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
203  'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia',
204  'Wisconsin', 'Wyoming'];
205  $firstCondition = [
206  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
207  'attribute' => 'region',
208  'operator' => '==',
209  'value' => $regions[($ruleId / 4) % 50],
210  ];
211 
212  $secondCondition = [
213  'type' => \Magento\SalesRule\Model\Rule\Condition\Address::class,
214  'attribute' => 'base_subtotal',
215  'operator' => '>=',
216  'value' => 5,
217  ];
218  $expected = [
219  'conditions' => [
220  1 => [
221  'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
222  'aggregator' => 'all',
223  'value' => '1',
224  'new_child' => '',
225  ],
226  '1--1' => $firstCondition,
227  '1--2' => $secondCondition
228  ],
229  'actions' => [
230  1 => [
231  'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Combine::class,
232  'aggregator' => 'all',
233  'value' => '1',
234  'new_child' => '',
235  ],
236  ]
237  ];
238  }
239  $this->assertSame($expected, $result);
240  }
241 
246  {
247  return [
248  [1, [0], 1],
249  [1, [0], 300]
250  ];
251  }
252 
253  public function testGetActionTitle()
254  {
255  $this->assertSame('Generating cart price rules', $this->model->getActionTitle());
256  }
257 
258  public function testIntroduceParamLabels()
259  {
260  $this->assertSame([
261  'cart_price_rules' => 'Cart Price Rules'
262  ], $this->model->introduceParamLabels());
263  }
264 }