protected function StockLevelTest::setUp in Commerce Stock 8
Overrides CommerceKernelTestBase::setUp
File
- modules/
field/ tests/ src/ Kernel/ StockLevelTest.php, line 77
Class
- StockLevelTest
- Ensure the stock level field works.
Namespace
Drupal\Tests\commerce_stock_field\KernelCode
protected function setUp() {
$this->fieldName = 'test_stock_level';
parent::setUp();
$this
->installEntitySchema('commerce_stock_location');
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('commerce_product_variation');
$this
->installConfig([
'commerce_product',
'commerce_stock',
'commerce_stock_local',
]);
$this
->installSchema('commerce_stock_local', [
'commerce_stock_transaction',
'commerce_stock_transaction_type',
'commerce_stock_location_level',
]);
$location = StockLocation::create([
'type' => 'default',
'name' => $this
->randomString(),
'status' => 1,
]);
$location
->save();
$configFactory = $this->container
->get('config.factory');
$config = $configFactory
->getEditable('commerce_stock.service_manager');
$config
->set('default_service_id', 'local_stock');
$config
->save();
$this->stockServiceManager = \Drupal::service('commerce_stock.service_manager');
$entity_type = 'commerce_product_variation';
$bundle = 'default';
$widget_id = 'commerce_stock_level_simple';
$this
->createStockLevelField($entity_type, $bundle, $widget_id);
$variation = ProductVariation::create([
'type' => 'default',
'sku' => 'TEST_' . strtolower($this
->randomMachineName()),
'status' => 1,
'price' => [
'number' => '12.00',
'currency_code' => 'USD',
],
]);
$variation
->save();
Product::create([
'type' => 'default',
'variations' => [
$variation,
],
'stores' => [
$this->store,
],
])
->save();
$this->variation = $this
->reloadEntity($variation);
$user = $this
->createUser([
'mail' => $this
->randomString() . '@example.com',
]);
$this->stockServiceManager
->createTransaction($this->variation, 1, '', 55, 0, 'EUR', StockTransactionsInterface::STOCK_IN);
$this->checker = $this->stockServiceManager
->getService($this->variation)
->getStockChecker();
$this->stockServiceConfiguration = $this->stockServiceManager
->getService($this->variation)
->getConfiguration();
$context = new Context($user, $this->store);
$this->locations = $this->stockServiceConfiguration
->getAvailabilityLocations($context, $this->variation);
}