forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
153 additions
and
13 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/Sylius/Bundle/OrderBundle/ChangesResetter/CartChangesResetter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\OrderBundle\ChangesResetter; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\UnitOfWork; | ||
use Sylius\Component\Order\Model\OrderInterface; | ||
|
||
final class CartChangesResetter implements CartChangesResetterInterface | ||
{ | ||
public function __construct(private readonly EntityManagerInterface $manager) | ||
{ | ||
} | ||
|
||
public function resetChanges(OrderInterface $cart): void | ||
{ | ||
if (!$this->manager->contains($cart)) { | ||
return; | ||
} | ||
|
||
$uow = $this->manager->getUnitOfWork(); | ||
|
||
foreach ($cart->getItems() as $item) { | ||
foreach ($item->getUnits() as $unit) { | ||
if ($uow->getEntityState($unit) === UnitOfWork::STATE_NEW) { | ||
$item->removeUnit($unit); | ||
} | ||
} | ||
$this->manager->refresh($item); | ||
} | ||
$this->manager->refresh($cart); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Sylius/Bundle/OrderBundle/ChangesResetter/CartChangesResetterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\OrderBundle\ChangesResetter; | ||
|
||
use Sylius\Component\Order\Model\OrderInterface; | ||
|
||
interface CartChangesResetterInterface | ||
{ | ||
public function resetChanges(OrderInterface $cart): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/Sylius/Bundle/OrderBundle/spec/ChangesResetter/CartChangesResetterSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\OrderBundle\ChangesResetter; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\UnitOfWork; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Bundle\OrderBundle\ChangesResetter\CartChangesResetter; | ||
use Sylius\Component\Order\Model\OrderInterface; | ||
use Sylius\Component\Order\Model\OrderItemInterface; | ||
use Sylius\Component\Order\Model\OrderItemUnitInterface; | ||
|
||
final class CartChangesResetterSpec extends ObjectBehavior | ||
{ | ||
function let(EntityManagerInterface $manager): void | ||
{ | ||
$this->beConstructedWith($manager); | ||
} | ||
|
||
function it_is_initializable(): void | ||
{ | ||
$this->shouldHaveType(CartChangesResetter::class); | ||
} | ||
|
||
function it_does_nothing_if_cart_is_not_managed( | ||
EntityManagerInterface $manager, | ||
OrderInterface $cart | ||
): void { | ||
$manager->contains($cart)->willReturn(false); | ||
|
||
$manager->refresh($cart)->shouldNotBeCalled(); | ||
|
||
$this->resetChanges($cart); | ||
} | ||
|
||
function it_resets_changes_for_cart_items_and_units( | ||
EntityManagerInterface $manager, | ||
UnitOfWork $unitOfWork, | ||
OrderInterface $cart, | ||
OrderItemInterface $item, | ||
OrderItemUnitInterface $unitNew, | ||
OrderItemUnitInterface $unitExisting, | ||
Collection $itemsCollection | ||
): void { | ||
$manager->contains($cart)->willReturn(true); | ||
$manager->getUnitOfWork()->willReturn($unitOfWork); | ||
|
||
$cart->getItems()->willReturn(new ArrayCollection([$item->getWrappedObject()])); | ||
|
||
$item->getUnits()->willReturn(new ArrayCollection([$unitNew->getWrappedObject(), $unitExisting->getWrappedObject()])); | ||
|
||
$unitOfWork->getEntityState($unitNew)->willReturn(UnitOfWork::STATE_NEW); | ||
$unitOfWork->getEntityState($unitExisting)->willReturn(UnitOfWork::STATE_MANAGED); | ||
|
||
$item->removeUnit($unitNew)->shouldBeCalled(); | ||
$manager->refresh($item)->shouldBeCalled(); | ||
$manager->refresh($cart)->shouldBeCalled(); | ||
|
||
$this->resetChanges($cart); | ||
} | ||
|
||
|
||
} |