Merge pull request #6665 from michalmarcinkowski/remove-last-shipment

[BC Break] Remove getLastShipment method from Order
This commit is contained in:
Paweł Jędrzejewski 2016-11-15 11:46:25 +01:00 committed by GitHub
commit e6c19b30af
5 changed files with 58 additions and 68 deletions

View file

@ -14,6 +14,7 @@ namespace Sylius\Behat\Context\Ui;
use Behat\Behat\Context\Context;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Test\Services\EmailCheckerInterface;
use Webmozart\Assert\Assert;
@ -100,7 +101,7 @@ final class EmailContext implements Context
public function anEmailWithShipmentDetailsOfOrderShouldBeSentTo(OrderInterface $order, $recipient)
{
$this->assertEmailContainsMessageTo($order->getNumber(), $recipient);
$this->assertEmailContainsMessageTo($order->getLastShipment()->getMethod()->getName(), $recipient);
$this->assertEmailContainsMessageTo($this->getShippingMethodName($order), $recipient);
$tracking = $this->sharedStorage->get('tracking_code');
$this->assertEmailContainsMessageTo($tracking, $recipient);
@ -117,4 +118,20 @@ final class EmailContext implements Context
sprintf('Message "%s" was not sent to "%s".', $message, $recipient)
);
}
/**
* @param OrderInterface $order
*
* @return string
*/
private function getShippingMethodName(OrderInterface $order)
{
/** @var ShipmentInterface $shipment */
$shipment = $order->getShipments()->first();
if (false === $shipment) {
throw new \LogicException('Order should have at least one shipment.');
}
return $shipment->getMethod()->getName();
}
}

View file

@ -368,7 +368,7 @@ class Order extends BaseOrder implements OrderInterface
}
/**
* @return null|BaseCouponInterface
* {@inheritdoc}
*/
public function getPromotionCoupon()
{
@ -467,27 +467,6 @@ class Order extends BaseOrder implements OrderInterface
$this->shippingState = $state;
}
/**
* Gets the last updated shipment of the order
*
* @return false|ShipmentInterface
*/
public function getLastShipment()
{
if ($this->shipments->isEmpty()) {
return false;
}
$last = $this->shipments->first();
foreach ($this->shipments as $shipment) {
if ($shipment->getUpdatedAt() > $last->getUpdatedAt()) {
$last = $shipment;
}
}
return $last;
}
/**
* {@inheritdoc}
*/

View file

@ -167,11 +167,6 @@ interface OrderInterface extends
*/
public function setShippingState($state);
/**
* @return ShipmentInterface
*/
public function getLastShipment();
/**
* @return null|PaymentInterface
*/

View file

@ -14,6 +14,8 @@ namespace Sylius\Component\Core\Taxation\Applicator;
use Sylius\Component\Addressing\Model\ZoneInterface;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
use Sylius\Component\Taxation\Calculator\CalculatorInterface;
use Sylius\Component\Taxation\Resolver\TaxRateResolverInterface;
@ -59,24 +61,17 @@ class OrderShipmentTaxesApplicator implements OrderTaxesApplicatorInterface
*/
public function apply(OrderInterface $order, ZoneInterface $zone)
{
$lastShipment = $order->getLastShipment();
if (!$lastShipment) {
$shippingTotal = $order->getShippingTotal();
if (0 === $shippingTotal) {
return;
}
$shippingAdjustments = $order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT);
if ($shippingAdjustments->isEmpty()) {
return;
}
$taxRate = $this->taxRateResolver->resolve($lastShipment->getMethod(), ['zone' => $zone]);
$taxRate = $this->taxRateResolver->resolve($this->getShippingMethod($order), ['zone' => $zone]);
if (null === $taxRate) {
return;
}
$shippingPrice = $order->getShippingTotal();
$taxAmount = $this->calculator->calculate($shippingPrice, $taxRate);
$taxAmount = $this->calculator->calculate($shippingTotal, $taxRate);
if (0 === $taxAmount) {
return;
}
@ -98,4 +93,20 @@ class OrderShipmentTaxesApplicator implements OrderTaxesApplicatorInterface
;
$order->addAdjustment($shippingTaxAdjustment);
}
/**
* @param OrderInterface $order
*
* @return ShippingMethodInterface
*/
private function getShippingMethod(OrderInterface $order)
{
/** @var ShipmentInterface $shipment */
$shipment = $order->getShipments()->first();
if (false === $shipment) {
throw new \LogicException('Order should have at least one shipment.');
}
return $shipment->getMethod();
}
}

View file

@ -11,7 +11,7 @@
namespace spec\Sylius\Component\Core\Taxation\Applicator;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Addressing\Model\ZoneInterface;
@ -55,23 +55,19 @@ final class OrderShipmentTaxesApplicatorSpec extends ObjectBehavior
AdjustmentFactoryInterface $adjustmentsFactory,
TaxRateResolverInterface $taxRateResolver,
AdjustmentInterface $shippingTaxAdjustment,
Collection $shippingAdjustments,
OrderInterface $order,
ShipmentInterface $shipment,
ShippingMethodInterface $shippingMethod,
TaxRateInterface $taxRate,
ZoneInterface $zone
) {
$order->getLastShipment()->willReturn($shipment);
$order->getShipments()->willReturn(new ArrayCollection([$shipment->getWrappedObject()]));
$shipment->getMethod()->willReturn($shippingMethod);
$taxRateResolver->resolve($shippingMethod, ['zone' => $zone])->willReturn($taxRate);
$taxRate->getLabel()->willReturn('Simple tax (10%)');
$taxRate->isIncludedInPrice()->willReturn(false);
$order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
$shippingAdjustments->isEmpty()->willReturn(false);
$order->getShippingTotal()->willReturn(1000);
$calculator->calculate(1000, $taxRate)->willReturn(100);
@ -85,24 +81,20 @@ final class OrderShipmentTaxesApplicatorSpec extends ObjectBehavior
$this->apply($order, $zone);
}
function it_does_nothing_if_there_are_no_shipment_taxes_on_order(
function it_does_nothing_if_the_tax_amount_is_0(
CalculatorInterface $calculator,
AdjustmentFactoryInterface $adjustmentsFactory,
TaxRateResolverInterface $taxRateResolver,
Collection $shippingAdjustments,
OrderInterface $order,
ShipmentInterface $shipment,
ShippingMethodInterface $shippingMethod,
TaxRateInterface $taxRate,
ZoneInterface $zone
) {
$order->getLastShipment()->willReturn($shipment);
$order->getShipments()->willReturn(new ArrayCollection([$shipment->getWrappedObject()]));
$shipment->getMethod()->willReturn($shippingMethod);
$taxRateResolver->resolve($shippingMethod, ['zone' => $zone])->willReturn($taxRate);
$order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
$shippingAdjustments->isEmpty()->willReturn(false);
$order->getShippingTotal()->willReturn(1000);
$calculator->calculate(1000, $taxRate)->willReturn(0);
@ -113,49 +105,45 @@ final class OrderShipmentTaxesApplicatorSpec extends ObjectBehavior
$this->apply($order, $zone);
}
function it_does_nothing_if_order_has_no_shipment(
TaxRateResolverInterface $taxRateResolver,
function it_throws_exception_if_order_has_no_shipment_but_shipment_total_is_greater_than_0(
OrderInterface $order,
ZoneInterface $zone
) {
$order->getLastShipment()->willReturn(null);
$taxRateResolver->resolve(Argument::any())->shouldNotBeCalled();
$order->getShippingTotal()->willReturn(10);
$order->getShipments()->willReturn(new ArrayCollection([]));
$this->apply($order, $zone);
$this->shouldThrow(\LogicException::class)->during('apply', [$order, $zone]);;
}
function it_does_nothing_if_tax_rate_cannot_be_resolved(
CalculatorInterface $calculator,
TaxRateResolverInterface $taxRateResolver,
Collection $shippingAdjustments,
OrderInterface $order,
ShipmentInterface $shipment,
ShippingMethodInterface $shippingMethod,
ZoneInterface $zone
) {
$order->getLastShipment()->willReturn($shipment);
$order->getShippingTotal()->willReturn(100);
$order->getShipments()->willReturn(new ArrayCollection([$shipment->getWrappedObject()]));
$shipment->getMethod()->willReturn($shippingMethod);
$order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
$shippingAdjustments->isEmpty()->willReturn(false);
$taxRateResolver->resolve($shippingMethod, ['zone' => $zone])->willReturn(null);
$calculator->calculate(Argument::any())->shouldNotBeCalled();
$order->addAdjustment(Argument::any())->shouldNotBeCalled();
$this->apply($order, $zone);
}
function it_does_nothing_if_order_has_no_shipping_adjustments(
function it_does_nothing_if_order_has_0_shipping_total(
TaxRateResolverInterface $taxRateResolver,
Collection $shippingAdjustments,
OrderInterface $order,
ShipmentInterface $shipment,
ZoneInterface $zone
) {
$order->getLastShipment()->willReturn($shipment);
$order->getAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT)->willReturn($shippingAdjustments);
$shippingAdjustments->isEmpty()->willReturn(true);
$order->getShippingTotal()->willReturn(0);
$taxRateResolver->resolve(Argument::any())->shouldNotBeCalled();
$order->addAdjustment(Argument::any())->shouldNotBeCalled();
$this->apply($order, $zone);
}