You are here

public function InventoryTest::testMultipleAssetInventory in farmOS 2.x

Test multiple asset inventories with measure+units pairs.

File

modules/core/inventory/tests/src/Kernel/InventoryTest.php, line 216

Class

InventoryTest
Tests for farmOS inventory logic.

Namespace

Drupal\Tests\farm_inventory\Kernel

Code

public function testMultipleAssetInventory() {

  // Create a new asset.

  /** @var \Drupal\asset\Entity\AssetInterface $asset */
  $asset = Asset::create([
    'type' => 'container',
    'name' => $this
      ->randomMachineName(),
    'status' => 'active',
  ]);
  $asset
    ->save();

  // Create two units terms.

  /** @var \Drupal\taxonomy\Entity\Term $unit1 */
  $unit1 = Term::create([
    'name' => 'liters',
    'vid' => 'unit',
  ]);
  $unit1
    ->save();

  /** @var \Drupal\taxonomy\Entity\Term $unit2 */
  $unit2 = Term::create([
    'name' => 'hours',
    'vid' => 'unit',
  ]);
  $unit2
    ->save();

  // Reset the asset's volume (liters) inventory to 1.
  $this
    ->adjustInventory($asset, 'reset', '1', 'volume', $unit1
    ->id());

  // Confirm that the asset has one inventory, with a measure of "volume",
  // units in "liters", and a value of 1.
  $inventory = $this->assetInventory
    ->getInventory($asset);
  $this
    ->assertEquals(1, count($inventory), 'Asset has a single inventory.');
  $this
    ->assertEquals('volume', $inventory[0]['measure'], 'Asset inventory has a measure of volume.');
  $this
    ->assertEquals('liters', $inventory[0]['units'], 'Asset inventory has units in liters.');
  $this
    ->assertEquals('1', $inventory[0]['value'], 'Asset inventory is 1.');

  // Reset the asset's time (hours) inventory to 2.
  $this
    ->adjustInventory($asset, 'reset', '2', 'time', $unit2
    ->id());

  // Confirm that the asset now has two inventories.
  $inventory = $this->assetInventory
    ->getInventory($asset);
  $this
    ->assertEquals(2, count($inventory), 'Asset has a single inventory.');

  // Load the time (hours) inventory and confirm that it is 2.
  $inventory = $this->assetInventory
    ->getInventory($asset, 'time', $unit2
    ->id());
  $this
    ->assertEquals('time', $inventory[0]['measure'], 'Asset inventory has a measure of time.');
  $this
    ->assertEquals('hours', $inventory[0]['units'], 'Asset inventory has units in hours.');
  $this
    ->assertEquals('2', $inventory[0]['value'], 'Asset inventory is 2.');

  // Load the volume (liters) inventory and confirm that it is still 1.
  $inventory = $this->assetInventory
    ->getInventory($asset, 'volume', $unit1
    ->id());
  $this
    ->assertEquals('volume', $inventory[0]['measure'], 'Asset inventory has a measure of volume.');
  $this
    ->assertEquals('liters', $inventory[0]['units'], 'Asset inventory has units in liters.');
  $this
    ->assertEquals('1', $inventory[0]['value'], 'Asset inventory is 1.');

  // Load all volume inventories (without specifying units) and confirm that
  // one inventory is returned.
  $inventory = $this->assetInventory
    ->getInventory($asset, 'volume', 0);
  $this
    ->assertEquals(1, count($inventory), 'Asset has a single volume inventory.');

  // Load all liters inventories (without specifying measure) and confirm that
  // one inventory is returned.
  $inventory = $this->assetInventory
    ->getInventory($asset, '', $unit2
    ->id());
  $this
    ->assertEquals(1, count($inventory), 'Asset has a single liters inventory.');

  // Test incrementing the volume (liters) inventory.
  $this
    ->adjustInventory($asset, 'increment', '4', 'volume', $unit1
    ->id());
  $inventory = $this->assetInventory
    ->getInventory($asset, 'volume', $unit1
    ->id());
  $this
    ->assertEquals('5', $inventory[0]['value'], 'Asset inventory is 5.');
}