Skip to content

Commit

Permalink
[ApiBundle] Extract AsMessageHandler check in PHPSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed Sep 6, 2024
1 parent f8837ae commit 914e17a
Show file tree
Hide file tree
Showing 29 changed files with 111 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
use Sylius\Bundle\CoreBundle\Resolver\CustomerResolverInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Webmozart\Assert\Assert;

#[AsMessageHandler]
final readonly class UpdateCartHandler
{
/** @param OrderRepositoryInterface<OrderInterface> $orderRepository */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use Sylius\Bundle\ApiBundle\Exception\UserNotFoundException;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final readonly class RemoveShopUserHandler
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
use Prophecy\Argument;
use Sylius\Bundle\ApiBundle\Changer\PaymentMethodChangerInterface;
use Sylius\Bundle\ApiBundle\Command\Account\ChangePaymentMethod;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;

final class ChangePaymentMethodHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
PaymentMethodChangerInterface $paymentMethodChanger,
OrderRepositoryInterface $orderRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
use Sylius\Bundle\ApiBundle\Command\Account\RegisterShopUser;
use Sylius\Bundle\ApiBundle\Command\Account\SendAccountRegistrationEmail;
use Sylius\Bundle\ApiBundle\Command\Account\SendShopUserVerificationEmail;
use Sylius\Bundle\ApiBundle\CommandHandler\Account\RegisterShopUserHandler;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Bundle\CoreBundle\Resolver\CustomerResolverInterface;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\User\Security\Generator\GeneratorInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;
use Webmozart\Assert\Assert;

final class RegisterShopUserHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
FactoryInterface $shopUserFactory,
ObjectManager $shopUserManager,
Expand All @@ -52,14 +52,6 @@ function let(
);
}

function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(RegisterShopUserHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}

function it_creates_a_shop_user_with_given_data(
FactoryInterface $shopUserFactory,
ObjectManager $shopUserManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Prophecy\Argument;
use Sylius\Bundle\ApiBundle\Command\Account\RequestResetPasswordToken;
use Sylius\Bundle\ApiBundle\Command\Account\SendResetPasswordEmail;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Bundle\CoreBundle\CommandHandler\Admin\Account\RequestResetPasswordEmailHandler;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
Expand All @@ -30,6 +31,8 @@

final class RequestResetPasswordTokenHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
UserRepositoryInterface $userRepository,
MessageBusInterface $messageBus,
Expand All @@ -39,14 +42,6 @@ function let(
$this->beConstructedWith($userRepository, $messageBus, $generator, $clock);
}

function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(RequestResetPasswordEmailHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}

function it_handles_request_for_password_reset_token(
UserRepositoryInterface $userRepository,
ShopUserInterface $shopUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sylius\Bundle\ApiBundle\Command\Account\RequestShopUserVerification;
use Sylius\Bundle\ApiBundle\Command\Account\SendShopUserVerificationEmail;
use Sylius\Bundle\ApiBundle\CommandHandler\Account\RequestShopUserVerificationHandler;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
Expand All @@ -29,6 +30,8 @@

final class RequestShopUserVerificationHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
UserRepositoryInterface $userRepository,
GeneratorInterface $generator,
Expand All @@ -37,14 +40,6 @@ function let(
$this->beConstructedWith($userRepository, $generator, $messageBus);
}

function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(RequestShopUserVerificationHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}

function it_throws_exception_if_shop_user_does_not_exist(UserRepositoryInterface $userRepository): void
{
$userRepository->find(42)->willReturn(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,20 @@
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Account\ResetPassword;
use Sylius\Bundle\ApiBundle\CommandHandler\Account\ResetPasswordHandler;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Bundle\CoreBundle\Security\UserPasswordResetterInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Webmozart\Assert\Assert;

final class ResetPasswordHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(UserPasswordResetterInterface $userPasswordResetter): void
{
$this->beConstructedWith($userPasswordResetter);
}

function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(ResetPasswordHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}

function it_delegates_password_resetting(UserPasswordResetterInterface $userPasswordResetter): void
{
$command = new ResetPassword('TOKEN');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\ApiBundle\Command\Account\SendAccountRegistrationEmail;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Bundle\CoreBundle\Mailer\AccountRegistrationEmailManagerInterface;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
Expand All @@ -24,6 +25,8 @@

final class SendAccountRegistrationEmailHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
UserRepositoryInterface $shopUserRepository,
ChannelRepositoryInterface $channelRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Account\SendResetPasswordEmail;
use Sylius\Bundle\ApiBundle\CommandHandler\Account\SendResetPasswordEmailHandler;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Bundle\CoreBundle\Mailer\ResetPasswordEmailManagerInterface;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
Expand All @@ -26,6 +27,8 @@

final class SendResetPasswordEmailHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
ChannelRepositoryInterface $channelRepository,
UserRepositoryInterface $userRepository,
Expand All @@ -34,14 +37,6 @@ function let(
$this->beConstructedWith($channelRepository, $userRepository, $resetPasswordEmailManager);
}

function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(SendResetPasswordEmailHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}

function it_sends_message_with_reset_password_token(
ChannelRepositoryInterface $channelRepository,
UserRepositoryInterface $userRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Bundle\ApiBundle\Command\Account\SendShopUserVerificationEmail;
use Sylius\Bundle\ApiBundle\Exception\ChannelNotFoundException;
use Sylius\Bundle\ApiBundle\Exception\UserNotFoundException;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Bundle\CoreBundle\Mailer\AccountVerificationEmailManagerInterface;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
Expand All @@ -26,6 +27,8 @@

final class SendShopUserVerificationEmailHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
UserRepositoryInterface $shopUserRepository,
ChannelRepositoryInterface $channelRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Bundle\ApiBundle\Command\Account\SendAccountRegistrationEmail;
use Sylius\Bundle\ApiBundle\Command\Account\VerifyShopUser;
use Sylius\Bundle\ApiBundle\CommandHandler\Account\VerifyShopUserHandler;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\User\Model\UserInterface;
use Symfony\Component\Clock\ClockInterface;
Expand All @@ -29,6 +30,8 @@

final class VerifyShopUserHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
RepositoryInterface $shopUserRepository,
ClockInterface $clock,
Expand All @@ -37,14 +40,6 @@ function let(
$this->beConstructedWith($shopUserRepository, $clock, $commandBus);
}

function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(VerifyShopUserHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}

function it_verifies_shop_user(
RepositoryInterface $shopUserRepository,
ClockInterface $clock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Cart\AddItemToCart;
use Sylius\Bundle\ApiBundle\CommandHandler\Cart\AddItemToCartHandler;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Component\Core\Factory\CartItemFactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
Expand All @@ -29,6 +30,8 @@

final class AddItemToCartHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
OrderRepositoryInterface $orderRepository,
ProductVariantRepositoryInterface $productVariantRepository,
Expand All @@ -45,14 +48,6 @@ function let(
);
}

function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(AddItemToCartHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}

function it_adds_simple_product_to_cart(
OrderRepositoryInterface $orderRepository,
ProductVariantRepositoryInterface $productVariantRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Cart\BlameCart;
use Sylius\Bundle\ApiBundle\CommandHandler\Cart\BlameCartHandler;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ShopUserInterface;
Expand All @@ -27,6 +28,8 @@

final class BlameCartHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
UserRepositoryInterface $shopUserRepository,
OrderRepositoryInterface $orderRepository,
Expand All @@ -39,14 +42,6 @@ function let(
);
}

function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(BlameCartHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}

function it_blames_cart_with_given_data(
UserRepositoryInterface $shopUserRepository,
OrderRepositoryInterface $orderRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\Cart\ChangeItemQuantityInCart;
use Sylius\Bundle\ApiBundle\CommandHandler\Cart\ChangeItemQuantityInCartHandler;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
Expand All @@ -26,6 +27,8 @@

final class ChangeItemQuantityInCartHandlerSpec extends ObjectBehavior
{
use MessageHandlerAttributeTrait;

function let(
OrderItemRepositoryInterface $orderItemRepository,
OrderItemQuantityModifierInterface $orderItemQuantityModifier,
Expand All @@ -34,14 +37,6 @@ function let(
$this->beConstructedWith($orderItemRepository, $orderItemQuantityModifier, $orderProcessor);
}

function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(ChangeItemQuantityInCartHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}

function it_changes_order_item_quantity(
OrderItemRepositoryInterface $orderItemRepository,
OrderItemQuantityModifierInterface $orderItemQuantityModifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@
use Sylius\Bundle\ApiBundle\Command\Cart\InformAboutCartRecalculation;
use Sylius\Bundle\ApiBundle\CommandHandler\Cart\InformAboutCartRecalculationHandler;
use Sylius\Bundle\ApiBundle\Exception\OrderNoLongerEligibleForPromotion;
use Sylius\Bundle\ApiBundle\spec\CommandHandler\MessageHandlerAttributeTrait;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Webmozart\Assert\Assert;

final class InformAboutCartRecalculationHandlerSpec extends ObjectBehavior
{
function it_is_a_message_handler(): void
{
$messageHandlerAttributes = (new \ReflectionClass(InformAboutCartRecalculationHandler::class))
->getAttributes(AsMessageHandler::class);

Assert::count($messageHandlerAttributes, 1);
}
use MessageHandlerAttributeTrait;

function it_throws_order_no_longer_eligible_for_promotion_exception(): void
{
Expand Down
Loading

0 comments on commit 914e17a

Please sign in to comment.