Skip to content

Commit

Permalink
pr fix, behat + spec fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
SirDomin committed Nov 17, 2020
1 parent 18818a7 commit da13fb2
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 100 deletions.
14 changes: 6 additions & 8 deletions src/Sylius/Bundle/ApiBundle/Command/SendOrderConfirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@

namespace Sylius\Bundle\ApiBundle\Command;

use Sylius\Component\Order\Model\OrderInterface;

class SendOrderConfirmation
{
/** @var OrderInterface */
protected $order;
/** @var string */
protected $orderToken;

public function __construct(OrderInterface $order)
public function __construct(string $orderToken)
{
$this->order = $order;
$this->orderToken = $orderToken;
}

public function order(): OrderInterface
public function orderToken(): string
{
return $this->order;
return $this->orderToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
namespace Sylius\Bundle\ApiBundle\CommandHandler\Checkout;

use SM\Factory\FactoryInterface;
use Sylius\Bundle\ApiBundle\Command\Cart\PickupCart;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\ApiBundle\Event\OrderCompletedEvent;
use Sylius\Bundle\ApiBundle\Event\OrderCompleted;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
Expand All @@ -34,16 +35,16 @@ final class CompleteOrderHandler implements MessageHandlerInterface
private $stateMachineFactory;

/** @var MessageBusInterface */
private $messageBus;
private $eventBus;

public function __construct(
OrderRepositoryInterface $orderRepository,
FactoryInterface $stateMachineFactory,
MessageBusInterface $messageBus
MessageBusInterface $eventBus
) {
$this->orderRepository = $orderRepository;
$this->stateMachineFactory = $stateMachineFactory;
$this->messageBus = $messageBus;
$this->eventBus = $eventBus;
}

public function __invoke(CompleteOrder $completeOrder): OrderInterface
Expand All @@ -68,7 +69,7 @@ public function __invoke(CompleteOrder $completeOrder): OrderInterface

$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);

$this->messageBus->dispatch(new OrderCompletedEvent($orderTokenValue), [new DispatchAfterCurrentBusStamp()]);
$this->eventBus->dispatch(new OrderCompleted($cart->getTokenValue()), [new DispatchAfterCurrentBusStamp()]);

return $cart;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* 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\ApiBundle\CommandHandler;

use Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation;
use Sylius\Bundle\ApiBundle\Event\OrderCompleted;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\OrderRepository;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

final class OrderCompletedHandler
{
/** @var MessageBusInterface */
private $bus;

public function __construct(MessageBusInterface $bus)
{
$this->bus = $bus;
}

public function __invoke(OrderCompleted $orderCompleted): void
{
$this->bus->dispatch(new SendOrderConfirmation($orderCompleted->orderToken()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use Sylius\Bundle\ApiBundle\Command\SendOrderConfirmation;
use Sylius\Bundle\CoreBundle\Mailer\Emails;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

Expand All @@ -24,14 +26,19 @@ final class SendOrderConfirmationHandler implements MessageHandlerInterface
/** @var SenderInterface */
private $emailSender;

public function __construct(SenderInterface $emailSender)
/** @var OrderRepositoryInterface */
private $orderRepository;

public function __construct(SenderInterface $emailSender, OrderRepositoryInterface $orderRepository)
{
$this->emailSender = $emailSender;
$this->orderRepository = $orderRepository;
}

public function __invoke(SendOrderConfirmation $sendOrderConfirmation): void
{
$order = $sendOrderConfirmation->order();
/** @var OrderInterface $order */
$order = $this->orderRepository->findOneByTokenValue($sendOrderConfirmation->orderToken());

$this->emailSender->send(
Emails::ORDER_CONFIRMATION_RESENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sylius\Bundle\ApiBundle\Event;

final class OrderCompletedEvent
final class OrderCompleted
{
/** @var string */
protected $orderToken;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
<service id="Sylius\Bundle\ApiBundle\CommandHandler\Checkout\CompleteOrderHandler">
<argument type="service" id="sylius.repository.order"/>
<argument type="service" id="sm.factory" />
<argument type="service" id="api_platform.message_bus" />
<tag name="messenger.command_handler" bus="sylius_default.bus" />
<argument type="service" id="sylius_default.bus" />
<tag name="messenger.message_handler" bus="sylius_default.bus" />
</service>

<service id="Sylius\Bundle\ApiBundle\CommandHandler\Cart\ChangeItemQuantityInCartHandler">
Expand All @@ -104,7 +104,14 @@

<service id="Sylius\Bundle\ApiBundle\CommandHandler\SendOrderConfirmationHandler">
<argument type="service" id="sylius.email_sender" />
<argument type="service" id="sylius.repository.order" />
<tag name="messenger.message_handler" bus="sylius_default.bus" />
</service>

<service id="Sylius\Bundle\ApiBundle\CommandHandler\OrderCompletedHandler">
<argument type="service" id="message_bus" />
<argument type="service" id="sylius.repository.order" />
<tag name="messenger.message_handler" bus="sylius_default.bus"/>
</service>
</services>
</container>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
namespace spec\Sylius\Bundle\ApiBundle\CommandHandler\Checkout;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SM\Factory\FactoryInterface;
use SM\StateMachine\StateMachineInterface;
use Sylius\Bundle\ApiBundle\Command\Checkout\CompleteOrder;
use Sylius\Bundle\ApiBundle\Event\OrderCompletedEvent;
use Sylius\Bundle\CoreBundle\Mailer\OrderEmailManagerInterface;
use Sylius\Bundle\ApiBundle\Event\OrderCompleted;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;

Expand All @@ -31,17 +30,17 @@ final class CompleteOrderHandlerSpec extends ObjectBehavior
function let(
OrderRepositoryInterface $orderRepository,
FactoryInterface $stateMachineFactory,
MessageBusInterface $messageBus
MessageBusInterface $eventBus
): void {
$this->beConstructedWith($orderRepository, $stateMachineFactory, $messageBus);
$this->beConstructedWith($orderRepository, $stateMachineFactory, $eventBus);
}

function it_handles_order_completion_without_notes(
OrderRepositoryInterface $orderRepository,
StateMachineInterface $stateMachine,
OrderInterface $order,
FactoryInterface $stateMachineFactory,
MessageBusInterface $messageBus
MessageBusInterface $eventBus
): void {
$completeOrder = new CompleteOrder();
$completeOrder->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -56,6 +55,12 @@ function it_handles_order_completion_without_notes(

$stateMachine->apply('complete')->shouldBeCalled();

$orderCompleted = new OrderCompleted('COMPLETED_ORDER_TOKEN');

$eventBus->dispatch($orderCompleted, [new DispatchAfterCurrentBusStamp()])
->willReturn(new Envelope($orderCompleted))
->shouldBeCalled();

$this($completeOrder)->shouldReturn($order);
}

Expand All @@ -64,7 +69,7 @@ function it_handles_order_completion_with_notes(
StateMachineInterface $stateMachine,
OrderInterface $order,
FactoryInterface $stateMachineFactory,
OrderEmailManagerInterface $emailManager
MessageBusInterface $eventBus
): void {
$completeOrder = new CompleteOrder('ThankYou');
$completeOrder->setOrderTokenValue('ORDERTOKEN');
Expand All @@ -79,7 +84,11 @@ function it_handles_order_completion_with_notes(

$stateMachine->apply('complete')->shouldBeCalled();

$emailManager->sendConfirmationEmail($order)->shouldBeCalled();
$orderCompleted = new OrderCompleted('COMPLETED_ORDER_TOKEN');

$eventBus->dispatch($orderCompleted, [new DispatchAfterCurrentBusStamp()])
->willReturn(new Envelope($orderCompleted))
->shouldBeCalled();

$this($completeOrder)->shouldReturn($order);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class SendOrderConfirmationHandlerSpec extends ObjectBehavior
{
function let(
SenderInterface $sender
): void {
$this->beConstructedWith($sender);
function let(SenderInterface $sender, OrderRepositoryInterface $orderRepository): void
{
$this->beConstructedWith($sender, $orderRepository);
}

function it_is_a_message_handler(): void
Expand All @@ -40,9 +40,12 @@ function it_sends_order_confirmation_message(
SendOrderConfirmation $sendOrderConfirmation,
SenderInterface $sender,
CustomerInterface $customer,
ChannelInterface $channel
ChannelInterface $channel,
OrderRepositoryInterface $orderRepository
): void {
$sendOrderConfirmation->order()->willReturn($order);
$sendOrderConfirmation->orderToken()->willReturn('TOKEN');

$orderRepository->findOneByTokenValue('TOKEN')->willReturn($order);

$order->getChannel()->willReturn($channel);
$order->getLocaleCode()->willReturn('pl_PL');
Expand All @@ -60,6 +63,6 @@ function it_sends_order_confirmation_message(
]
)->shouldBeCalled();

$this(new SendOrderConfirmation($order->getWrappedObject()));
$this(new SendOrderConfirmation('TOKEN'));
}
}

0 comments on commit da13fb2

Please sign in to comment.