|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\commerce_order\Kernel; |
| 4 | + |
| 5 | +use Drupal\commerce_order\Entity\Order; |
| 6 | +use Drupal\commerce_order\Entity\OrderItem; |
| 7 | +use Drupal\commerce_order\Entity\OrderItemType; |
| 8 | +use Drupal\commerce_price\Price; |
| 9 | +use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests the price splitter. |
| 13 | + * |
| 14 | + * @coversDefaultClass \Drupal\commerce_order\PriceSplitter |
| 15 | + * |
| 16 | + * @group commerce |
| 17 | + */ |
| 18 | +class PriceSplitterTest extends CommerceKernelTestBase { |
| 19 | + |
| 20 | + /** |
| 21 | + * A sample order. |
| 22 | + * |
| 23 | + * @var \Drupal\commerce_order\Entity\OrderInterface |
| 24 | + */ |
| 25 | + protected $order; |
| 26 | + |
| 27 | + /** |
| 28 | + * The price splitter. |
| 29 | + * |
| 30 | + * @var \Drupal\commerce_order\PriceSplitterInterface |
| 31 | + */ |
| 32 | + protected $splitter; |
| 33 | + |
| 34 | + /** |
| 35 | + * Modules to enable. |
| 36 | + * |
| 37 | + * @var array |
| 38 | + */ |
| 39 | + public static $modules = [ |
| 40 | + 'entity_reference_revisions', |
| 41 | + 'path', |
| 42 | + 'profile', |
| 43 | + 'state_machine', |
| 44 | + 'commerce_product', |
| 45 | + 'commerce_order', |
| 46 | + ]; |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritdoc} |
| 50 | + */ |
| 51 | + protected function setUp() { |
| 52 | + parent::setUp(); |
| 53 | + |
| 54 | + $this->installEntitySchema('profile'); |
| 55 | + $this->installEntitySchema('commerce_order'); |
| 56 | + $this->installEntitySchema('commerce_order_item'); |
| 57 | + $this->installEntitySchema('commerce_product'); |
| 58 | + $this->installEntitySchema('commerce_product_variation'); |
| 59 | + $this->installConfig(['commerce_product', 'commerce_order']); |
| 60 | + $user = $this->createUser(['mail' => $this->randomString() . '@example.com']); |
| 61 | + |
| 62 | + OrderItemType::create([ |
| 63 | + 'id' => 'test', |
| 64 | + 'label' => 'Test', |
| 65 | + 'orderType' => 'default', |
| 66 | + ])->save(); |
| 67 | + |
| 68 | + $order = Order::create([ |
| 69 | + 'type' => 'default', |
| 70 | + 'state' => 'draft', |
| 71 | + 'mail' => $user->getEmail(), |
| 72 | + 'uid' => $user->id(), |
| 73 | + 'ip_address' => '127.0.0.1', |
| 74 | + 'order_number' => '6', |
| 75 | + 'store_id' => $this->store->id(), |
| 76 | + ]); |
| 77 | + $order->save(); |
| 78 | + $this->order = $this->reloadEntity($order); |
| 79 | + |
| 80 | + $this->splitter = $this->container->get('commerce_order.price_splitter'); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @covers ::split |
| 85 | + */ |
| 86 | + public function testSplit() { |
| 87 | + // 6 x 3 + 6 x 3 = 36. |
| 88 | + $amount = new Price('6', 'USD'); |
| 89 | + $order_items = $this->buildOrderItems([$amount, $amount]); |
| 90 | + $this->order->setItems($order_items); |
| 91 | + $this->order->save(); |
| 92 | + |
| 93 | + // Each order item should be discounted by half (9 USD). |
| 94 | + $amounts = $this->splitter->split($this->order, new Price('18', 'USD')); |
| 95 | + $expected_amount = new Price('9', 'USD'); |
| 96 | + $this->assertEquals([$expected_amount, $expected_amount], array_values($amounts)); |
| 97 | + |
| 98 | + // Same result with an explicit percentage. |
| 99 | + $amounts = $this->splitter->split($this->order, new Price('18', 'USD'), '0.5'); |
| 100 | + $expected_amount = new Price('9', 'USD'); |
| 101 | + $this->assertEquals([$expected_amount, $expected_amount], array_values($amounts)); |
| 102 | + |
| 103 | + // 9.99 x 3 + 1.01 x 3 = 33. |
| 104 | + $first_amount = new Price('9.99', 'USD'); |
| 105 | + $second_amount = new Price('1.01', 'USD'); |
| 106 | + $order_items = $this->buildOrderItems([$first_amount, $second_amount]); |
| 107 | + $this->order->setItems($order_items); |
| 108 | + $this->order->save(); |
| 109 | + |
| 110 | + $amount = new Price('5', 'USD'); |
| 111 | + $amounts = $this->splitter->split($this->order, $amount); |
| 112 | + $first_expected_amount = new Price('4.54', 'USD'); |
| 113 | + $second_expected_amount = new Price('0.46', 'USD'); |
| 114 | + $this->assertEquals($amount, $first_expected_amount->add($second_expected_amount)); |
| 115 | + $this->assertEquals([$first_expected_amount, $second_expected_amount], array_values($amounts)); |
| 116 | + |
| 117 | + // Split an amount that has a reminder. |
| 118 | + $amount = new Price('3.98', 'USD'); |
| 119 | + $amounts = $this->splitter->split($this->order, $amount); |
| 120 | + $first_expected_amount = new Price('3.62', 'USD'); |
| 121 | + $second_expected_amount = new Price('0.36', 'USD'); |
| 122 | + $this->assertEquals($amount, $first_expected_amount->add($second_expected_amount)); |
| 123 | + $this->assertEquals([$first_expected_amount, $second_expected_amount], array_values($amounts)); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Builds the order items for the given unit prices. |
| 128 | + * |
| 129 | + * @param \Drupal\commerce_price\Price[] $unit_prices |
| 130 | + * The unit prices. |
| 131 | + * |
| 132 | + * @return \Drupal\commerce_order\Entity\OrderItemInterface[] |
| 133 | + * The order items. |
| 134 | + */ |
| 135 | + protected function buildOrderItems(array $unit_prices) { |
| 136 | + $order_items = []; |
| 137 | + foreach ($unit_prices as $unit_price) { |
| 138 | + $order_item = OrderItem::create([ |
| 139 | + 'type' => 'test', |
| 140 | + 'unit_price' => $unit_price, |
| 141 | + 'quantity' => '3', |
| 142 | + ]); |
| 143 | + $order_item->save(); |
| 144 | + |
| 145 | + $order_items[] = $order_item; |
| 146 | + } |
| 147 | + |
| 148 | + return $order_items; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments