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 10, 2022
1 parent 56090b8 commit ed19222
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 124 deletions.
@@ -1,7 +1,7 @@
@applying_shipping_method_rules
Feature: Viewing available shipping methods based on channel
In order to only see applicable shipping methods
As a guest
As a guest or as a logged in customer
I want to see the shipping methods that are available to my order based on the channel

Background:
Expand All @@ -17,17 +17,35 @@ Feature: Viewing available shipping methods based on 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 as a guest
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

@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 as a guest
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

@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 am a logged in customer
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

@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 am a logged in customer
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

This file was deleted.

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
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
Expand Up @@ -29,47 +29,27 @@
/** @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 OrderRepositoryInterface $orderRepository,
private ShipmentRepositoryInterface $shipmentRepository,
private ShippingMethodRepositoryInterface $shippingMethodsRepository,
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
{
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

if (!isset($context['filters'])) {
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();

return $this->shippingMethodsRepository->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) {
Expand All @@ -87,6 +67,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 @@ -69,6 +69,7 @@
<argument type="service" id="sylius.repository.shipment" />
<argument type="service" id="sylius.registry.shipping_calculator" />
<argument type="service" id="Symfony\Component\HttpFoundation\RequestStack" />
<argument type="service" id="Sylius\Component\Channel\Context\ChannelContextInterface" />
<tag name="serializer.normalizer" priority="64" />
</service>

Expand Down
39 changes: 16 additions & 23 deletions src/Sylius/Bundle/ApiBundle/Serializer/ShippingMethodNormalizer.php
Expand Up @@ -14,14 +14,15 @@
namespace Sylius\Bundle\ApiBundle\Serializer;

use ApiPlatform\Core\Util\RequestParser;
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\Registry\ServiceRegistryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
Expand All @@ -34,24 +35,13 @@ final class ShippingMethodNormalizer implements ContextAwareNormalizerInterface,

private const ALREADY_CALLED = 'sylius_shipping_method_normalizer_already_called';

private OrderRepositoryInterface $orderRepository;

private ShipmentRepositoryInterface $shipmentRepository;

private ServiceRegistryInterface $shippingCalculators;

private RequestStack $requestStack;

public function __construct(
OrderRepositoryInterface $orderRepository,
ShipmentRepositoryInterface $shipmentRepository,
ServiceRegistryInterface $shippingCalculators,
RequestStack $requestStack
private OrderRepositoryInterface $orderRepository,
private ShipmentRepositoryInterface $shipmentRepository,
private ServiceRegistryInterface $shippingCalculators,
private RequestStack $requestStack,
private ChannelContextInterface $channelContext
) {
$this->orderRepository = $orderRepository;
$this->shipmentRepository = $shipmentRepository;
$this->shippingCalculators = $shippingCalculators;
$this->requestStack = $requestStack;
}

public function normalize($object, $format = null, array $context = [])
Expand All @@ -77,17 +67,20 @@ public function normalize($object, $format = null, array $context = [])
Assert::keyExists($filters, 'tokenValue');
Assert::keyExists($filters, 'shipmentId');

/** @var ShipmentInterface $shipment */
$shipment = $this->shipmentRepository->find($filters['shipmentId']); // Duplication of logic from CartShippingMethodCollectionDataProvider

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

/** @var OrderInterface|null $cart */
$cart = $this->orderRepository->findCartByTokenValue($filters['tokenValue']);
$cart = $this->orderRepository->findCartByTokenValueAndChannel($filters['tokenValue'], $channel);

Assert::notNull($cart);

Assert::true($cart->hasShipment($shipment), 'Shipment doesn\'t match for order');
/** @var ShipmentInterface|null $shipment */
$shipment = $this->shipmentRepository->findOneByOrderId($filters['shipmentId'], $cart->getId());

Assert::notNull($shipment);

Assert::true($cart->hasShipment($shipment), 'Shipment doesn\'t match for order');

$calculator = $this->shippingCalculators->get($object->getCalculator());
$data['price'] = $calculator->calculate($shipment, $object->getConfiguration());
Expand Down
Expand Up @@ -38,13 +38,16 @@ function let(
$this->beConstructedWith($orderRepository, $shipmentRepository, $shippingMethodsRepository, $shippingMethodsResolver, $channelContext);
}

function it_supports_shipping(): void
function it_supports_shipping_methods_interface_and_only_shop_context(): void
{
$this
->supports(
ShippingMethodInterface::class,
Request::METHOD_GET,
['filters' => ['tokenValue' => '666', 'shipmentId' => '999']],
[
'collection_operation_name' => 'shop_get',
'filters' => ['tokenValue' => '666', 'shipmentId' => '999']
],
)
->shouldReturn(true)
;
Expand Down

0 comments on commit ed19222

Please sign in to comment.