CartOrderPlacedTest.php in Commerce Core 8.2
File
modules/cart/tests/src/Kernel/CartOrderPlacedTest.php
View source
<?php
namespace Drupal\Tests\commerce_cart\Kernel;
use Drupal\Component\Render\FormattableMarkup;
class CartOrderPlacedTest extends CartKernelTestBase {
protected $variation;
protected function setUp() : void {
parent::setUp();
$this
->createUser();
$this->variation = $this
->createEntity('commerce_product_variation', [
'type' => 'default',
'sku' => $this
->randomMachineName(),
'price' => [
'number' => 999,
'currency_code' => 'USD',
],
]);
$this
->createEntity('commerce_product', [
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => [
$this->variation,
],
]);
$this
->reloadEntity($this->variation);
$this->variation
->save();
$this->user = $this
->createUser();
}
public function testCartOrderPlaced() {
$this->store = $this
->createStore();
$cart_order = $this->container
->get('commerce_cart.cart_provider')
->createCart('default', $this->store, $this->user);
$this->cartManager = $this->container
->get('commerce_cart.cart_manager');
$this->cartManager
->addEntity($cart_order, $this->variation);
$this
->assertNotEmpty($cart_order->cart->value);
$cart_order
->getState()
->applyTransitionById('place');
$cart_order
->save();
$cart_order = $this
->reloadEntity($cart_order);
$this
->assertEmpty($cart_order->cart->value);
$new_cart_order = $this->container
->get('commerce_cart.cart_provider')
->createCart('default', $this->store, $this->user);
$this
->assertNotEquals($cart_order
->id(), $new_cart_order
->id());
}
protected function createEntity($entity_type, array $values) {
$storage = $this->container
->get('entity_type.manager')
->getStorage($entity_type);
$entity = $storage
->create($values);
$status = $entity
->save();
$this
->assertEquals(SAVED_NEW, $status, new FormattableMarkup('Created %label entity %type.', [
'%label' => $entity
->getEntityType()
->getLabel(),
'%type' => $entity
->id(),
]));
$entity = $storage
->load($entity
->id());
return $entity;
}
}