You are here

public function CartAddResourceTest::testMalformedPayload in Commerce Cart API 8

Tests malformed payloads.

File

tests/src/Functional/CartAddResourceTest.php, line 32

Class

CartAddResourceTest
Tests the cart add resource.

Namespace

Drupal\Tests\commerce_cart_api\Functional

Code

public function testMalformedPayload() {
  $url = Url::fromUri('base:cart/add');
  $url
    ->setOption('query', [
    '_format' => static::$format,
  ]);
  $request_options = $this
    ->getAuthenticationRequestOptions('POST');
  $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;

  // Missing purchasable entity type.
  $request_options[RequestOptions::BODY] = '[{ "purchased_entity_id": "1", "quantity": "1"}]';
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, 'You must specify a purchasable entity type for row: 0', $response);

  // Missing purchasable entity ID.
  $request_options[RequestOptions::BODY] = '[{ "purchased_entity_type": "commerce_product_variation", "quantity": "1"}]';
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, 'You must specify a purchasable entity ID for row: 0', $response);

  // Invalid purchasable entity type.
  $request_options[RequestOptions::BODY] = '[{ "purchased_entity_type": "invalid_type", "purchased_entity_id": "1", "quantity": "1"}]';
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, 'You must specify a valid purchasable entity type for row: 0', $response);
}