From 0e445e4b876e96139e45a53a5e98c8c87fa745f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Chru=C5=9Bciel?= Date: Fri, 10 May 2019 15:12:11 +0200 Subject: [PATCH] [Tests] Fix placed order creation in test env --- .../Cart/CartViewRepository.php | 1 - .../Controller/Cart/CartSummarizeApiTest.php | 15 +++-- tests/Controller/Order/OrderShowApiTest.php | 15 +++-- tests/Controller/Order/OrdersListApiTest.php | 11 +++- tests/Controller/Utils/OrderPlacerTrait.php | 51 ++++++++++++++ tests/DataFixtures/ORM/order.yml | 66 ------------------- .../order/order_details_response.json | 52 +++++++-------- .../Expected/order/orders_list_response.json | 52 +++++++-------- 8 files changed, 128 insertions(+), 135 deletions(-) create mode 100644 tests/Controller/Utils/OrderPlacerTrait.php delete mode 100644 tests/DataFixtures/ORM/order.yml diff --git a/src/ViewRepository/Cart/CartViewRepository.php b/src/ViewRepository/Cart/CartViewRepository.php index e41c4f327..fb86edf76 100644 --- a/src/ViewRepository/Cart/CartViewRepository.php +++ b/src/ViewRepository/Cart/CartViewRepository.php @@ -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; diff --git a/tests/Controller/Cart/CartSummarizeApiTest.php b/tests/Controller/Cart/CartSummarizeApiTest.php index 4c984921c..e30747b60 100644 --- a/tests/Controller/Cart/CartSummarizeApiTest.php +++ b/tests/Controller/Cart/CartSummarizeApiTest.php @@ -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 @@ -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); diff --git a/tests/Controller/Order/OrderShowApiTest.php b/tests/Controller/Order/OrderShowApiTest.php index 4e48e6987..b594f4416 100644 --- a/tests/Controller/Order/OrderShowApiTest.php +++ b/tests/Controller/Order/OrderShowApiTest.php @@ -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); diff --git a/tests/Controller/Order/OrdersListApiTest.php b/tests/Controller/Order/OrdersListApiTest.php index 1250dd97c..c24346f43 100644 --- a/tests/Controller/Order/OrdersListApiTest.php +++ b/tests/Controller/Order/OrdersListApiTest.php @@ -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(); diff --git a/tests/Controller/Utils/OrderPlacerTrait.php b/tests/Controller/Utils/OrderPlacerTrait.php new file mode 100644 index 000000000..b2850eb63 --- /dev/null +++ b/tests/Controller/Utils/OrderPlacerTrait.php @@ -0,0 +1,51 @@ +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); +} diff --git a/tests/DataFixtures/ORM/order.yml b/tests/DataFixtures/ORM/order.yml deleted file mode 100644 index 29c21db91..000000000 --- a/tests/DataFixtures/ORM/order.yml +++ /dev/null @@ -1,66 +0,0 @@ -Sylius\Component\Core\Model\Order: - placed_order: - channel: "@gb_web_channel" - items: ["@mug_item"] - currencyCode: "GBP" - localeCode: "en_GB" - customer: "@customer_oliver" - checkoutState: "completed" - checkoutCompletedAt: "" - state: "new" - payments: ["@placed_order_payment"] - shipments: ["@placed_order_shipment"] - shippingAddress: "@customer_oliver_home_address" - billingAddress: "@customer_oliver_work_address" - number: "ORDERNUMBERPLACED" - tokenValue: "ORDERTOKENPLACED" - not_placed_order: - channel: "@gb_web_channel" - items: ["@large_t_shirt_item"] - currencyCode: "GBP" - localeCode: "en_GB" - customer: "@customer_oliver" - checkoutState: "cart" - state: "cart" - payments: ["@not_placed_order_payment"] - shipments: ["@not_placed_order_shipment"] - shippingAddress: "@customer_oliver_freelance_address" - billingAddress: "@customer_hater_address" - number: "ORDERNUMBERUNPLACED" - tokenValue: "ORDERTOKENUNPLACED" - -Sylius\Component\Core\Model\OrderItem: - mug_item: - variant: "@mug_variant" - large_t_shirt_item: - variant: "@t_shirt_large" - -Sylius\Component\Core\Model\OrderItemUnit: - mug_item_unit1: - __construct: ["@mug_item"] - mug_item_unit2: - __construct: ["@mug_item"] - large_t_shirt_item_unit: - __construct: ["@large_t_shirt_item"] - -Sylius\Component\Core\Model\Payment: - placed_order_payment: - state: "cart" - method: "@pay_by_check" - order: "@placed_order" - currencyCode: "GBP" - not_placed_order_payment: - state: "cart" - method: "@cash_on_delivery" - order: "@not_placed_order" - currencyCode: "GBP" - -Sylius\Component\Core\Model\Shipment: - placed_order_shipment: - state: "cart" - method: "@dhl" - order: "@placed_order" - not_placed_order_shipment: - state: "cart" - method: "@fedEx" - order: "@not_placed_order" diff --git a/tests/Responses/Expected/order/order_details_response.json b/tests/Responses/Expected/order/order_details_response.json index 32478960c..52f4f96c0 100644 --- a/tests/Responses/Expected/order/order_details_response.json +++ b/tests/Responses/Expected/order/order_details_response.json @@ -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", @@ -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" @@ -115,6 +113,6 @@ } ], "cartDiscounts": [], - "tokenValue": @string@, - "number": @string@ + "tokenValue": "SDAOSLEFNWU35H3QLI5325", + "number": "000000001" } diff --git a/tests/Responses/Expected/order/orders_list_response.json b/tests/Responses/Expected/order/orders_list_response.json index 2077add43..6e7a50a24 100644 --- a/tests/Responses/Expected/order/orders_list_response.json +++ b/tests/Responses/Expected/order/orders_list_response.json @@ -4,12 +4,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", @@ -60,54 +60,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" @@ -116,7 +114,7 @@ } ], "cartDiscounts": [], - "tokenValue": @string@, - "number": @string@ + "tokenValue": "SDAOSLEFNWU35H3QLI5325", + "number": "000000001" } ]