Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TierPriceManagementTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 
18 class TierPriceManagementTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $service;
24 
28  protected $repositoryMock;
29 
33  protected $priceFactoryMock;
34 
38  protected $storeManagerMock;
39 
43  protected $priceModifierMock;
44 
48  protected $websiteMock;
49 
53  protected $configMock;
54 
58  protected $productMock;
59 
64 
69 
70  protected function setUp()
71  {
72  $this->repositoryMock = $this->createMock(\Magento\Catalog\Model\ProductRepository::class);
73  $this->priceFactoryMock = $this->createPartialMock(
74  \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class,
75  ['create']
76  );
77  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
78  $this->websiteMock =
79  $this->createPartialMock(\Magento\Store\Model\Website::class, ['getId', '__wakeup']);
80  $this->productMock = $this->createPartialMock(
81  \Magento\Catalog\Model\Product::class,
82  ['getData', 'getIdBySku', 'load', '__wakeup', 'save', 'validate', 'setData']
83  );
84  $this->configMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
85  $this->priceModifierMock =
86  $this->createMock(\Magento\Catalog\Model\Product\PriceModifier::class);
87  $this->repositoryMock->expects($this->any())->method('get')->with('product_sku')
88  ->will($this->returnValue($this->productMock));
89  $this->groupManagementMock =
90  $this->createMock(\Magento\Customer\Api\GroupManagementInterface::class);
91  $this->groupRepositoryMock =
92  $this->createMock(\Magento\Customer\Api\GroupRepositoryInterface::class);
93 
94  $this->service = new TierPriceManagement(
95  $this->repositoryMock,
96  $this->priceFactoryMock,
97  $this->storeManagerMock,
98  $this->priceModifierMock,
99  $this->configMock,
100  $this->groupManagementMock,
101  $this->groupRepositoryMock
102  );
103  }
104 
112  public function testGetList($configValue, $customerGroupId, $groupData, $expected)
113  {
114  $group = $this->createMock(\Magento\Customer\Model\Data\Group::class);
115  $group->expects($this->any())->method('getId')->willReturn(GroupManagement::CUST_GROUP_ALL);
116  $this->groupManagementMock->expects($this->any())->method('getAllCustomersGroup')
117  ->will($this->returnValue($group));
118  $this->repositoryMock->expects($this->once())->method('get')->with('product_sku')
119  ->will($this->returnValue($this->productMock));
120  $this->productMock
121  ->expects($this->once())
122  ->method('getData')
123  ->with('tier_price')
124  ->will($this->returnValue([$groupData]));
125  $this->configMock
126  ->expects($this->once())
127  ->method('getValue')
128  ->with('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE)
129  ->will($this->returnValue($configValue));
130  if ($expected) {
131  $priceMock = $this->createMock(\Magento\Catalog\Api\Data\ProductTierPriceInterface::class);
132  $priceMock->expects($this->once())
133  ->method('setValue')
134  ->with($expected['value'])
135  ->willReturnSelf();
136  $priceMock->expects($this->once())
137  ->method('setQty')
138  ->with($expected['qty'])
139  ->willReturnSelf();
140  $this->priceFactoryMock
141  ->expects($this->once())
142  ->method('create')
143  ->will($this->returnValue($priceMock));
144  } else {
145  $this->priceFactoryMock->expects($this->never())->method('create');
146  }
147  $prices = $this->service->getList('product_sku', $customerGroupId);
148  $this->assertCount($expected ? 1 : 0, $prices);
149  if ($expected) {
150  $this->assertEquals($priceMock, $prices[0]);
151  }
152  }
153 
157  public function getListDataProvider()
158  {
159  return [
160  [
161  1,
162  'all',
163  ['website_price' => 10, 'price' => 5, 'all_groups' => 1, 'price_qty' => 5],
164  ['value' => 10, 'qty' => 5],
165  ],
166  [
167  0,
168  1,
169  ['website_price' => 10, 'price' => 5, 'all_groups' => 0, 'cust_group' => 1, 'price_qty' => 5],
170  ['value' => 5, 'qty' => 5]
171  ],
172  [
173  0,
174  'all',
175  ['website_price' => 10, 'price' => 5, 'all_groups' => 0, 'cust_group' => 1, 'price_qty' => 5],
176  []
177  ]
178  ];
179  }
180 
181  public function testSuccessDeleteTierPrice()
182  {
183  $this->storeManagerMock
184  ->expects($this->never())
185  ->method('getWebsite');
186  $this->configMock
187  ->expects($this->once())
188  ->method('getValue')
189  ->with('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE)
190  ->will($this->returnValue(0));
191  $this->priceModifierMock->expects($this->once())->method('removeTierPrice')->with($this->productMock, 4, 5, 0);
192 
193  $this->assertEquals(true, $this->service->remove('product_sku', 4, 5, 0));
194  }
195 
201  {
202  $this->repositoryMock->expects($this->once())->method('get')
203  ->will($this->throwException(new NoSuchEntityException()));
204  $this->priceModifierMock->expects($this->never())->method('removeTierPrice');
205  $this->storeManagerMock
206  ->expects($this->never())
207  ->method('getWebsite');
208  $this->service->remove('product_sku', null, 10, 5);
209  }
210 
212  {
213  $this->storeManagerMock
214  ->expects($this->once())
215  ->method('getWebsite')
216  ->will($this->returnValue($this->websiteMock));
217  $this->websiteMock->expects($this->once())->method('getId')->will($this->returnValue(1));
218  $this->configMock
219  ->expects($this->once())
220  ->method('getValue')
221  ->with('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE)
222  ->will($this->returnValue(1));
223  $this->priceModifierMock->expects($this->once())->method('removeTierPrice')->with($this->productMock, 4, 5, 1);
224 
225  $this->assertEquals(true, $this->service->remove('product_sku', 4, 5, 6));
226  }
227 
229  {
230  $websiteMock = $this->getMockBuilder(\Magento\Store\Model\Website::class)
231  ->setMethods(['getId', '__wakeup'])
232  ->disableOriginalConstructor()
233  ->getMock();
234  $websiteMock->expects($this->once())->method('getId')->will($this->returnValue(0));
235 
236  $this->storeManagerMock->expects($this->once())->method('getWebsite')->will($this->returnValue($websiteMock));
237 
238  $this->productMock
239  ->expects($this->once())
240  ->method('getData')
241  ->with('tier_price')
242  ->will(
243  $this->returnValue(
244  [['all_groups' => 0, 'website_id' => 0, 'price_qty' => 4, 'price' => 50]]
245  )
246  );
247  $this->configMock
248  ->expects($this->once())
249  ->method('getValue')
250  ->with('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE)
251  ->will($this->returnValue(1));
252 
253  $this->productMock->expects($this->once())->method('setData')->with(
254  'tier_price',
255  [
256  ['all_groups' => 0, 'website_id' => 0, 'price_qty' => 4, 'price' => 50],
257  [
258  'cust_group' => 32000,
259  'price' => 100,
260  'website_price' => 100,
261  'website_id' => 0,
262  'price_qty' => 3
263  ]
264  ]
265  );
266  $this->repositoryMock->expects($this->once())->method('save')->with($this->productMock);
267  $group = $this->createMock(\Magento\Customer\Model\Data\Group::class);
268  $group->expects($this->once())->method('getId')->willReturn(GroupManagement::CUST_GROUP_ALL);
269  $this->groupManagementMock->expects($this->once())->method('getAllCustomersGroup')
270  ->will($this->returnValue($group));
271  $this->service->add('product_sku', 'all', 100, 3);
272  }
273 
275  {
276  $group = $this->createMock(\Magento\Customer\Model\Data\Group::class);
277  $group->expects($this->once())->method('getId')->will($this->returnValue(1));
278  $this->groupRepositoryMock->expects($this->once())->method('getById')->will($this->returnValue($group));
279  $this->productMock
280  ->expects($this->once())
281  ->method('getData')
282  ->with('tier_price')
283  ->will(
284  $this->returnValue(
285  [['cust_group' => 1, 'website_id' => 0, 'price_qty' => 4, 'price' => 50]]
286  )
287  );
288  $this->configMock
289  ->expects($this->once())
290  ->method('getValue')
291  ->with('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE)
292  ->will($this->returnValue(0));
293 
294  $this->productMock->expects($this->once())->method('setData')->with(
295  'tier_price',
296  [
297  ['cust_group' => 1, 'website_id' => 0, 'price_qty' => 4, 'price' => 50],
298  ['cust_group' => 1, 'website_id' => 0, 'price_qty' => 3, 'price' => 100, 'website_price' => 100]
299  ]
300  );
301  $this->repositoryMock->expects($this->once())->method('save')->with($this->productMock);
302  $this->service->add('product_sku', 1, 100, 3);
303  }
304 
306  {
307  $this->productMock
308  ->expects($this->once())
309  ->method('getData')
310  ->with('tier_price')
311  ->will(
312  $this->returnValue(
313  [['cust_group' => 1, 'website_id' => 0, 'price_qty' => 3, 'price' => 50]]
314  )
315  );
316  $this->configMock
317  ->expects($this->once())
318  ->method('getValue')
319  ->with('catalog/price/scope', \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE)
320  ->will($this->returnValue(0));
321 
322  $this->productMock->expects($this->once())->method('setData')->with(
323  'tier_price',
324  [
325  ['cust_group' => 1, 'website_id' => 0, 'price_qty' => 3, 'price' => 100]
326  ]
327  );
328  $this->repositoryMock->expects($this->once())->method('save')->with($this->productMock);
329  $this->service->add('product_sku', 1, 100, 3);
330  }
331 
337  {
338  $group = $this->createMock(\Magento\Customer\Model\Data\Group::class);
339  $group->expects($this->once())->method('getId')->will($this->returnValue(1));
340  $this->productMock
341  ->expects($this->once())
342  ->method('getData')
343  ->with('tier_price')
344  ->will($this->returnValue([]));
345 
346  $this->groupRepositoryMock->expects($this->once())->method('getById')->will($this->returnValue($group));
347  $this->productMock->expects($this->once())->method('validate')->will(
348  $this->returnValue(
349  ['attr1' => '', 'attr2' => '']
350  )
351  );
352  $this->repositoryMock->expects($this->never())->method('save');
353  $this->service->add('product_sku', 1, 100, 2);
354  }
355 
360  {
361  $group = $this->createMock(\Magento\Customer\Model\Data\Group::class);
362  $group->expects($this->once())->method('getId')->will($this->returnValue(1));
363  $this->productMock
364  ->expects($this->once())
365  ->method('getData')
366  ->with('tier_price')
367  ->will($this->returnValue([]));
368 
369  $this->groupRepositoryMock->expects($this->once())->method('getById')->will($this->returnValue($group));
370  $this->repositoryMock->expects($this->once())->method('save')->will($this->throwException(new \Exception()));
371  $this->service->add('product_sku', 1, 100, 2);
372  }
373 
378  {
379  $group = $this->createMock(\Magento\Customer\Model\Data\Group::class);
380  $group->expects($this->once())
381  ->method('getId')
382  ->willReturn(1);
383  $this->productMock
384  ->expects($this->once())
385  ->method('getData')
386  ->with('tier_price')
387  ->will($this->returnValue([]));
388  $this->groupRepositoryMock->expects($this->once())
389  ->method('getById')
390  ->willReturn($group);
391  $this->repositoryMock->expects($this->once())
392  ->method('save')
393  ->willThrowException(new CouldNotSaveException(__('Lock wait timeout')));
394 
395  $this->service->add('product_sku', 1, 100, 2);
396  }
397 
404  public function testAddWithInvalidData($price, $qty)
405  {
406  $this->service->add('product_sku', 1, $price, $qty);
407  }
408 
412  public function addDataProvider()
413  {
414  return [
415  ['string', 10],
416  [10, '10string'],
417  [10, -15]
418  ];
419  }
420 }
testGetList($configValue, $customerGroupId, $groupData, $expected)
$group
Definition: sections.phtml:16
__()
Definition: __.php:13
$price