Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BundleRegularPriceTest.php
Go to the documentation of this file.
1 <?php
8 
12 
13 class BundleRegularPriceTest extends \PHPUnit\Framework\TestCase
14 {
16  protected $regularPrice;
17 
20 
23 
26 
28  protected $priceInfoMock;
29 
32 
36  protected $quantity = 1;
37 
41  protected $priceCurrencyMock;
42 
46  protected function setUp()
47  {
48  $this->saleableInterfaceMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
49  ->disableOriginalConstructor()
50  ->setMethods(['getPriceInfo', 'getPriceType', 'getPrice'])
51  ->getMock();
52  $this->bundleCalculatorMock = $this->createMock(
53  \Magento\Bundle\Pricing\Adjustment\BundleCalculatorInterface::class
54  );
55 
56  $this->priceInfoMock = $this->createMock(\Magento\Framework\Pricing\PriceInfo\Base::class);
57 
58  $this->customOptionPriceMock = $this->getMockBuilder(\Magento\Catalog\Pricing\Price\CustomOptionPrice::class)
59  ->disableOriginalConstructor()
60  ->getMock();
61 
62  $this->saleableInterfaceMock->expects($this->once())
63  ->method('getPriceInfo')
64  ->will($this->returnValue($this->priceInfoMock));
65 
66  $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
67 
68  $this->objectManagerHelper = new ObjectManagerHelper($this);
69  $this->regularPrice = new \Magento\Bundle\Pricing\Price\BundleRegularPrice(
70  $this->saleableInterfaceMock,
71  $this->quantity,
72  $this->bundleCalculatorMock,
73  $this->priceCurrencyMock
74  );
75  }
76 
77  public function testGetAmount()
78  {
79  $expectedResult = 5;
80 
81  $this->saleableInterfaceMock->expects($this->once())
82  ->method('getPrice')
83  ->will($this->returnValue($expectedResult));
84 
85  $this->bundleCalculatorMock->expects($this->once())
86  ->method('getMinRegularAmount')
87  ->with($expectedResult, $this->saleableInterfaceMock)
88  ->will($this->returnValue($expectedResult));
89 
90  $this->priceCurrencyMock->expects($this->once())
91  ->method('convertAndRound')
92  ->will($this->returnArgument(0));
93 
94  $result = $this->regularPrice->getAmount();
95  $this->assertEquals($expectedResult, $result, 'Incorrect amount');
96 
97  //Calling a second time, should use cached value
98  $result = $this->regularPrice->getAmount();
99  $this->assertEquals($expectedResult, $result, 'Incorrect amount');
100  }
101 
102  public function testGetMaximalPrice()
103  {
104  $expectedResult = 5;
105 
106  $this->saleableInterfaceMock->expects($this->once())
107  ->method('getPrice')
108  ->will($this->returnValue($expectedResult));
109 
110  $this->bundleCalculatorMock->expects($this->once())
111  ->method('getMaxRegularAmount')
112  ->with($expectedResult, $this->saleableInterfaceMock)
113  ->will($this->returnValue($expectedResult));
114 
115  $this->priceCurrencyMock->expects($this->once())
116  ->method('convertAndRound')
117  ->will($this->returnArgument(0));
118 
119  $result = $this->regularPrice->getMaximalPrice();
120  $this->assertEquals($expectedResult, $result, 'Incorrect amount');
121 
122  //Calling a second time, should use cached value
123  $result = $this->regularPrice->getMaximalPrice();
124  $this->assertEquals($expectedResult, $result, 'Incorrect amount the second time');
125  }
126 
128  {
129  $price = 5;
130  $maxOptionPrice = 2;
131 
132  $expectedPrice = $price + $maxOptionPrice;
133 
134  $this->priceInfoMock->expects($this->atLeastOnce())
135  ->method('getPrice')
137  ->willReturn($this->customOptionPriceMock);
138 
139  $this->customOptionPriceMock->expects($this->once())
140  ->method('getCustomOptionRange')
141  ->with(false)
142  ->willReturn($maxOptionPrice);
143 
144  $this->saleableInterfaceMock->expects($this->once())
145  ->method('getPriceType')
146  ->willReturn(Price::PRICE_TYPE_FIXED);
147 
148  $this->saleableInterfaceMock->expects($this->once())
149  ->method('getPrice')
150  ->will($this->returnValue($price));
151 
152  $this->bundleCalculatorMock->expects($this->once())
153  ->method('getMaxRegularAmount')
154  ->with($expectedPrice, $this->saleableInterfaceMock)
155  ->will($this->returnValue($expectedPrice));
156 
157  $this->priceCurrencyMock->expects($this->once())
158  ->method('convertAndRound')
159  ->will($this->returnArgument(0));
160 
161  $result = $this->regularPrice->getMaximalPrice();
162  $this->assertEquals($expectedPrice, $result, 'Incorrect amount');
163 
164  //Calling a second time, should use cached value
165  $result = $this->regularPrice->getMaximalPrice();
166  $this->assertEquals($expectedPrice, $result, 'Incorrect amount the second time');
167  }
168 
169  public function testGetMinimalPrice()
170  {
171  $expectedResult = 5;
172 
173  $this->saleableInterfaceMock->expects($this->once())
174  ->method('getPrice')
175  ->will($this->returnValue($expectedResult));
176 
177  $this->priceCurrencyMock->expects($this->once())
178  ->method('convertAndRound')
179  ->will($this->returnArgument(0));
180 
181  $this->bundleCalculatorMock->expects($this->once())
182  ->method('getMinRegularAmount')
183  ->with($expectedResult, $this->saleableInterfaceMock)
184  ->will($this->returnValue($expectedResult));
185 
186  $result = $this->regularPrice->getMinimalPrice();
187  $this->assertEquals($expectedResult, $result, 'Incorrect amount');
188 
189  //Calling a second time, should use cached value
190  $result = $this->regularPrice->getMinimalPrice();
191  $this->assertEquals($expectedResult, $result, 'Incorrect amount the second time');
192  }
193 
195  {
196  $price = 5;
197  $minOptionPrice = 1;
198  $expectedValue = $price + $minOptionPrice;
199 
200  $this->saleableInterfaceMock->expects($this->once())
201  ->method('getPrice')
202  ->will($this->returnValue($price));
203 
204  $this->saleableInterfaceMock->expects($this->once())
205  ->method('getPriceType')
206  ->willReturn(Price::PRICE_TYPE_FIXED);
207 
208  $this->priceInfoMock->expects($this->atLeastOnce())
209  ->method('getPrice')
211  ->willReturn($this->customOptionPriceMock);
212 
213  $this->customOptionPriceMock->expects($this->once())
214  ->method('getCustomOptionRange')
215  ->with(true)
216  ->willReturn($minOptionPrice);
217 
218  $this->priceCurrencyMock->expects($this->once())
219  ->method('convertAndRound')
220  ->will($this->returnArgument(0));
221 
222  $this->bundleCalculatorMock->expects($this->once())
223  ->method('getMinRegularAmount')
224  ->with($expectedValue, $this->saleableInterfaceMock)
225  ->will($this->returnValue($expectedValue));
226 
227  $result = $this->regularPrice->getMinimalPrice();
228  $this->assertEquals($expectedValue, $result, 'Incorrect amount');
229 
230  //Calling a second time, should use cached value
231  $result = $this->regularPrice->getMinimalPrice();
232  $this->assertEquals($expectedValue, $result, 'Incorrect amount the second time');
233  }
234 }
$price