View source
<?php
namespace Drupal\Tests\commerce_order\Kernel;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_price\Price;
class PriceSplitterTest extends OrderKernelTestBase {
protected $order;
protected $splitter;
protected function setUp() : void {
parent::setUp();
$user = $this
->createUser([
'mail' => $this
->randomString() . '@example.com',
]);
$order = Order::create([
'type' => 'default',
'state' => 'draft',
'mail' => $user
->getEmail(),
'uid' => $user
->id(),
'ip_address' => '127.0.0.1',
'order_number' => '6',
'store_id' => $this->store
->id(),
]);
$order
->save();
$this->order = $this
->reloadEntity($order);
$this->splitter = $this->container
->get('commerce_order.price_splitter');
}
public function testEmptyOrder() {
$amounts = $this->splitter
->split($this->order, new Price('42', 'USD'), '50');
$this
->assertEquals([], $amounts);
$amounts = $this->splitter
->split($this->order, new Price('42', 'USD'));
$this
->assertEquals([], $amounts);
}
public function testSplit() {
$unit_price = new Price('6', 'USD');
$order_items = $this
->buildOrderItems([
$unit_price,
$unit_price,
], 3);
$this->order
->setItems($order_items);
$this->order
->save();
$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);
}
$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);
}
$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]);
$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]);
}
protected function buildOrderItems(array $unit_prices, $quantity = '1') {
$order_items = [];
foreach ($unit_prices as $unit_price) {
$order_item = OrderItem::create([
'type' => 'test',
'unit_price' => $unit_price,
'quantity' => $quantity,
]);
$order_item
->save();
$order_items[] = $order_item;
}
return $order_items;
}
}