Skip to content

Commit

Permalink
[API] Test POST action when adding new items to cart
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed Sep 17, 2021
1 parent 27d0121 commit d8c3511
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/Sylius/Behat/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static function customItemAction(?string $section, string $resource, stri
return new self(
sprintf('/api/v2/%s%s/%s/%s', self::prepareSection($section), $resource, $id, $action),
$type,
['CONTENT_TYPE' => 'application/merge-patch+json']
['CONTENT_TYPE' => self::resolveHttpMethod($type)]
);
}

Expand All @@ -168,7 +168,7 @@ public static function upload(

public static function custom(string $url, string $method, ?string $token = null): RequestInterface
{
$headers = ['CONTENT_TYPE' => 'application/merge-patch+json'];
$headers = ['CONTENT_TYPE' => self::resolveHttpMethod($method)];
if ($token !== null) {
$headers['HTTP_Authorization'] = 'Bearer ' . $token;
}
Expand Down Expand Up @@ -277,4 +277,9 @@ private static function prepareSection(?string $section): string

return $section . '/';
}

private static function resolveHttpMethod(string $method): string
{
return $method === HttpRequest::METHOD_PATCH ? 'application/merge-patch+json' : 'application/json';
}
}
4 changes: 2 additions & 2 deletions src/Sylius/Behat/Context/Api/Shop/CartContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function iShouldBeNotifiedThatTheProductHasBeenSuccessfullyAdded(): void
{
$response = $this->cartsClient->getLastResponse();
Assert::true(
$this->responseChecker->isUpdateSuccessful($response),
$this->responseChecker->isCreationSuccessful($response),
SprintfResponseEscaper::provideMessageWithEscapedResponseContent('Item has not been added.', $response)
);
}
Expand All @@ -379,7 +379,7 @@ public function iShouldBeNotifiedThatQuantityOfAddedProductCannotBeLowerThan1():
{
$response = $this->cartsClient->getLastResponse();
Assert::false(
$this->responseChecker->isUpdateSuccessful($response),
$this->responseChecker->isCreationSuccessful($response),
SprintfResponseEscaper::provideMessageWithEscapedResponseContent('Quantity of an order item cannot be lower than 1.', $response)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -1361,7 +1361,7 @@ private function putVariantToCart(ProductVariantInterface $productVariant, strin
'shop',
'orders',
$tokenValue,
HTTPRequest::METHOD_PATCH,
HTTPRequest::METHOD_POST,
'items'
);

Expand Down
48 changes: 48 additions & 0 deletions tests/Api/Responses/Expected/shop/add_item_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"@context": "\/api\/v2\/contexts\/Order",
"@id": "\/api\/v2\/shop\/orders\/nAWw2jewpA",
"@type": "Order",
"payments": [
{
"@id": "\/api\/v2\/shop\/payments\/@integer@",
"@type": "Payment",
"id": @integer@,
"method": "\/api\/v2\/shop\/payment-methods\/CASH_ON_DELIVERY"
}
],
"shipments": [
{
"@id": "\/api\/v2\/shop\/shipments\/@integer@",
"@type": "Shipment",
"id": @integer@,
"method": "\/api\/v2\/shop\/shipping-methods\/UPS"
}
],
"currencyCode": "USD",
"localeCode": "en_US",
"checkoutState": "cart",
"paymentState": "cart",
"shippingState": "cart",
"tokenValue": "nAWw2jewpA",
"id": @integer@,
"items": [
{
"@id": "\/api\/v2\/shop\/order-items\/@integer@",
"@type": "OrderItem",
"variant": "\/api\/v2\/shop\/product-variants\/MUG_BLUE",
"productName": "Mug",
"id": @integer@,
"quantity": 3,
"unitPrice": 2000,
"total": 6000,
"discountedUnitPrice": 2000,
"subtotal": 6000,
"originalPrice": null
}
],
"itemsTotal": 6000,
"total": 6500,
"taxTotal": 0,
"shippingTotal": 500,
"orderPromotionTotal": 0
}
23 changes: 23 additions & 0 deletions tests/Api/Shop/OrdersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ public function it_gets_an_order(): void
$this->assertResponse($response, 'shop/get_order_response', Response::HTTP_OK);
}

/** @test */
public function it_allows_to_add_items_to_order(): void
{
$this->loadFixturesFromFiles(['channel.yaml', 'cart.yaml', 'country.yaml', 'shipping_method.yaml', 'payment_method.yaml']);

$tokenValue = 'nAWw2jewpA';

/** @var MessageBusInterface $commandBus */
$commandBus = $this->get('sylius.command_bus');

$pickupCartCommand = new PickupCart($tokenValue, 'en_US');
$pickupCartCommand->setChannelCode('WEB');
$commandBus->dispatch($pickupCartCommand);

$this->client->request('POST', '/api/v2/shop/orders/nAWw2jewpA/items', [], [], self::CONTENT_TYPE_HEADER, json_encode([
'productVariant' => '/api/v2/shop/product-variants/MUG_BLUE',
'quantity' => 3,
]));
$response = $this->client->getResponse();

$this->assertResponse($response, 'shop/add_item_response', Response::HTTP_CREATED);
}

/** @test */
public function it_does_not_get_orders_collection_for_guest(): void
{
Expand Down

0 comments on commit d8c3511

Please sign in to comment.