You are here

public function PriceListRepositoryTest::testQuantity in Commerce Pricelist 8.2

Tests price list item selection based on the quantity, weight and status.

@covers ::loadItem @covers ::loadItems

File

tests/src/Kernel/PriceListRepositoryTest.php, line 238

Class

PriceListRepositoryTest
Tests the price list repository.

Namespace

Drupal\Tests\commerce_pricelist\Kernel

Code

public function testQuantity() {
  $context = new Context($this->user, $this->store);
  $repository = $this->container
    ->get('commerce_pricelist.repository');
  $this->priceListItem
    ->setQuantity(10);
  $this->priceListItem
    ->save();

  // Create a second price list with a smaller weight, which should be
  // selected instead of the first price list.
  $price_list = PriceList::create([
    'type' => 'commerce_product_variation',
    'stores' => [
      $this->store
        ->id(),
    ],
    'weight' => '-1',
  ]);
  $price_list
    ->save();

  // Create two price list items, to test quantity tier selection.
  $price_list_item = PriceListItem::create([
    'type' => 'commerce_product_variation',
    'price_list_id' => $price_list
      ->id(),
    'purchasable_entity' => $this->variation
      ->id(),
    'quantity' => '10',
    'price' => new Price('7.00', 'USD'),
  ]);
  $price_list_item
    ->save();
  $another_price_list_item = PriceListItem::create([
    'type' => 'commerce_product_variation',
    'price_list_id' => $price_list
      ->id(),
    'purchasable_entity' => $this->variation
      ->id(),
    'quantity' => '3',
    'price' => new Price('6.00', 'USD'),
  ]);
  $another_price_list_item
    ->save();
  $price_list_item = $repository
    ->loadItem($this->variation, 1, $context);
  $this
    ->assertEmpty($price_list_item);
  $price_list_item = $repository
    ->loadItem($this->variation, 15, $context);
  $this
    ->assertEquals(new Price('7.00', 'USD'), $price_list_item
    ->getPrice());

  // Reload the service to clear the static cache.
  $this->container
    ->set('commerce_pricelist.repository', NULL);
  $repository = $this->container
    ->get('commerce_pricelist.repository');

  // Confirm that disabled price list items are skipped.
  $price_list_item
    ->setEnabled(FALSE);
  $price_list_item
    ->save();
  $price_list_item = $repository
    ->loadItem($this->variation, 15, $context);
  $this
    ->assertEquals(new Price('6.00', 'USD'), $price_list_item
    ->getPrice());

  // Reload the repository to clear the static cache.
  $this->container
    ->set('commerce_pricelist.repository', NULL);
  $repository = $this->container
    ->get('commerce_pricelist.repository');

  // Confirm that disabled price lists are skipped.
  $price_list
    ->setEnabled(FALSE);
  $price_list
    ->save();
  $another_user = $this
    ->createUser();
  $context = new Context($another_user, $this->store);
  $price_list_item = $repository
    ->loadItem($this->variation, 15, $context);
  $this
    ->assertEquals(new Price('5.00', 'USD'), $price_list_item
    ->getPrice());
}