public function PriceSplitterTest::testSplit in Commerce Core 8.2
@covers ::split
File
- modules/
order/ tests/ src/ Kernel/ PriceSplitterTest.php, line 69
Class
- PriceSplitterTest
- Tests the price splitter.
Namespace
Drupal\Tests\commerce_order\KernelCode
public function testSplit() {
// 6 x 3 + 6 x 3 = 36.
$unit_price = new Price('6', 'USD');
$order_items = $this
->buildOrderItems([
$unit_price,
$unit_price,
], 3);
$this->order
->setItems($order_items);
$this->order
->save();
// Each order item should be discounted by half (9 USD).
$amounts = $this->splitter
->split($this->order, new Price('18', 'USD'));
$expected_amount = new Price('9', 'USD');
foreach ($amounts as $amount) {
$this
->assertEquals($expected_amount, $amount);
}
// Same result with an explicit percentage.
$amounts = $this->splitter
->split($this->order, new Price('18', 'USD'), '0.5');
$expected_amount = new Price('9', 'USD');
foreach ($amounts as $amount) {
$this
->assertEquals($expected_amount, $amount);
}
// 9.99 x 3 + 1.01 x 3 = 33.
$first_unit_price = new Price('9.99', 'USD');
$second_unit_price = new Price('1.01', 'USD');
$order_items = $this
->buildOrderItems([
$first_unit_price,
$second_unit_price,
], 3);
$this->order
->setItems($order_items);
$this->order
->save();
$amount = new Price('5', 'USD');
$amounts = $this->splitter
->split($this->order, $amount);
$first_expected_amount = new Price('4.54', 'USD');
$second_expected_amount = new Price('0.46', 'USD');
$this
->assertEquals($first_expected_amount
->add($second_expected_amount), $amount);
$amounts = array_values($amounts);
$this
->assertEquals($first_expected_amount, $amounts[0]);
$this
->assertEquals($second_expected_amount, $amounts[1]);
// Split an amount that has a reminder.
$unit_price = new Price('69.99', 'USD');
$order_items = $this
->buildOrderItems([
$unit_price,
$unit_price,
]);
$this->order
->setItems($order_items);
$this->order
->save();
$amount = new Price('41.99', 'USD');
$amounts = $this->splitter
->split($this->order, $amount, '0.3');
$first_expected_amount = new Price('21.00', 'USD');
$second_expected_amount = new Price('20.99', 'USD');
$this
->assertEquals($first_expected_amount
->add($second_expected_amount), $amount);
$amounts = array_values($amounts);
$this
->assertEquals($first_expected_amount, $amounts[0]);
$this
->assertEquals($second_expected_amount, $amounts[1]);
}