View source
<?php
namespace Drupal\Tests\commerce_cart_api\Kernel;
use Drupal\commerce_cart_api\Plugin\rest\resource\CartAddResource;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\Tests\commerce_order\Kernel\OrderKernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
final class CartAddResourceSelectStoreExceptionTest extends OrderKernelTestBase {
public static $modules = [
'serialization',
'commerce_product',
'commerce_cart',
'commerce_cart_api',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('commerce_product_variation');
EntityFormMode::create([
'id' => 'commerce_order_item.add_to_cart',
'label' => 'Add to cart',
'targetEntityType' => 'commerce_order_item',
])
->save();
$this
->installConfig([
'commerce_product',
]);
}
public function testNoStoresException() {
$product = Product::create([
'type' => 'default',
]);
$product_variation = ProductVariation::create([
'type' => 'default',
]);
$product_variation
->save();
$product
->addVariation($product_variation);
$product
->save();
$request = $this
->prophesize(Request::class);
$this
->expectException(UnprocessableEntityHttpException::class);
$this
->expectExceptionMessage('The given entity is not assigned to any store.');
$controller = $this
->getController();
$controller
->post([
[
'purchased_entity_type' => 'commerce_product_variation',
'purchased_entity_id' => $product_variation
->id(),
'quantity' => 1,
],
], $request
->reveal());
}
public function testNotCurrentStoreException() {
$additional_store1 = $this
->createStore();
$addiitonal_store2 = $this
->createStore();
$product = Product::create([
'type' => 'default',
'stores' => [
$addiitonal_store2
->id(),
$additional_store1
->id(),
],
]);
$product_variation = ProductVariation::create([
'type' => 'default',
]);
$product_variation
->save();
$product
->addVariation($product_variation);
$product
->save();
$request = $this
->prophesize(Request::class);
$this
->expectException(UnprocessableEntityHttpException::class);
$this
->expectExceptionMessage("The given entity can't be purchased from the current store.");
$controller = $this
->getController();
$controller
->post([
[
'purchased_entity_type' => 'commerce_product_variation',
'purchased_entity_id' => $product_variation
->id(),
'quantity' => 1,
],
], $request
->reveal());
}
protected function getController() {
return new CartAddResource([], 'cart_add_resource', [], [
'json',
], $this->container
->get('logger.channel.default'), $this->container
->get('commerce_cart.cart_provider'), $this->container
->get('commerce_cart.cart_manager'), $this->container
->get('entity_type.manager'), $this->container
->get('commerce_order.chain_order_type_resolver'), $this->container
->get('commerce_store.current_store'), $this->container
->get('commerce_price.chain_price_resolver'), $this->container
->get('current_user'), $this->container
->get('entity.repository'));
}
}