minor #14651 Remove unit from shipment (kayue, everwhatever)

This PR was merged into the 1.11 branch.

Discussion
----------

| Q               | A                                                            |
|-----------------|--------------------------------------------------------------|
| Branch?         | 1.11  |
| Bug fix?        | yes  |
| New feature?    | no |
| BC breaks?      | no |
| Deprecations?   | no |
| Related tickets | [ticket](https://github.com/Sylius/Sylius/pull/14133)  |
| License         | MIT                                                          |



Commits
-------

981951509a Disassociate OrderItemUnit from all shipments before removal
3889e688a9 add spec
This commit is contained in:
Jakub Tobiasz 2022-12-22 13:17:18 +01:00 committed by GitHub
commit 11e59732d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View file

@ -291,6 +291,13 @@ class Order extends BaseOrder implements OrderInterface
public function removeShipments(): void
{
// Disassociate OrderItemUnit from all shipments before removal
foreach ($this->shipments as $shipment) {
foreach ($shipment->getUnits() as $unit) {
$shipment->removeUnit($unit);
}
}
$this->shipments->clear();
}

View file

@ -16,6 +16,7 @@ namespace spec\Sylius\Component\Core\Model;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\CatalogPromotionInterface;
@ -31,6 +32,7 @@ use Sylius\Component\Core\OrderShippingStates;
use Sylius\Component\Order\Model\Order as BaseOrder;
use Sylius\Component\Order\Model\OrderInterface;
use Sylius\Component\Promotion\Model\PromotionCouponInterface;
use Sylius\Component\Shipping\Model\ShipmentUnitInterface;
final class OrderSpec extends ObjectBehavior
{
@ -134,13 +136,33 @@ final class OrderSpec extends ObjectBehavior
$this->shouldNotHaveShipment($shipment);
}
function it_removes_shipments(ShipmentInterface $shipment): void
function it_removes_shipments_with_units(
ShipmentInterface $shipment,
ShipmentUnitInterface $shipmentUnit
): void
{
$this->addShipment($shipment);
$this->hasShipment($shipment)->shouldReturn(true);
$shipment->getUnits()->willReturn(new ArrayCollection([$shipmentUnit->getWrappedObject()]));
$this->removeShipments();
$shipment->removeUnit(Argument::any())->shouldBeCalled();
$this->hasShipment($shipment)->shouldReturn(false);
}
function it_removes_shipments_without_units(
ShipmentInterface $shipment,
ShipmentUnitInterface $shipmentUnit
): void
{
$this->addShipment($shipment);
$this->hasShipment($shipment)->shouldReturn(true);
$shipment->getUnits()->willReturn(new ArrayCollection([]));
$this->removeShipments();
$shipment->removeUnit()->shouldNotBeCalled();
$this->hasShipment($shipment)->shouldReturn(false);
}