Skip to content

Commit

Permalink
[Tests] Fix placed order creation in test env
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel committed May 10, 2019
1 parent cb73260 commit cc8d716
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 126 deletions.
15 changes: 9 additions & 6 deletions tests/Controller/Cart/CartSummarizeApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

namespace Tests\Sylius\ShopApiPlugin\Controller\Cart;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\ShopApiPlugin\Command\Cart\AddCoupon;
use Sylius\ShopApiPlugin\Command\Cart\PickupCart;
use Sylius\ShopApiPlugin\Command\Cart\PutSimpleItemToCart;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase;
use Tests\Sylius\ShopApiPlugin\Controller\Utils\OrderPlacerTrait;
use Tests\Sylius\ShopApiPlugin\Controller\Utils\ShopUserLoginTrait;

final class CartSummarizeApiTest extends JsonApiTestCase
{
use ShopUserLoginTrait;
use OrderPlacerTrait;

/**
* @test
Expand Down Expand Up @@ -54,13 +55,15 @@ public function it_returns_not_found_exception_if_cart_has_not_been_found(): voi
*/
public function it_returns_not_found_exception_if_order_is_not_in_state_cart(): void
{
$fixtures = $this->loadFixturesFromFiles(['customer.yml', 'country.yml', 'address.yml', 'shop.yml', 'payment.yml', 'shipping.yml', 'order.yml']);
$this->logInUser('oliver@queen.com', '123password');
$this->loadFixturesFromFiles(['customer.yml', 'country.yml', 'address.yml', 'shop.yml', 'payment.yml', 'shipping.yml']);
$token = 'SDAOSLEFNWU35H3QLI5325';
$email = 'oliver@queen.com';

$this->logInUser($email, '123password');

/** @var OrderInterface $placedOrder */
$placedOrder = $fixtures['placed_order'];
$this->placeOrderForCustomerWithEmail($email, $token);

$this->client->request('GET', '/shop-api/WEB_GB/carts/' . $placedOrder->getTokenValue(), [], [], self::CONTENT_TYPE_HEADER);
$this->client->request('GET', '/shop-api/WEB_GB/carts/' . $token, [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();

$this->assertResponse($response, 'cart/cart_has_not_been_found_response', Response::HTTP_NOT_FOUND);
Expand Down
15 changes: 9 additions & 6 deletions tests/Controller/Order/OrderShowApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@

namespace Tests\Sylius\ShopApiPlugin\Controller\Order;

use Sylius\Component\Core\Model\OrderInterface;
use Symfony\Component\HttpFoundation\Response;
use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase;
use Tests\Sylius\ShopApiPlugin\Controller\Utils\OrderPlacerTrait;
use Tests\Sylius\ShopApiPlugin\Controller\Utils\ShopUserLoginTrait;

final class OrderShowApiTest extends JsonApiTestCase
{
use ShopUserLoginTrait;
use OrderPlacerTrait;

/**
* @test
*/
public function it_shows_details_of_placed_order_of_logged_in_customer(): void
{
$fixtures = $this->loadFixturesFromFiles(['customer.yml', 'country.yml', 'address.yml', 'shop.yml', 'payment.yml', 'shipping.yml', 'order.yml']);
$this->logInUser('oliver@queen.com', '123password');
$this->loadFixturesFromFiles(['customer.yml', 'country.yml', 'address.yml', 'shop.yml', 'payment.yml', 'shipping.yml']);
$email = 'oliver@queen.com';
$token = 'SDAOSLEFNWU35H3QLI5325';

$this->logInUser($email, '123password');

/** @var OrderInterface $placedOrder */
$placedOrder = $fixtures['placed_order'];
$this->placeOrderForCustomerWithEmail($email, $token);

$this->client->request('GET', '/shop-api/WEB_GB/orders/' . $placedOrder->getTokenValue(), [], [], self::CONTENT_TYPE_HEADER);
$this->client->request('GET', '/shop-api/WEB_GB/orders/' . $token, [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();

$this->assertResponse($response, 'order/order_details_response', Response::HTTP_OK);
Expand Down
11 changes: 9 additions & 2 deletions tests/Controller/Order/OrdersListApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@

use Symfony\Component\HttpFoundation\Response;
use Tests\Sylius\ShopApiPlugin\Controller\JsonApiTestCase;
use Tests\Sylius\ShopApiPlugin\Controller\Utils\OrderPlacerTrait;
use Tests\Sylius\ShopApiPlugin\Controller\Utils\ShopUserLoginTrait;

final class OrdersListApiTest extends JsonApiTestCase
{
use ShopUserLoginTrait;
use OrderPlacerTrait;

/**
* @test
*/
public function it_lists_only_placed_orders_of_logged_in_customer(): void
{
$this->loadFixturesFromFiles(['customer.yml', 'country.yml', 'address.yml', 'shop.yml', 'payment.yml', 'shipping.yml', 'order.yml']);
$this->logInUser('oliver@queen.com', '123password');
$this->loadFixturesFromFiles(['customer.yml', 'country.yml', 'address.yml', 'shop.yml', 'payment.yml', 'shipping.yml']);
$token = 'SDAOSLEFNWU35H3QLI5325';
$email = 'oliver@queen.com';

$this->logInUser($email, '123password');

$this->placeOrderForCustomerWithEmail($email, $token);

$this->client->request('GET', '/shop-api/WEB_GB/orders', [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();
Expand Down
50 changes: 50 additions & 0 deletions tests/Controller/Utils/OrderPlacerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\ShopApiPlugin\Controller\Utils;

use Sylius\ShopApiPlugin\Command\Cart\AddressOrder;
use Sylius\ShopApiPlugin\Command\Cart\ChoosePaymentMethod;
use Sylius\ShopApiPlugin\Command\Cart\ChooseShippingMethod;
use Sylius\ShopApiPlugin\Command\Cart\CompleteOrder;
use Sylius\ShopApiPlugin\Command\Cart\PickupCart;
use Sylius\ShopApiPlugin\Command\Cart\PutSimpleItemToCart;
use Sylius\ShopApiPlugin\Model\Address;
use Symfony\Component\Messenger\MessageBusInterface;

trait OrderPlacerTrait
{
protected function placeOrderForCustomerWithEmail(string $email, string $token): void
{
/** @var MessageBusInterface $bus */
$bus = $this->get('sylius_shop_api_plugin.command_bus');
$bus->dispatch(new PickupCart($token, 'WEB_GB'));
$bus->dispatch(new PutSimpleItemToCart($token, 'LOGAN_MUG_CODE', 5));
$bus->dispatch(new AddressOrder(
$token,
Address::createFromArray([
'firstName' => 'Sherlock',
'lastName' => 'Holmes',
'city' => 'London',
'street' => 'Baker Street 221b',
'countryCode' => 'GB',
'postcode' => 'NWB',
'provinceName' => 'Greater London',
]), Address::createFromArray([
'firstName' => 'Sherlock',
'lastName' => 'Holmes',
'city' => 'London',
'street' => 'Baker Street 221b',
'countryCode' => 'GB',
'postcode' => 'NWB',
'provinceName' => 'Greater London',
])
));
$bus->dispatch(new ChooseShippingMethod($token, 0, 'DHL'));
$bus->dispatch(new ChoosePaymentMethod($token, 0, 'PBC'));
$bus->dispatch(new CompleteOrder($token, $email));
}

abstract protected function get($id);
}
66 changes: 0 additions & 66 deletions tests/DataFixtures/ORM/order.yml

This file was deleted.

48 changes: 25 additions & 23 deletions tests/Responses/Expected/order/order_details_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"currency": "GBP",
"locale": "en_GB",
"checkoutState": "completed",
"checkoutCompletedAt": "2019-02-01T03:00:00+00:00",
"checkoutCompletedAt": "@string@.isDateTime()",
"items": [
{
"id": @integer@,
"quantity": 2,
"total": 3998,
"quantity": 5,
"total": 9995,
"product": {
"code": "LOGAN_MUG_CODE",
"name": "Logan Mug",
Expand Down Expand Up @@ -59,50 +59,52 @@
}
],
"totals": {
"total": 5498,
"items": 3998,
"total": 11495,
"items": 9995,
"taxes": 0,
"shipping": 1500,
"promotion": 0
},
"shippingAddress": {
"firstName": "Jeanie",
"lastName": "Metz",
"firstName": "Sherlock",
"lastName": "Holmes",
"countryCode": "GB",
"street": "McGlynn Island",
"city": "Klingside",
"postcode": "33553"
"street": "Baker Street 221b",
"city": "London",
"postcode": "NWB",
"provinceName": "Greater London"
},
"billingAddress": {
"firstName": "Jeanie",
"lastName": "Metz",
"firstName": "Sherlock",
"lastName": "Holmes",
"countryCode": "GB",
"street": "Kupreska",
"city": "Klingside",
"postcode": "33553"
"street": "Baker Street 221b",
"city": "London",
"postcode": "NWB",
"provinceName": "Greater London"
},
"payments": [
{
"state": "cart",
"state": "new",
"method": {
"code": "PBC",
"name": "Pay by check",
"description": @string@,
"instructions": @string@
"description": "Eveniet exercitationem aut porro. Magni cupiditate sit vitae voluptas. Non voluptates ut optio quos qui illo error nihil.",
"instructions": "Please put the money in the bag!"
},
"price": {
"current": 5498,
"current": 11495,
"currency": "GBP"
}
}
],
"shipments": [
{
"state": "cart",
"state": "ready",
"method": {
"code": "DHL",
"name": "DHL",
"description": @string@,
"description": "Sed natus debitis voluptas aut laudantium sit. Esse perspiciatis dignissimos error et itaque quibusdam tempora velit.",
"price": {
"current": 1500,
"currency": "GBP"
Expand All @@ -111,6 +113,6 @@
}
],
"cartDiscounts": [],
"tokenValue": @string@,
"number": @string@
"tokenValue": "SDAOSLEFNWU35H3QLI5325",
"number": "000000001"
}
Loading

0 comments on commit cc8d716

Please sign in to comment.