Skip to content

Commit

Permalink
Correcting failing tests, comments and missing configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMilek committed Feb 16, 2022
1 parent 900f5c8 commit 76fb1d7
Show file tree
Hide file tree
Showing 22 changed files with 331 additions and 194 deletions.
@@ -1,34 +1,35 @@
@applying_shipping_method_rules
Feature: Viewing available shipping methods based on channel
@viewing_shipping_methods
Feature: Viewing available shipping methods based on channel as a Shop User
In order to only see applicable shipping methods
As a Customer
As a Shop User
I want to see the shipping methods that are available to my order based on the channel

Background:
Given the store operates on a channel named "United Kingdom" in "USD" currency and with hostname "uk.cool-clothes.example"
And the store operates on another channel named "United States" in "USD" currency and with hostname "usa.cool-clothes.example"
And the store operates on another channel named "Colombia" in "USD" currency and with hostname "colombia.cool-clothes.example"
And the store has a zone "World" with code "WRLD"
And the store has a zone "World"
And the store ships to "United States"
And this zone has the "United States" country member
And the store ships everywhere for free for all channels
And the store has "ultra fast" shipping method with "$4" fee per unit for "United States" channel
And the store has "uber speedy" shipping method with "$15.00" fee per shipment for "United Kingdom" channel and "$12.00" for "Colombia" channel
And the store has "ultra fast" shipping method with "$4.00" fee per unit for "United States" channel
And the store has "uber speedy" shipping method with "$15.00" fee per shipment for "United Kingdom" channel
And the store has a product "T-Shirt" priced at "$20.00" available in channel "United Kingdom" and channel "United States"
And I am a logged in customer

@ui @api
@api @ui
Scenario: Seeing shipping methods that are available in channel as an logged in customer
Given I changed my current channel to "United States"
And I have product "T-Shirt" in the cart
When I specified the billing address
Then I should be on the checkout shipping step
And I should see "ultra fast" shipping method
And I should not see "uber speedy" shipping method

@ui @api
@api @ui
Scenario: Seeing shipping methods that are available in another channel as an logged in customer
Given I changed my current channel to "United Kingdom"
And I have product "T-Shirt" in the cart
When I specified the billing address
Then I should be on the checkout shipping step
And I should see "uber speedy" shipping method
And I should not see "ultra fast" shipping method
@@ -1,33 +1,34 @@
@applying_shipping_method_rules
Feature: Viewing available shipping methods based on channel
@viewing_shipping_methods
Feature: Viewing available shipping methods based on channel as a Visitor
In order to only see applicable shipping methods
As a guest
As a Visitor
I want to see the shipping methods that are available to my order based on the channel

Background:
Given the store operates on a channel named "United Kingdom" in "USD" currency and with hostname "uk.cool-clothes.example"
And the store operates on another channel named "United States" in "USD" currency and with hostname "usa.cool-clothes.example"
And the store operates on another channel named "Colombia" in "USD" currency and with hostname "colombia.cool-clothes.example"
And the store has a zone "World" with code "WRLD"
And the store has a zone "World"
And the store ships to "United States"
And this zone has the "United States" country member
And the store ships everywhere for free for all channels
And the store has "ultra fast" shipping method with "$4" fee per unit for "United States" channel
And the store has "uber speedy" shipping method with "$15.00" fee per shipment for "United Kingdom" channel and "$12.00" for "Colombia" channel
And the store has "ultra fast" shipping method with "$4.00" fee per unit for "United States" channel
And the store has "uber speedy" shipping method with "$15.00" fee per shipment for "United Kingdom" channel
And the store has a product "T-Shirt" priced at "$20.00" available in channel "United Kingdom" and channel "United States"

@api @ui
Scenario: Seeing shipping methods that are available in channel as an logged in customer
Scenario: Seeing shipping methods that are available in channel
Given I changed my current channel to "United States"
And I have product "T-Shirt" in the cart
When I complete addressing step with email "john@example.com" and "United States" based billing address
Then I should be on the checkout shipping step
And I should see "ultra fast" shipping method
And I should not see "uber speedy" shipping method

@api @ui
Scenario: Seeing shipping methods that are available in another channel as an logged in customer
Scenario: Seeing shipping methods that are available in another channel
Given I changed my current channel to "United Kingdom"
And I have product "T-Shirt" in the cart
When I complete addressing step with email "john@example.com" and "United States" based billing address
Then I should be on the checkout shipping step
And I should see "uber speedy" shipping method
And I should not see "ultra fast" shipping method
8 changes: 8 additions & 0 deletions psalm.xml
Expand Up @@ -127,9 +127,17 @@
<errorLevel type="info">
<referencedMethod name="PHPUnit\Framework\TestCase::__construct" />
<referencedMethod name="Symfony\Bundle\SecurityBundle\Security\_FirewallMap::getFirewallConfig" />
<referencedMethod name="ApiPlatform\Core\Util\RequestParser::getQueryString" />
<referencedMethod name="ApiPlatform\Core\Util\RequestParser::parseRequestParams" />
</errorLevel>
</InternalMethod>

<InternalClass>
<errorLevel type="info">
<referencedClass name="ApiPlatform\Core\Util\RequestParser" />
</errorLevel>
</InternalClass>

<InvalidDocblock>
<errorLevel type="info">
<file name="vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php" />
Expand Down
10 changes: 5 additions & 5 deletions src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php
Expand Up @@ -49,7 +49,7 @@ final class CheckoutContext implements Context

private ApiClientInterface $addressesClient;

private ApiClientInterface $shippingMethodClient;
private ApiClientInterface $shippingMethodsClient;

private ResponseCheckerInterface $responseChecker;

Expand All @@ -75,7 +75,7 @@ final class CheckoutContext implements Context
public function __construct(
ApiClientInterface $ordersClient,
ApiClientInterface $addressesClient,
ApiClientInterface $shippingMethodClient,
ApiClientInterface $shippingMethodsClient,
ResponseCheckerInterface $responseChecker,
RepositoryInterface $shippingMethodRepository,
OrderRepositoryInterface $orderRepository,
Expand All @@ -88,7 +88,7 @@ public function __construct(
) {
$this->ordersClient = $ordersClient;
$this->addressesClient = $addressesClient;
$this->shippingMethodClient = $shippingMethodClient;
$this->shippingMethodsClient = $shippingMethodsClient;
$this->responseChecker = $responseChecker;
$this->shippingMethodRepository = $shippingMethodRepository;
$this->orderRepository = $orderRepository;
Expand Down Expand Up @@ -1204,9 +1204,9 @@ private function getCartShippingMethods(array $cart): array
HTTPRequest::METHOD_GET
);

$this->shippingMethodClient->executeCustomRequest($request);
$this->shippingMethodsClient->executeCustomRequest($request);

return $this->responseChecker->getCollection($this->shippingMethodClient->getLastResponse());
return $this->responseChecker->getCollection($this->shippingMethodsClient->getLastResponse());
}

private function hasShippingMethod(ShippingMethodInterface $shippingMethod): bool
Expand Down
22 changes: 22 additions & 0 deletions src/Sylius/Behat/Context/Setup/ShippingContext.php
Expand Up @@ -258,6 +258,28 @@ public function storeHasShippingMethodWithFeePerShipmentForChannels(
]));
}

/**
* @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per shipment for ("[^"]+" channel)$/
*/
public function storeHasShippingMethodWithFeePerShipmentForChannel(
string $shippingMethodName,
string $fee,
ChannelInterface $channel,
): void {
$configuration = [$channel->getCode() => ['amount' => $fee]];

$this->saveShippingMethod($this->shippingMethodExampleFactory->create([
'name' => $shippingMethodName,
'enabled' => true,
'zone' => $this->getShippingZone(),
'calculator' => [
'type' => DefaultCalculators::FLAT_RATE,
'configuration' => $configuration,
],
'channels' => [$channel],
]));
}

/**
* @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit for ("[^"]+" channel)$/
* @Given /^the store has "([^"]+)" shipping method with ("[^"]+") fee per unit for ("[^"]+" channel) and ("[^"]+") for ("[^"]+" channel)$/
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Resources/config/services/api.xml
Expand Up @@ -24,7 +24,7 @@
<argument>shop</argument>
</service>

<service id="sylius.behat.api_platform_client.shop.shipping-methods" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
<service id="sylius.behat.api_platform_client.shop.shipping_methods" class="Sylius\Behat\Client\ApiPlatformClient" parent="sylius.behat.api_platform_client">
<argument>shipping-methods</argument>
<argument>shop</argument>
</service>
Expand Down
Expand Up @@ -68,7 +68,7 @@
<service id="sylius.behat.context.api.shop.checkout" class="Sylius\Behat\Context\Api\Shop\CheckoutContext">
<argument type="service" id="sylius.behat.api_platform_client.shop.order" />
<argument type="service" id="sylius.behat.api_platform_client.shop.address" />
<argument type="service" id="sylius.behat.api_platform_client.shop.shipping-methods" />
<argument type="service" id="sylius.behat.api_platform_client.shop.shipping_methods" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
<argument type="service" id="sylius.repository.shipping_method" />
<argument type="service" id="sylius.repository.order" />
Expand Down
4 changes: 3 additions & 1 deletion src/Sylius/Behat/Resources/config/suites.yml
Expand Up @@ -40,7 +40,8 @@ imports:
- suites/api/promotion/managing_catalog_promotions.yml
- suites/api/promotion/managing_promotions.yml
- suites/api/promotion/receiving_discount.yml
- suites/api/shipping/applying_shipping_method_rules.yml
- suites/ui/shipping/applying_shipping_method_rules.yml
- suites/api/shipping/viewing_shipping_methods.yml
- suites/api/shipping/managing_shipments.yml
- suites/api/shipping/managing_shipping_categories.yml
- suites/api/shipping/managing_shipping_methods.yml
Expand Down Expand Up @@ -113,6 +114,7 @@ imports:
- suites/ui/promotion/receiving_discount.yml
- suites/ui/shipping/applying_shipping_fee.yml
- suites/ui/shipping/applying_shipping_method_rules.yml
- suites/ui/shipping/viewing_shipping_methods.yml
- suites/ui/shipping/managing_shipments.yml
- suites/ui/shipping/managing_shipping_categories.yml
- suites/ui/shipping/managing_shipping_methods.yml
Expand Down
@@ -0,0 +1,38 @@
# This file is part of the Sylius package.
# (c) Paweł Jędrzejewski

default:
suites:
api_viewing_shipping_methods:
contexts:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.address
- sylius.behat.context.transform.cart
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.country
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.product
- sylius.behat.context.transform.shipping_method
- sylius.behat.context.transform.tax_category
- sylius.behat.context.transform.zone

- sylius.behat.context.transform.shared_storage

- sylius.behat.context.setup.address
- sylius.behat.context.setup.cart
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.geographical
- sylius.behat.context.setup.payment
- sylius.behat.context.setup.product
- sylius.behat.context.setup.shop_security
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.taxation
- sylius.behat.context.setup.zone

- sylius.behat.context.api.shop.cart
- sylius.behat.context.api.shop.checkout

filters:
tags: "@viewing_shipping_methods&&@api"
@@ -0,0 +1,38 @@
# This file is part of the Sylius package.
# (c) Paweł Jędrzejewski

default:
suites:
ui_viewing_shipping_methods:
contexts:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.address
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.country
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.product
- sylius.behat.context.transform.shipping_method
- sylius.behat.context.transform.tax_category
- sylius.behat.context.transform.zone

- sylius.behat.context.transform.shared_storage

- sylius.behat.context.setup.channel
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.geographical
- sylius.behat.context.setup.payment
- sylius.behat.context.setup.product
- sylius.behat.context.setup.shop_security
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.taxation
- sylius.behat.context.setup.zone

- sylius.behat.context.ui.shop.cart
- sylius.behat.context.ui.shop.checkout
- sylius.behat.context.ui.shop.checkout.addressing
- sylius.behat.context.ui.shop.checkout.payment
- sylius.behat.context.ui.shop.checkout.shipping

filters:
tags: "@viewing_shipping_methods&&@ui"
Expand Up @@ -17,67 +17,40 @@
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Core\Repository\ShipmentRepositoryInterface;
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
use Webmozart\Assert\Assert;

/** @experimental */
final class CartShippingMethodsCollectionDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface
{
private OrderRepositoryInterface $orderRepository;

private ShipmentRepositoryInterface $shipmentRepository;

private ShippingMethodRepositoryInterface $shippingMethodsRepository;

private ShippingMethodsResolverInterface $shippingMethodsResolver;

private ChannelContextInterface $channelContext;

public function __construct(
OrderRepositoryInterface $orderRepository,
ShipmentRepositoryInterface $shipmentRepository,
ShippingMethodRepositoryInterface $shippingMethodsRepository,
ShippingMethodsResolverInterface $shippingMethodsResolver,
ChannelContextInterface $channelContext
private ShipmentRepositoryInterface $shipmentRepository,
private ShippingMethodRepositoryInterface $shippingMethodRepository,
private ShippingMethodsResolverInterface $shippingMethodsResolver,
private ChannelContextInterface $channelContext
) {
$this->orderRepository = $orderRepository;
$this->shipmentRepository = $shipmentRepository;
$this->shippingMethodsRepository = $shippingMethodsRepository;
$this->shippingMethodsResolver = $shippingMethodsResolver;
$this->channelContext= $channelContext;
}

public function getCollection(string $resourceClass, string $operationName = null, array $context = []): array
{
if (!isset($context['filters'])) {
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

return $this->shippingMethodsRepository->findEnabledForChannel($channel);
if (!isset($context['filters'])) {
return $this->shippingMethodRepository->findEnabledForChannel($channel);
}

$parameters = $context['filters'];

Assert::keyExists($parameters, 'tokenValue');
Assert::keyExists($parameters, 'shipmentId');

/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

/** @var OrderInterface|null $cart */
$cart = $this->orderRepository->findCartByTokenValueAndChannel($parameters['tokenValue'], $channel);
if ($cart === null) {
if (!isset($parameters['tokenValue']) || !isset($parameters['shipmentId'])) {
return [];
}

/** @var ShipmentInterface|null $shipment */
$shipment = $this->shipmentRepository->findOneByOrderId($parameters['shipmentId'], $cart->getId());
$shipment = $this->shipmentRepository->findOneByOrderTokenAndChannel($parameters['shipmentId'], $parameters['tokenValue'], $channel);
if ($shipment === null) {
return [];
}
Expand All @@ -87,6 +60,11 @@ public function getCollection(string $resourceClass, string $operationName = nul

public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return is_a($resourceClass, ShippingMethodInterface::class, true); // only for shop context
return is_a($resourceClass, ShippingMethodInterface::class, true) && $this->isShopGetCollectionOperation($context);
}

private function isShopGetCollectionOperation(array $context): bool
{
return isset($context['collection_operation_name']) && \str_starts_with($context['collection_operation_name'], 'shop');
}
}
Expand Up @@ -35,7 +35,7 @@ public function getDescription(string $resourceClass): array
'property' => null,
'swagger' => [
'name' => 'Order',
'description' => 'Id of order for which you would like to check available shipping methods. Should be set if you are checking shipping methods for shipment, that is not assigned to user.',
'description' => 'Token of an order for which you would like to check available shipping methods. Should be set if you are checking shipping methods for shipment, that is not assigned to user.',
],
],
];
Expand Down
Expand Up @@ -59,7 +59,6 @@
</service>

<service id="Sylius\Bundle\ApiBundle\DataProvider\CartShippingMethodsCollectionDataProvider">
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="sylius.repository.shipment" />
<argument type="service" id="sylius.repository.shipping_method" />
<argument type="service" id="sylius.shipping_methods_resolver" />
Expand Down

0 comments on commit 76fb1d7

Please sign in to comment.