Skip to content

Commit

Permalink
Avoid BC breaks in modified services
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed May 9, 2018
1 parent 68154e3 commit 90de95c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 19 deletions.
@@ -1,5 +1,5 @@
@checkout
Feature: Seeing shipping methods which category is same as category of all my units
Feature: Seeing default shipping method selected based on shipping address
In order to select correct shipping method for my order
As a Customer
I want to be able to choose only shipping methods that match shipping category of all my items
Expand Down
Expand Up @@ -29,8 +29,8 @@

<service id="sylius.order_processing.order_shipment_processor" class="Sylius\Component\Core\OrderProcessing\OrderShipmentProcessor">
<argument type="service" id="sylius.shipping_method_resolver.default" />
<argument type="service" id="sylius.shipping_methods_resolver" />
<argument type="service" id="sylius.factory.shipment" />
<argument type="service" id="sylius.shipping_methods_resolver" />
<tag name="sylius.order_processor" priority="50"/>
</service>

Expand Down
Expand Up @@ -31,28 +31,35 @@ final class OrderShipmentProcessor implements OrderProcessorInterface
private $defaultShippingMethodResolver;

/**
* @var ShippingMethodsResolverInterface
* @var FactoryInterface
*/
private $shippingMethodsResolver;
private $shipmentFactory;

/**
* @var FactoryInterface
* @var ShippingMethodsResolverInterface|null
*/
private $shipmentFactory;
private $shippingMethodsResolver;

/**
* @param DefaultShippingMethodResolverInterface $defaultShippingMethodResolver
* @param ShippingMethodsResolverInterface $shippingMethodsResolver
* @param FactoryInterface $shipmentFactory
* @param ShippingMethodsResolverInterface|null $shippingMethodsResolver
*/
public function __construct(
DefaultShippingMethodResolverInterface $defaultShippingMethodResolver,
ShippingMethodsResolverInterface $shippingMethodsResolver,
FactoryInterface $shipmentFactory
FactoryInterface $shipmentFactory,
?ShippingMethodsResolverInterface $shippingMethodsResolver = null
) {
$this->defaultShippingMethodResolver = $defaultShippingMethodResolver;
$this->shippingMethodsResolver = $shippingMethodsResolver;
$this->shipmentFactory = $shipmentFactory;
$this->shippingMethodsResolver = $shippingMethodsResolver;

if (2 === func_num_args() || null === $shippingMethodsResolver) {
@trigger_error(
'Not passing ShippingMethodsResolverInterface explicitly is deprecated since 1.2 and will be prohibited in 2.0',
E_USER_DEPRECATED
);
}
}

/**
Expand Down Expand Up @@ -121,7 +128,11 @@ private function getExistingShipmentWithProperMethod(OrderInterface $order): ?Sh
/** @var ShipmentInterface $shipment */
$shipment = $order->getShipments()->first();

if (!in_array($shipment->getMethod(), $this->shippingMethodsResolver->getSupportedMethods($shipment))) {
if (null === $this->shippingMethodsResolver) {
return $shipment;
}

if (!in_array($shipment->getMethod(), $this->shippingMethodsResolver->getSupportedMethods($shipment), true)) {
try {
$shipment->setMethod($this->defaultShippingMethodResolver->getDefaultShippingMethod($shipment));
} catch (UnresolvedDefaultShippingMethodException $exception) {
Expand Down
Expand Up @@ -33,20 +33,27 @@ class DefaultShippingMethodResolver implements DefaultShippingMethodResolverInte
private $shippingMethodRepository;

/**
* @var ZoneMatcherInterface
* @var ZoneMatcherInterface|null
*/
private $zoneMatcher;

/**
* @param ShippingMethodRepositoryInterface $shippingMethodRepository
* @param ZoneMatcherInterface $zoneMatcher
* @param ZoneMatcherInterface|null $zoneMatcher
*/
public function __construct(
ShippingMethodRepositoryInterface $shippingMethodRepository,
ZoneMatcherInterface $zoneMatcher
?ZoneMatcherInterface $zoneMatcher = null
) {
$this->shippingMethodRepository = $shippingMethodRepository;
$this->zoneMatcher = $zoneMatcher;

if (1 === func_num_args() || null === $zoneMatcher) {
@trigger_error(
'Not passing ZoneMatcherInterface explicitly is deprecated since 1.2 and will be prohibited in 2.0',
E_USER_DEPRECATED
);
}
}

/**
Expand Down Expand Up @@ -76,11 +83,11 @@ public function getDefaultShippingMethod(ShipmentInterface $shipment): ShippingM
* @param ChannelInterface $channel
* @param AddressInterface|null $address
*
* @return array
* @return array|ShippingMethodInterface[]
*/
private function getShippingMethods(ChannelInterface $channel, ?AddressInterface $address): array
{
if (null === $address) {
if (null === $address || null === $this->zoneMatcher) {
return $this->shippingMethodRepository->findEnabledForChannel($channel);
}

Expand Down
Expand Up @@ -31,10 +31,10 @@ final class OrderShipmentProcessorSpec extends ObjectBehavior
{
function let(
DefaultShippingMethodResolverInterface $defaultShippingMethodResolver,
ShippingMethodsResolverInterface $shippingMethodsResolver,
FactoryInterface $shipmentFactory
FactoryInterface $shipmentFactory,
ShippingMethodsResolverInterface $shippingMethodsResolver
): void {
$this->beConstructedWith($defaultShippingMethodResolver, $shippingMethodsResolver, $shipmentFactory);
$this->beConstructedWith($defaultShippingMethodResolver, $shipmentFactory, $shippingMethodsResolver);
}

function it_is_an_order_processor(): void
Expand Down Expand Up @@ -137,6 +137,41 @@ function it_adds_new_item_units_to_existing_shipment(
$this->process($order);
}

function it_adds_new_item_units_to_existing_shipment_without_checking_methods_if_shipping_methods_resolver_is_not_used(
DefaultShippingMethodResolverInterface $defaultShippingMethodResolver,
FactoryInterface $shipmentFactory,
OrderInterface $order,
ShipmentInterface $shipment,
Collection $shipments,
OrderItemUnitInterface $itemUnit,
OrderItemUnitInterface $itemUnitWithoutShipment,
OrderItemInterface $orderItem,
ProductVariantInterface $productVariant
): void {
$this->beConstructedWith($defaultShippingMethodResolver, $shipmentFactory);

$shipments->first()->willReturn($shipment);

$orderItem->getVariant()->willReturn($productVariant);

$order->isShippingRequired()->willReturn(true);

$order->getItems()->willReturn(new ArrayCollection([$orderItem->getWrappedObject()]));

$order->isEmpty()->willReturn(false);
$order->hasShipments()->willReturn(true);
$order->getItemUnits()->willReturn(new ArrayCollection([$itemUnit->getWrappedObject(), $itemUnitWithoutShipment->getWrappedObject()]));
$order->getShipments()->willReturn($shipments);

$itemUnit->getShipment()->willReturn($shipment);

$shipment->getUnits()->willReturn(new ArrayCollection([]));
$shipment->addUnit($itemUnitWithoutShipment)->shouldBeCalled();
$shipment->addUnit($itemUnit)->shouldNotBeCalled();

$this->process($order);
}

function it_removes_units_before_adding_new_ones(
ShippingMethodsResolverInterface $shippingMethodsResolver,
OrderInterface $order,
Expand Down
Expand Up @@ -65,6 +65,30 @@ function it_returns_first_enabled_shipping_method_from_shipment_order_channel_if
$this->getDefaultShippingMethod($shipment)->shouldReturn($firstShippingMethod);
}

function it_returns_first_enabled_shipping_method_from_shipment_order_channel_if_zone_matcher_is_not_used(
AddressInterface $shippingAddress,
ChannelInterface $channel,
OrderInterface $order,
ShipmentInterface $shipment,
ShippingMethodInterface $firstShippingMethod,
ShippingMethodInterface $secondShippingMethod,
ShippingMethodRepositoryInterface $shippingMethodRepository
): void {
$this->beConstructedWith($shippingMethodRepository);

$shipment->getOrder()->willReturn($order);

$order->getChannel()->willReturn($channel);
$order->getShippingAddress()->willReturn($shippingAddress);

$shippingMethodRepository
->findEnabledForChannel($channel)
->willReturn([$firstShippingMethod, $secondShippingMethod])
;

$this->getDefaultShippingMethod($shipment)->shouldReturn($firstShippingMethod);
}

function it_returns_first_enabled_shipping_method_matched_by_order_channel_and_shipping_address_zone(
AddressInterface $shippingAddress,
ChannelInterface $channel,
Expand Down

0 comments on commit 90de95c

Please sign in to comment.