From 45978e5efb491e4129110fe2c3ece25518b17e41 Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Thu, 17 Jun 2021 12:51:46 +0200 Subject: [PATCH] [API][ShippingMethod] Add contract test for available shipping methods endpoint --- .../config/serialization/ShippingMethod.xml | 7 +- .../Api/DataFixtures/ORM/shipping_method.yaml | 35 ++++++++++ tests/Api/DataFixtures/ORM/zone.yaml | 10 +++ .../shop/get_shipping_methods_response.json | 44 ++++++++++++ tests/Api/Shop/ShippingMethodsTest.php | 70 +++++++++++++++++++ 5 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 tests/Api/DataFixtures/ORM/shipping_method.yaml create mode 100644 tests/Api/DataFixtures/ORM/zone.yaml create mode 100644 tests/Api/Responses/Expected/shop/get_shipping_methods_response.json create mode 100644 tests/Api/Shop/ShippingMethodsTest.php diff --git a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ShippingMethod.xml b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ShippingMethod.xml index a119fabd6dbc..f8c1b3f5a143 100644 --- a/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ShippingMethod.xml +++ b/src/Sylius/Bundle/ApiBundle/Resources/config/serialization/ShippingMethod.xml @@ -37,6 +37,11 @@ shop:shipping_method:read + + admin:shipping_method:read + shop:shipping_method:read + + admin:shipping_method:create admin:shipping_method:read @@ -54,14 +59,12 @@ admin:shipping_method:create admin:shipping_method:read admin:shipping_method:update - shop:shipping_method:read admin:shipping_method:create admin:shipping_method:read admin:shipping_method:update - shop:shipping_method:read diff --git a/tests/Api/DataFixtures/ORM/shipping_method.yaml b/tests/Api/DataFixtures/ORM/shipping_method.yaml new file mode 100644 index 000000000000..061fa9f50147 --- /dev/null +++ b/tests/Api/DataFixtures/ORM/shipping_method.yaml @@ -0,0 +1,35 @@ +Sylius\Component\Core\Model\ShippingMethod: + shipping_method_ups: + code: 'UPS' + enabled: true + calculator: 'flat_rate' + configuration: + WEB: + amount: 500 + zone: '@zone_world' + currentLocale: 'en_US' + translations: ["@shipping_method_translation_ups"] + channels: ["@channel_web"] + shipping_method_dhl: + code: 'DHL' + enabled: true + calculator: 'flat_rate' + configuration: + WEB: + amount: 1000 + zone: '@zone_world' + currentLocale: 'en_US' + translations: ["@shipping_method_translation_dhl"] + channels: ["@channel_web"] + +Sylius\Component\Shipping\Model\ShippingMethodTranslation: + shipping_method_translation_ups: + name: 'UPS' + locale: 'en_US' + description: '' + translatable: '@shipping_method_ups' + shipping_method_translation_dhl: + name: 'DHL' + locale: 'en_US' + description: '' + translatable: '@shipping_method_dhl' diff --git a/tests/Api/DataFixtures/ORM/zone.yaml b/tests/Api/DataFixtures/ORM/zone.yaml new file mode 100644 index 000000000000..91137ee083ee --- /dev/null +++ b/tests/Api/DataFixtures/ORM/zone.yaml @@ -0,0 +1,10 @@ +Sylius\Component\Addressing\Model\ZoneMember: + zone_member_{US, FR, DE}: + code: '' + +Sylius\Component\Addressing\Model\Zone: + zone_world: + code: 'WORLD' + name: 'World' + type: 'country' + members: ['@zone_member_US', '@zone_member_FR', '@zone_member_DE'] diff --git a/tests/Api/Responses/Expected/shop/get_shipping_methods_response.json b/tests/Api/Responses/Expected/shop/get_shipping_methods_response.json new file mode 100644 index 000000000000..1bc5d8621b23 --- /dev/null +++ b/tests/Api/Responses/Expected/shop/get_shipping_methods_response.json @@ -0,0 +1,44 @@ +{ + "@context": "\/api\/v2\/contexts\/ShippingMethod", + "@id": "\/api\/v2\/shop\/orders\/nAWw2jewpA\/shipments\/@integer@\/methods", + "@type": "hydra:Collection", + "hydra:member": [ + { + "@id": "\/api\/v2\/shop\/shipping-methods\/UPS", + "@type": "ShippingMethod", + "id": @integer@, + "code": "UPS", + "position": 0, + "translations": { + "en_US": { + "@id": "\/api\/v2\/shop\/shipping-method-translations\/@integer@", + "@type": "ShippingMethodTranslation", + "id": @integer@, + "name": "UPS", + "description": @string@ + } + }, + "name": "UPS", + "price": 500 + }, + { + "@id": "\/api\/v2\/shop\/shipping-methods\/DHL", + "@type": "ShippingMethod", + "id": @integer@, + "code": "DHL", + "position": 1, + "translations": { + "en_US": { + "@id": "\/api\/v2\/shop\/shipping-method-translations\/@integer@", + "@type": "ShippingMethodTranslation", + "id": @integer@, + "name": "DHL", + "description": @string@ + } + }, + "name": "DHL", + "price": 1000 + } + ], + "hydra:totalItems": 2 +} diff --git a/tests/Api/Shop/ShippingMethodsTest.php b/tests/Api/Shop/ShippingMethodsTest.php new file mode 100644 index 000000000000..9c40e09c1e88 --- /dev/null +++ b/tests/Api/Shop/ShippingMethodsTest.php @@ -0,0 +1,70 @@ +loadFixturesFromFiles(['cart.yaml', 'country.yaml', 'zone.yaml', 'shipping_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); + + $addItemToCartCommand = new AddItemToCart('MUG_BLUE', 3); + $addItemToCartCommand->setOrderTokenValue($tokenValue); + $commandBus->dispatch($addItemToCartCommand); + + $address = new Address(); + $address->setFirstName('John'); + $address->setLastName('Doe'); + $address->setCity('New York'); + $address->setStreet('Avenue'); + $address->setCountryCode('US'); + $address->setPostcode('90000'); + + $addressOrderCommand = new AddressOrder('sylius@example.com', $address); + $addressOrderCommand->setOrderTokenValue($tokenValue); + $commandBus->dispatch($addressOrderCommand); + + $this->client->request('GET', '/api/v2/shop/orders/nAWw2jewpA', [], [], self::CONTENT_TYPE_HEADER); + $orderResponse = json_decode($this->client->getResponse()->getContent(), true); + + $this->client->request( + 'GET', + sprintf('/api/v2/shop/orders/nAWw2jewpA/shipments/%s/methods', $orderResponse['shipments'][0]['id']), + [], + [], + self::CONTENT_TYPE_HEADER + ); + $response = $this->client->getResponse(); + + $this->assertResponse($response, 'shop/get_shipping_methods_response', Response::HTTP_OK); + } +}