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 1cc6ac6 commit 0e445e4
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 135 deletions.
1 change: 0 additions & 1 deletion src/ViewRepository/Cart/CartViewRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Sylius\ShopApiPlugin\ViewRepository\Cart;

use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutStates;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\ShopApiPlugin\Factory\Cart\CartViewFactoryInterface;
use Sylius\ShopApiPlugin\View\Cart\CartSummaryView;
Expand Down
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
51 changes: 51 additions & 0 deletions tests/Controller/Utils/OrderPlacerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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));
}

/** Function is not typehinted because has to be compatible with \ApiTestCase\ApiTestCase::get($id) */
abstract protected function get($id);
}
66 changes: 0 additions & 66 deletions tests/DataFixtures/ORM/order.yml

This file was deleted.

52 changes: 25 additions & 27 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,54 +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",
"company": "Sylius",
"phoneNumber": "349713"
"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",
"company": "Locastic",
"phoneNumber": "349713"
"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 @@ -115,6 +113,6 @@
}
],
"cartDiscounts": [],
"tokenValue": @string@,
"number": @string@
"tokenValue": "SDAOSLEFNWU35H3QLI5325",
"number": "000000001"
}
Loading

0 comments on commit 0e445e4

Please sign in to comment.