You are here

public function CartCouponsResourceTest::testApplyCoupons in Commerce Cart API 8

Tests applying a valid coupon.

File

tests/src/Functional/CartCouponsResourceTest.php, line 44

Class

CartCouponsResourceTest
Tests the cart coupons resource.

Namespace

Drupal\Tests\commerce_cart_api\Functional

Code

public function testApplyCoupons() {
  $promotion = Promotion::create([
    'order_types' => [
      'default',
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'usage_limit' => 1,
    'start_date' => '2017-01-01',
    'status' => TRUE,
    'offer' => [
      'target_plugin_id' => 'order_item_percentage_off',
      'target_plugin_configuration' => [
        'percentage' => '0.5',
      ],
    ],
  ]);
  $promotion
    ->save();
  $coupon = Coupon::create([
    'promotion_id' => $promotion
      ->id(),
    'code' => 'PERCENTAGE_OFF',
    'usage_limit' => 1,
    'status' => TRUE,
  ]);
  $coupon
    ->save();

  // Add a cart that does belong to the account.
  $cart = $this->container
    ->get('commerce_cart.cart_provider')
    ->createCart('default', $this->store, $this->account);
  $this
    ->assertInstanceOf(OrderInterface::class, $cart);

  // Add order item to the cart.
  $this->cartManager
    ->addEntity($cart, $this->variation, 2);
  $this
    ->assertEquals(count($cart
    ->getItems()), 1);
  $url = Url::fromUri("base:cart/{$cart->id()}/coupons");
  $url
    ->setOption('query', [
    '_format' => static::$format,
  ]);
  $request_options = $this
    ->getAuthenticationRequestOptions('PATCH');
  $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
  $request_options[RequestOptions::BODY] = sprintf('{ "coupon_code": "%s"}', $coupon
    ->getCode());
  $response = $this
    ->request('PATCH', $url, $request_options);
  $this
    ->assertResourceResponse(200, FALSE, $response);
  $response_body = Json::decode((string) $response
    ->getBody());
  $this
    ->assertEquals([
    $coupon
      ->id(),
  ], $response_body['coupons']);
  $order_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_order');
  $order_storage
    ->resetCache();
  $cart = $order_storage
    ->load($cart
    ->id());
  $this
    ->assertFalse($cart
    ->get('coupons')
    ->isEmpty());
}