Skip to content

Commit

Permalink
Merge pull request Sylius#5951 from steffenbrem/bugfix/core-shipping-…
Browse files Browse the repository at this point in the history
…methods-resolver

[BugFix][Core] Added shipping method eligibility checker to core methods resolver
  • Loading branch information
Paweł Jędrzejewski authored Sep 10, 2016
2 parents 0f73608 + 2fc5ba0 commit e86942f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
22 changes: 20 additions & 2 deletions Resolver/ZoneAndChannelBasedShippingMethodsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Shipping\Checker\ShippingMethodEligibilityCheckerInterface;
use Sylius\Component\Shipping\Model\ShippingSubjectInterface;
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
Expand All @@ -34,16 +35,24 @@ class ZoneAndChannelBasedShippingMethodsResolver implements ShippingMethodsResol
*/
private $zoneMatcher;

/**
* @var ShippingMethodEligibilityCheckerInterface
*/
private $eligibilityChecker;

/**
* @param ShippingMethodRepositoryInterface $shippingMethodRepository
* @param ZoneMatcherInterface $zoneMatcher
* @param ShippingMethodEligibilityCheckerInterface $eligibilityChecker
*/
public function __construct(
ShippingMethodRepositoryInterface $shippingMethodRepository,
ZoneMatcherInterface $zoneMatcher
ZoneMatcherInterface $zoneMatcher,
ShippingMethodEligibilityCheckerInterface $eligibilityChecker
) {
$this->shippingMethodRepository = $shippingMethodRepository;
$this->zoneMatcher = $zoneMatcher;
$this->eligibilityChecker = $eligibilityChecker;
}

/**
Expand All @@ -61,7 +70,16 @@ public function getSupportedMethods(ShippingSubjectInterface $subject)
return [];
}

return $this->shippingMethodRepository->findEnabledForZonesAndChannel($zones, $order->getChannel());
$methods = [];

$shippingMethods = $this->shippingMethodRepository->findEnabledForZonesAndChannel($zones, $order->getChannel());
foreach ($shippingMethods as $shippingMethod) {
if ($this->eligibilityChecker->isEligible($subject, $shippingMethod)) {
$methods[] = $shippingMethod;
}
}

return $methods;
}

/**
Expand Down
42 changes: 40 additions & 2 deletions spec/Resolver/ZoneAndChannelBasedShippingMethodsResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace spec\Sylius\Component\Core\Resolver;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Addressing\Matcher\ZoneMatcherInterface;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Core\Model\AddressInterface;
Expand All @@ -20,6 +21,7 @@
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Core\Resolver\ZoneAndChannelBasedShippingMethodsResolver;
use Sylius\Component\Shipping\Checker\ShippingMethodEligibilityCheckerInterface;
use Sylius\Component\Shipping\Model\ShippingSubjectInterface;
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
Expand All @@ -33,9 +35,10 @@ final class ZoneAndChannelBasedShippingMethodsResolverSpec extends ObjectBehavio
{
function let(
ShippingMethodRepositoryInterface $shippingMethodRepository,
ZoneMatcherInterface $zoneMatcher
ZoneMatcherInterface $zoneMatcher,
ShippingMethodEligibilityCheckerInterface $eligibilityChecker
) {
$this->beConstructedWith($shippingMethodRepository, $zoneMatcher);
$this->beConstructedWith($shippingMethodRepository, $zoneMatcher, $eligibilityChecker);
}

function it_is_initializable()
Expand All @@ -49,6 +52,7 @@ function it_implements_shipping_methods_by_zones_and_channel_resolver_interface(
}

function it_returns_shipping_methods_matched_for_shipment_order_shipping_address_and_order_channel(
ShippingMethodEligibilityCheckerInterface $eligibilityChecker,
AddressInterface $address,
ChannelInterface $channel,
OrderInterface $order,
Expand All @@ -71,10 +75,14 @@ function it_returns_shipping_methods_matched_for_shipment_order_shipping_address
->willReturn([$firstShippingMethod, $secondShippingMethod])
;

$eligibilityChecker->isEligible($shipment, $firstShippingMethod)->willReturn(true);
$eligibilityChecker->isEligible($shipment, $secondShippingMethod)->willReturn(true);

$this->getSupportedMethods($shipment)->shouldReturn([$firstShippingMethod, $secondShippingMethod]);
}

function it_returns_empty_array_if_zone_matcher_could_not_match_any_zone(
ShippingMethodEligibilityCheckerInterface $eligibilityChecker,
OrderInterface $order,
AddressInterface $address,
ChannelInterface $channel,
Expand All @@ -90,6 +98,36 @@ function it_returns_empty_array_if_zone_matcher_could_not_match_any_zone(
$this->getSupportedMethods($shipment)->shouldReturn([]);
}

function it_returns_only_shipping_methods_that_are_eligible(
ShippingMethodEligibilityCheckerInterface $eligibilityChecker,
AddressInterface $address,
ChannelInterface $channel,
OrderInterface $order,
ShipmentInterface $shipment,
ShippingMethodInterface $firstShippingMethod,
ShippingMethodInterface $secondShippingMethod,
ShippingMethodRepositoryInterface $shippingMethodRepository,
ZoneInterface $firstZone,
ZoneInterface $secondZone,
ZoneMatcherInterface $zoneMatcher
) {
$shipment->getOrder()->willReturn($order);
$order->getShippingAddress()->willReturn($address);
$order->getChannel()->willReturn($channel);

$zoneMatcher->matchAll($address)->willReturn([$firstZone, $secondZone]);

$eligibilityChecker->isEligible($shipment, $firstShippingMethod)->willReturn(false);
$eligibilityChecker->isEligible($shipment, $secondShippingMethod)->willReturn(true);

$shippingMethodRepository
->findEnabledForZonesAndChannel([$firstZone, $secondZone], $channel)
->willReturn([$firstShippingMethod, $secondShippingMethod])
;

$this->getSupportedMethods($shipment)->shouldReturn([$secondShippingMethod]);
}

function it_supports_shipments_with_order_and_its_shipping_address_defined(
OrderInterface $order,
AddressInterface $address,
Expand Down

0 comments on commit e86942f

Please sign in to comment.