You are here

public function OrderItemMatcherTest::testOrderItemMatcher in Commerce Core 8.2

Tests the order item matcher.

@covers ::matchAll @covers ::match

File

modules/cart/tests/src/Kernel/OrderItemMatcherTest.php, line 80

Class

OrderItemMatcherTest
Tests the order item matcher.

Namespace

Drupal\Tests\commerce_cart\Kernel

Code

public function testOrderItemMatcher() {
  $order_item1 = OrderItem::create([
    'type' => 'default',
    'quantity' => 2,
    'unit_price' => new Price('12.00', 'USD'),
    'purchased_entity' => $this->variation1,
  ]);
  $order_item1
    ->save();
  $order_item2 = OrderItem::create([
    'type' => 'default',
    'quantity' => 6,
    'unit_price' => new Price('12.00', 'USD'),
    'purchased_entity' => $this->variation1,
  ]);
  $order_item2
    ->save();
  $order_item3 = OrderItem::create([
    'type' => 'default',
    'quantity' => 6,
    'unit_price' => new Price('12.00', 'USD'),
    'purchased_entity' => $this->variation2,
  ]);
  $order_item3
    ->save();

  // Order item should match just the second item.
  $matches = $this->orderItemMatcher
    ->matchAll($order_item1, [
    $order_item2,
    $order_item3,
  ]);
  $this
    ->assertNotEmpty($matches);
  $this
    ->assertEquals($matches, [
    $order_item2,
  ]);

  // First matching item should be returned.
  $match = $this->orderItemMatcher
    ->match($order_item1, [
    $order_item2,
    $order_item3,
  ]);
  $this
    ->assertNotEmpty($match);
  $this
    ->assertEquals($match, $order_item2);

  // Third order item should not match first two items.
  $matches = $this->orderItemMatcher
    ->matchAll($order_item3, [
    $order_item1,
    $order_item2,
  ]);
  $this
    ->assertEmpty($matches);

  // If first item doesn't match, the second should be returned.
  $match = $this->orderItemMatcher
    ->match($order_item1, [
    $order_item3,
    $order_item2,
  ]);
  $this
    ->assertNotEmpty($match);
  $this
    ->assertEquals($match, $order_item2);

  // Order item with same purchased entity, different type does not match.
  $order_item4 = OrderItem::create([
    'type' => 'test',
    'quantity' => 2,
    'unit_price' => new Price('1.00', 'USD'),
    'purchased_entity' => $this->variation1,
  ]);
  $order_item4
    ->save();
  $matches = $this->orderItemMatcher
    ->matchAll($order_item4, [
    $order_item1,
    $order_item2,
    $order_item3,
  ]);
  $this
    ->assertEmpty($matches);
}