Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductPriceCalculatorTest.php
Go to the documentation of this file.
1 <?php
8 
9 class ProductPriceCalculatorTest extends \PHPUnit\Framework\TestCase
10 {
14  private $model;
15 
19  private $priceCurrencyMock;
20 
21  protected function setUp()
22  {
23  $this->priceCurrencyMock = $this->getMockBuilder(\Magento\Framework\Pricing\PriceCurrencyInterface::class)
24  ->disableOriginalConstructor()
25  ->getMock();
26  $this->model = new \Magento\CatalogRule\Model\Indexer\ProductPriceCalculator($this->priceCurrencyMock);
27  }
28 
29  public function testCalculateToFixedPrice()
30  {
31  $rulePrice = 100;
32  $actionAmount = 50;
33  $ruleData = [
34  'action_operator' => 'to_fixed',
35  'action_amount' => $actionAmount
36  ];
37  $productData = ['rule_price' => $rulePrice];
38 
39  $this->priceCurrencyMock->expects($this->once())
40  ->method('round')
41  ->with($actionAmount)
42  ->willReturn($actionAmount);
43 
44  $this->assertEquals($actionAmount, $this->model->calculate($ruleData, $productData));
45  }
46 
47  public function testCalculateToPercentPrice()
48  {
49  $rulePrice = 200;
50  $actionAmount = 50;
51  $expectedPrice = 100;
52  $ruleData = [
53  'action_operator' => 'to_percent',
54  'action_amount' => $actionAmount
55  ];
56  $productData = ['rule_price' => $rulePrice];
57 
58  $this->priceCurrencyMock->expects($this->once())
59  ->method('round')
60  ->with($expectedPrice)
61  ->willReturn($expectedPrice);
62 
63  $this->assertEquals($expectedPrice, $this->model->calculate($ruleData, $productData));
64  }
65 
66  public function testCalculateByFixedPrice()
67  {
68  $rulePrice = 200;
69  $actionAmount = 50;
70  $expectedPrice = 150;
71  $ruleData = [
72  'action_operator' => 'by_fixed',
73  'action_amount' => $actionAmount
74  ];
75  $productData = ['rule_price' => $rulePrice];
76 
77  $this->priceCurrencyMock->expects($this->once())
78  ->method('round')
79  ->with($expectedPrice)
80  ->willReturn($expectedPrice);
81 
82  $this->assertEquals($expectedPrice, $this->model->calculate($ruleData, $productData));
83  }
84 
85  public function testCalculateByPercentPrice()
86  {
87  $rulePrice = 200;
88  $actionAmount = 50;
89  $expectedPrice = 100;
90  $ruleData = [
91  'action_operator' => 'by_percent',
92  'action_amount' => $actionAmount
93  ];
94  $productData = ['rule_price' => $rulePrice];
95 
96  $this->priceCurrencyMock->expects($this->once())
97  ->method('round')
98  ->with($expectedPrice)
99  ->willReturn($expectedPrice);
100 
101  $this->assertEquals($expectedPrice, $this->model->calculate($ruleData, $productData));
102  }
103 }
$ruleData
Definition: tax_rule.php:26
$productData