Skip to content

Commit

Permalink
[API][ShippingMethod] Add contract test for available shipping method…
Browse files Browse the repository at this point in the history
…s endpoint
  • Loading branch information
GSadee committed Jun 21, 2021
1 parent c436f16 commit 45978e5
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<group>shop:shipping_method:read</group>
</attribute>

<attribute name="name">
<group>admin:shipping_method:read</group>
<group>shop:shipping_method:read</group>
</attribute>

<attribute name="position">
<group>admin:shipping_method:create</group>
<group>admin:shipping_method:read</group>
Expand All @@ -54,14 +59,12 @@
<group>admin:shipping_method:create</group>
<group>admin:shipping_method:read</group>
<group>admin:shipping_method:update</group>
<group>shop:shipping_method:read</group>
</attribute>

<attribute name="channels">
<group>admin:shipping_method:create</group>
<group>admin:shipping_method:read</group>
<group>admin:shipping_method:update</group>
<group>shop:shipping_method:read</group>
</attribute>

<attribute name="calculator">
Expand Down
35 changes: 35 additions & 0 deletions tests/Api/DataFixtures/ORM/shipping_method.yaml
Original file line number Diff line number Diff line change
@@ -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: '<paragraph(2)>'
translatable: '@shipping_method_ups'
shipping_method_translation_dhl:
name: 'DHL'
locale: 'en_US'
description: '<paragraph(2)>'
translatable: '@shipping_method_dhl'
10 changes: 10 additions & 0 deletions tests/Api/DataFixtures/ORM/zone.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Sylius\Component\Addressing\Model\ZoneMember:
zone_member_{US, FR, DE}:
code: '<current()>'

Sylius\Component\Addressing\Model\Zone:
zone_world:
code: 'WORLD'
name: 'World'
type: 'country'
members: ['@zone_member_US', '@zone_member_FR', '@zone_member_DE']
Original file line number Diff line number Diff line change
@@ -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
}
70 changes: 70 additions & 0 deletions tests/Api/Shop/ShippingMethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Tests\Api\Shop;

use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart;
use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart;
use Sylius\Bundle\ApiBundle\Command\Checkout\AddressOrder;
use Sylius\Component\Core\Model\Address;
use Sylius\Tests\Api\JsonApiTestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;

final class ShippingMethodsTest extends JsonApiTestCase
{
/** @test */
public function it_gets_available_shipping_methods(): void
{
$this->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);
}
}

0 comments on commit 45978e5

Please sign in to comment.