You are here

protected function OutOfStockTest::createProduct in Commerce Stock 8

Create a product with stock for testing.

1 call to OutOfStockTest::createProduct()
OutOfStockTest::setUp in modules/enforcement/tests/src/Functional/OutOfStockTest.php

File

modules/enforcement/tests/src/Functional/OutOfStockTest.php, line 51

Class

OutOfStockTest
Tests out of stock functionality.

Namespace

Drupal\Tests\commerce_stock_enforcement\Functional

Code

protected function createProduct() {
  $entity_type = 'commerce_product_variation';
  $bundle = 'default';
  $entity_manager = \Drupal::entityTypeManager();
  $entity_manager
    ->clearCachedDefinitions();
  $field_name = 'field_stock_level_test';

  /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
  FieldStorageConfig::create([
    'field_name' => $field_name,
    'type' => 'commerce_stock_level',
    'entity_type' => $entity_type,
  ])
    ->save();
  FieldConfig::create([
    'entity_type' => $entity_type,
    'field_name' => $field_name,
    'bundle' => $bundle,
    'label' => 'StockLevel',
  ])
    ->save();
  $this->product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
  ]);
  $this->variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'default',
    'product_id' => $this->product
      ->id(),
    'sku' => strtolower($this
      ->randomMachineName()),
    'status' => 1,
    'price' => [
      'number' => '12.12',
      'currency_code' => 'USD',
    ],
  ]);
  $stockServiceConfiguration = $this->stockServiceManager
    ->getService($this->variation)
    ->getConfiguration();
  $context = new Context($this->adminUser, $this->store);
  $this->locations = $stockServiceConfiguration
    ->getAvailabilityLocations($context, $this->variation);

  // Set initial Stock level.
  $this->stockServiceManager
    ->createTransaction($this->variation, $this->locations[1]
    ->getId(), '', 1, 12.12, 'USD', StockTransactionsInterface::STOCK_IN, []);
}