Skip to content

Commit 16992c3

Browse files
committed
Add tests
1 parent 839c9df commit 16992c3

File tree

4 files changed

+445
-0
lines changed

4 files changed

+445
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusRestockNotificationPlugin\Tests\EmailManager;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Prophecy\Prophecy\ObjectProphecy;
10+
use Setono\SyliusRestockNotificationPlugin\EmailManager\RestockNotificationEmailManager;
11+
use Setono\SyliusRestockNotificationPlugin\Mailer\Emails;
12+
use Setono\SyliusRestockNotificationPlugin\Model\RestockNotificationRequestInterface;
13+
use Setono\SyliusRestockNotificationPlugin\Resolver\ImageUrlResolverInterface;
14+
use Sylius\Component\Channel\Model\ChannelInterface;
15+
use Sylius\Component\Mailer\Sender\SenderInterface;
16+
17+
final class RestockNotificationEmailManagerTest extends TestCase
18+
{
19+
use ProphecyTrait;
20+
21+
/** @var ObjectProphecy<SenderInterface> */
22+
private ObjectProphecy $sender;
23+
24+
/** @var ObjectProphecy<ImageUrlResolverInterface> */
25+
private ObjectProphecy $imageUrlResolver;
26+
27+
protected function setUp(): void
28+
{
29+
$this->sender = $this->prophesize(SenderInterface::class);
30+
$this->imageUrlResolver = $this->prophesize(ImageUrlResolverInterface::class);
31+
}
32+
33+
/**
34+
* @test
35+
*/
36+
public function it_sends_email_with_correct_parameters(): void
37+
{
38+
$emailManager = new RestockNotificationEmailManager(
39+
$this->sender->reveal(),
40+
$this->imageUrlResolver->reveal(),
41+
);
42+
43+
$email = '[email protected]';
44+
$localeCode = 'en_US';
45+
$imageUrl = 'https://example.com/image.jpg';
46+
47+
$channel = $this->prophesize(ChannelInterface::class);
48+
$request = $this->prophesize(RestockNotificationRequestInterface::class);
49+
50+
$request->getChannel()->willReturn($channel->reveal());
51+
$request->getLocaleCode()->willReturn($localeCode);
52+
$request->getEmail()->willReturn($email);
53+
54+
$this->imageUrlResolver->resolve($request->reveal())->willReturn($imageUrl);
55+
56+
$this->sender->send(
57+
Emails::RESTOCK_NOTIFICATION_REQUEST,
58+
[$email],
59+
[
60+
'restockNotificationRequest' => $request->reveal(),
61+
'channel' => $channel->reveal(),
62+
'localeCode' => $localeCode,
63+
'imageUrl' => $imageUrl,
64+
],
65+
)->shouldBeCalled();
66+
67+
$emailManager->sendRestockNotificationEmail($request->reveal());
68+
}
69+
70+
/**
71+
* @test
72+
*/
73+
public function it_throws_exception_when_channel_is_null(): void
74+
{
75+
$emailManager = new RestockNotificationEmailManager(
76+
$this->sender->reveal(),
77+
$this->imageUrlResolver->reveal(),
78+
);
79+
80+
$request = $this->prophesize(RestockNotificationRequestInterface::class);
81+
$request->getChannel()->willReturn(null);
82+
83+
$this->expectException(\InvalidArgumentException::class);
84+
85+
$emailManager->sendRestockNotificationEmail($request->reveal());
86+
}
87+
88+
/**
89+
* @test
90+
*/
91+
public function it_throws_exception_when_locale_code_is_null(): void
92+
{
93+
$emailManager = new RestockNotificationEmailManager(
94+
$this->sender->reveal(),
95+
$this->imageUrlResolver->reveal(),
96+
);
97+
98+
$channel = $this->prophesize(ChannelInterface::class);
99+
$request = $this->prophesize(RestockNotificationRequestInterface::class);
100+
101+
$request->getChannel()->willReturn($channel->reveal());
102+
$request->getLocaleCode()->willReturn(null);
103+
104+
$this->expectException(\InvalidArgumentException::class);
105+
106+
$emailManager->sendRestockNotificationEmail($request->reveal());
107+
}
108+
109+
/**
110+
* @test
111+
*/
112+
public function it_throws_exception_when_email_is_null(): void
113+
{
114+
$emailManager = new RestockNotificationEmailManager(
115+
$this->sender->reveal(),
116+
$this->imageUrlResolver->reveal(),
117+
);
118+
119+
$channel = $this->prophesize(ChannelInterface::class);
120+
$request = $this->prophesize(RestockNotificationRequestInterface::class);
121+
122+
$request->getChannel()->willReturn($channel->reveal());
123+
$request->getLocaleCode()->willReturn('en_US');
124+
$request->getEmail()->willReturn(null);
125+
126+
$this->expectException(\InvalidArgumentException::class);
127+
128+
$emailManager->sendRestockNotificationEmail($request->reveal());
129+
}
130+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusRestockNotificationPlugin\Tests\Message\Handler;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Prophecy\Prophecy\ObjectProphecy;
10+
use Setono\SyliusRestockNotificationPlugin\Message\Command\Notify;
11+
use Setono\SyliusRestockNotificationPlugin\Message\Handler\NotifyHandler;
12+
use Setono\SyliusRestockNotificationPlugin\Model\RestockNotificationRequestInterface;
13+
use Setono\SyliusRestockNotificationPlugin\Notifier\NotifierInterface;
14+
use Setono\SyliusRestockNotificationPlugin\Repository\RestockNotificationRequestRepositoryInterface;
15+
16+
final class NotifyHandlerTest extends TestCase
17+
{
18+
use ProphecyTrait;
19+
20+
/** @var ObjectProphecy<RestockNotificationRequestRepositoryInterface> */
21+
private ObjectProphecy $repository;
22+
23+
/** @var ObjectProphecy<NotifierInterface> */
24+
private ObjectProphecy $notifier;
25+
26+
protected function setUp(): void
27+
{
28+
$this->repository = $this->prophesize(RestockNotificationRequestRepositoryInterface::class);
29+
$this->notifier = $this->prophesize(NotifierInterface::class);
30+
}
31+
32+
/**
33+
* @test
34+
*/
35+
public function it_does_nothing_when_restock_notification_request_is_not_found(): void
36+
{
37+
$handler = new NotifyHandler(
38+
$this->repository->reveal(),
39+
$this->notifier->reveal(),
40+
);
41+
42+
$requestId = 123;
43+
$message = new Notify($requestId);
44+
45+
$this->repository->findOneByIdInState($requestId, RestockNotificationRequestInterface::STATE_PENDING)
46+
->willReturn(null);
47+
48+
$handler($message);
49+
50+
// Assert that notify was not called on the notifier
51+
$this->notifier->notify(\Prophecy\Argument::any())->shouldNotHaveBeenCalled();
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function it_notifies_when_restock_notification_request_is_found(): void
58+
{
59+
$handler = new NotifyHandler(
60+
$this->repository->reveal(),
61+
$this->notifier->reveal(),
62+
);
63+
64+
$requestId = 123;
65+
$message = new Notify($requestId);
66+
67+
$request = $this->prophesize(RestockNotificationRequestInterface::class);
68+
69+
$this->repository->findOneByIdInState($requestId, RestockNotificationRequestInterface::STATE_PENDING)
70+
->willReturn($request->reveal());
71+
72+
$handler($message);
73+
74+
// Assert that notify was called on the notifier with the request
75+
$this->notifier->notify($request->reveal())->shouldHaveBeenCalled();
76+
}
77+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Setono\SyliusRestockNotificationPlugin\Tests\Message\Handler;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Prophecy\PhpUnit\ProphecyTrait;
9+
use Prophecy\Prophecy\ObjectProphecy;
10+
use Setono\SyliusRestockNotificationPlugin\Message\Command\Notify;
11+
use Setono\SyliusRestockNotificationPlugin\Message\Event\ProductVariantRestocked;
12+
use Setono\SyliusRestockNotificationPlugin\Message\Handler\ProductVariantRestockedHandler;
13+
use Setono\SyliusRestockNotificationPlugin\Model\RestockNotificationRequestInterface;
14+
use Setono\SyliusRestockNotificationPlugin\Repository\RestockNotificationRequestRepositoryInterface;
15+
use Sylius\Component\Core\Model\ProductVariantInterface;
16+
use Sylius\Component\Product\Repository\ProductVariantRepositoryInterface;
17+
use Symfony\Component\Messenger\Envelope;
18+
use Symfony\Component\Messenger\MessageBusInterface;
19+
20+
final class ProductVariantRestockedHandlerTest extends TestCase
21+
{
22+
use ProphecyTrait;
23+
24+
/** @var ObjectProphecy<ProductVariantRepositoryInterface> */
25+
private ObjectProphecy $productVariantRepository;
26+
27+
/** @var ObjectProphecy<RestockNotificationRequestRepositoryInterface> */
28+
private ObjectProphecy $restockNotificationRequestRepository;
29+
30+
/** @var ObjectProphecy<MessageBusInterface> */
31+
private ObjectProphecy $commandBus;
32+
33+
protected function setUp(): void
34+
{
35+
$this->productVariantRepository = $this->prophesize(ProductVariantRepositoryInterface::class);
36+
$this->restockNotificationRequestRepository = $this->prophesize(RestockNotificationRequestRepositoryInterface::class);
37+
$this->commandBus = $this->prophesize(MessageBusInterface::class);
38+
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function it_does_nothing_when_product_variant_is_not_found(): void
44+
{
45+
$handler = new ProductVariantRestockedHandler(
46+
$this->productVariantRepository->reveal(),
47+
$this->restockNotificationRequestRepository->reveal(),
48+
$this->commandBus->reveal(),
49+
);
50+
51+
$productVariantId = 123;
52+
$message = new ProductVariantRestocked($productVariantId);
53+
54+
$this->productVariantRepository->find($productVariantId)->willReturn(null);
55+
56+
$handler($message);
57+
58+
// Assert that findByProductVariant was not called on the repository
59+
$this->restockNotificationRequestRepository->findByProductVariant(\Prophecy\Argument::any())->shouldNotHaveBeenCalled();
60+
61+
// Assert that dispatch was not called on the command bus
62+
$this->commandBus->dispatch(\Prophecy\Argument::any())->shouldNotHaveBeenCalled();
63+
}
64+
65+
/**
66+
* @test
67+
*/
68+
public function it_dispatches_notify_commands_for_each_restock_notification_request(): void
69+
{
70+
$handler = new ProductVariantRestockedHandler(
71+
$this->productVariantRepository->reveal(),
72+
$this->restockNotificationRequestRepository->reveal(),
73+
$this->commandBus->reveal(),
74+
);
75+
76+
$productVariantId = 123;
77+
$message = new ProductVariantRestocked($productVariantId);
78+
79+
$productVariant = $this->prophesize(ProductVariantInterface::class);
80+
$this->productVariantRepository->find($productVariantId)->willReturn($productVariant->reveal());
81+
82+
$request1 = $this->prophesize(RestockNotificationRequestInterface::class);
83+
$request2 = $this->prophesize(RestockNotificationRequestInterface::class);
84+
85+
$this->restockNotificationRequestRepository->findByProductVariant($productVariant->reveal())
86+
->willReturn([$request1->reveal(), $request2->reveal()]);
87+
88+
// Mock the dispatch method to return an envelope
89+
$this->commandBus->dispatch(new Notify($request1->reveal()))
90+
->willReturn(new Envelope(new Notify($request1->reveal())));
91+
$this->commandBus->dispatch(new Notify($request2->reveal()))
92+
->willReturn(new Envelope(new Notify($request2->reveal())));
93+
94+
$handler($message);
95+
96+
// Assert that dispatch was called twice on the command bus
97+
$this->commandBus->dispatch(\Prophecy\Argument::type(Notify::class))->shouldHaveBeenCalledTimes(2);
98+
}
99+
}

0 commit comments

Comments
 (0)