-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor #481 [Checkout] Extract cart assignment from order complete …
…(lchrusciel) This PR was merged into the 1.0-dev branch. Discussion ---------- Commits ------- 696eb7a [Checkout] Extract cart assignment from order complete
- Loading branch information
Showing
13 changed files
with
175 additions
and
56 deletions.
There are no files selected for viewing
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Command\Cart; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
|
||
final class AssignCustomerToCartSpec extends ObjectBehavior | ||
{ | ||
function let(): void | ||
{ | ||
$this->beConstructedWith('ORDERTOKEN', 'example@customer.com'); | ||
} | ||
|
||
function it_has_order_token(): void | ||
{ | ||
$this->orderToken()->shouldReturn('ORDERTOKEN'); | ||
} | ||
|
||
function it_has_email(): void | ||
{ | ||
$this->email()->shouldReturn('example@customer.com'); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\ShopApiPlugin\Handler\Cart; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\CustomerInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart; | ||
use Sylius\ShopApiPlugin\Provider\CustomerProviderInterface; | ||
|
||
final class AssignCustomerToCartHandlerSpec extends ObjectBehavior | ||
{ | ||
function let(OrderRepositoryInterface $orderRepository, CustomerProviderInterface $customerProvider): void | ||
{ | ||
$this->beConstructedWith($orderRepository, $customerProvider); | ||
} | ||
|
||
function it_handles_assigning_customer_to_cart( | ||
CustomerInterface $customer, | ||
CustomerProviderInterface $customerProvider, | ||
OrderInterface $order, | ||
OrderRepositoryInterface $orderRepository | ||
): void { | ||
$orderRepository->findOneBy(['tokenValue' => 'ORDERTOKEN'])->willReturn($order); | ||
|
||
$customerProvider->provide('example@customer.com')->willReturn($customer); | ||
|
||
$order->setCustomer($customer)->shouldBeCalled(); | ||
|
||
$this(new AssignCustomerToCart('ORDERTOKEN', 'example@customer.com')); | ||
} | ||
|
||
function it_throws_an_exception_if_order_does_not_exist( | ||
OrderRepositoryInterface $orderRepository | ||
): void { | ||
$orderRepository->findOneBy(['tokenValue' => 'ORDERTOKEN'])->willReturn(null); | ||
|
||
$this | ||
->shouldThrow(\InvalidArgumentException::class) | ||
->during('__invoke', [new AssignCustomerToCart('ORDERTOKEN', 'example@customer.com')]) | ||
; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Command\Cart; | ||
|
||
class AssignCustomerToCart | ||
{ | ||
/** @var string */ | ||
protected $orderToken; | ||
|
||
/** @var string */ | ||
protected $email; | ||
|
||
public function __construct(string $orderToken, string $email) | ||
{ | ||
$this->orderToken = $orderToken; | ||
$this->email = $email; | ||
} | ||
|
||
public function orderToken(): string | ||
{ | ||
return $this->orderToken; | ||
} | ||
|
||
public function email(): string | ||
{ | ||
return $this->email; | ||
} | ||
} |
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
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\ShopApiPlugin\Handler\Cart; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Repository\OrderRepositoryInterface; | ||
use Sylius\ShopApiPlugin\Command\Cart\AssignCustomerToCart; | ||
use Sylius\ShopApiPlugin\Provider\CustomerProviderInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class AssignCustomerToCartHandler | ||
{ | ||
/** @var OrderRepositoryInterface */ | ||
private $orderRepository; | ||
|
||
/** @var CustomerProviderInterface */ | ||
private $customerProvider; | ||
|
||
public function __construct(OrderRepositoryInterface $orderRepository, CustomerProviderInterface $customerProvider) | ||
{ | ||
$this->orderRepository = $orderRepository; | ||
$this->customerProvider = $customerProvider; | ||
} | ||
|
||
public function __invoke(AssignCustomerToCart $assignOrderToCustomer): void | ||
{ | ||
/** @var OrderInterface $order */ | ||
$order = $this->orderRepository->findOneBy(['tokenValue' => $assignOrderToCustomer->orderToken()]); | ||
|
||
Assert::notNull($order, sprintf('Order with %s token has not been found.', $assignOrderToCustomer->orderToken())); | ||
|
||
$customer = $this->customerProvider->provide($assignOrderToCustomer->email()); | ||
|
||
$order->setCustomer($customer); | ||
} | ||
} |
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
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
Oops, something went wrong.