-
-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathCreateOrderHandlerTest.php
More file actions
214 lines (177 loc) · 8.44 KB
/
CreateOrderHandlerTest.php
File metadata and controls
214 lines (177 loc) · 8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Tests\Unit\Services\Application\Handlers\Order;
use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\EventSettingDomainObject;
use HiEvents\DomainObjects\OrderDomainObject;
use HiEvents\DomainObjects\OrderItemDomainObject;
use HiEvents\DomainObjects\Status\EventStatus;
use HiEvents\Repository\Interfaces\AffiliateRepositoryInterface;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Repository\Interfaces\PromoCodeRepositoryInterface;
use HiEvents\Services\Application\Handlers\Order\CreateOrderHandler;
use HiEvents\Services\Application\Handlers\Order\DTO\CreateOrderPublicDTO;
use HiEvents\Services\Application\Handlers\Order\DTO\ProductOrderDetailsDTO;
use HiEvents\Services\Domain\Order\OrderItemProcessingService;
use HiEvents\Services\Domain\Order\OrderManagementService;
use HiEvents\Services\Domain\Product\AvailableProductQuantitiesFetchService;
use HiEvents\Services\Domain\Product\DTO\AvailableProductQuantitiesDTO;
use HiEvents\Services\Domain\Product\DTO\AvailableProductQuantitiesResponseDTO;
use HiEvents\Services\Domain\Product\DTO\OrderProductPriceDTO;
use Illuminate\Database\DatabaseManager;
use Illuminate\Validation\ValidationException;
use Mockery;
use Mockery\MockInterface;
use Tests\TestCase;
class CreateOrderHandlerTest extends TestCase
{
private EventRepositoryInterface|MockInterface $eventRepository;
private PromoCodeRepositoryInterface|MockInterface $promoCodeRepository;
private AffiliateRepositoryInterface|MockInterface $affiliateRepository;
private OrderManagementService|MockInterface $orderManagementService;
private OrderItemProcessingService|MockInterface $orderItemProcessingService;
private AvailableProductQuantitiesFetchService|MockInterface $availabilityService;
private DatabaseManager|MockInterface $databaseManager;
private CreateOrderHandler $handler;
protected function setUp(): void
{
parent::setUp();
$this->eventRepository = Mockery::mock(EventRepositoryInterface::class);
$this->promoCodeRepository = Mockery::mock(PromoCodeRepositoryInterface::class);
$this->affiliateRepository = Mockery::mock(AffiliateRepositoryInterface::class);
$this->orderManagementService = Mockery::mock(OrderManagementService::class);
$this->orderItemProcessingService = Mockery::mock(OrderItemProcessingService::class);
$this->availabilityService = Mockery::mock(AvailableProductQuantitiesFetchService::class);
$this->databaseManager = Mockery::mock(DatabaseManager::class);
$this->databaseManager->shouldReceive('transaction')
->andReturnUsing(fn($callback) => $callback());
$this->handler = new CreateOrderHandler(
$this->eventRepository,
$this->promoCodeRepository,
$this->affiliateRepository,
$this->orderManagementService,
$this->orderItemProcessingService,
$this->availabilityService,
$this->databaseManager,
);
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
public function testAcquiresAdvisoryLockBeforeCreatingOrder(): void
{
$eventId = 42;
$this->databaseManager->shouldReceive('statement')
->once()
->with('SELECT pg_advisory_xact_lock(?)', [$eventId])
->andReturn(true);
$this->setupSuccessfulOrderCreation($eventId);
$result = $this->handler->handle($eventId, $this->createOrderDTO());
$this->assertInstanceOf(OrderDomainObject::class, $result);
}
public function testThrowsWhenProductQuantityExceedsAvailability(): void
{
$eventId = 1;
$this->databaseManager->shouldReceive('statement')->andReturn(true);
$this->setupEventMock($eventId);
$this->orderManagementService->shouldReceive('deleteExistingOrders');
$this->availabilityService->shouldReceive('getAvailableProductQuantities')
->with($eventId, true)
->andReturn(new AvailableProductQuantitiesResponseDTO(
productQuantities: collect([
AvailableProductQuantitiesDTO::fromArray([
'product_id' => 10,
'price_id' => 100,
'product_title' => 'Test',
'price_label' => null,
'quantity_available' => 2,
'quantity_reserved' => 0,
'initial_quantity_available' => 10,
]),
]),
));
$dto = $this->createOrderDTO(quantity: 5);
$this->expectException(ValidationException::class);
$this->handler->handle($eventId, $dto);
}
public function testPassesWhenQuantityIsWithinAvailability(): void
{
$eventId = 1;
$this->databaseManager->shouldReceive('statement')->andReturn(true);
$this->setupSuccessfulOrderCreation($eventId, productId: 10, priceId: 100, available: 5);
$dto = $this->createOrderDTO(quantity: 2);
$result = $this->handler->handle($eventId, $dto);
$this->assertInstanceOf(OrderDomainObject::class, $result);
}
public function testSkipsZeroQuantityProducts(): void
{
$eventId = 1;
$this->databaseManager->shouldReceive('statement')->andReturn(true);
$this->setupSuccessfulOrderCreation($eventId, available: 0);
$dto = $this->createOrderDTO(quantity: 0);
$result = $this->handler->handle($eventId, $dto);
$this->assertInstanceOf(OrderDomainObject::class, $result);
}
private function createOrderDTO(int $productId = 10, int $priceId = 100, int $quantity = 1): CreateOrderPublicDTO
{
return CreateOrderPublicDTO::fromArray([
'is_user_authenticated' => false,
'session_identifier' => 'test-session',
'order_locale' => 'en',
'products' => collect([
ProductOrderDetailsDTO::fromArray([
'product_id' => $productId,
'quantities' => collect([
OrderProductPriceDTO::fromArray([
'price_id' => $priceId,
'quantity' => $quantity,
]),
]),
]),
]),
]);
}
private function setupEventMock(int $eventId): void
{
$eventSettings = Mockery::mock(EventSettingDomainObject::class);
$eventSettings->shouldReceive('getOrderTimeoutInMinutes')->andReturn(15);
$event = Mockery::mock(EventDomainObject::class);
$event->shouldReceive('getId')->andReturn($eventId);
$event->shouldReceive('getStatus')->andReturn(EventStatus::LIVE->name);
$event->shouldReceive('getEventSettings')->andReturn($eventSettings);
$this->eventRepository->shouldReceive('loadRelation')->andReturnSelf();
$this->eventRepository->shouldReceive('findById')->with($eventId)->andReturn($event);
}
private function setupSuccessfulOrderCreation(
int $eventId,
int $productId = 10,
int $priceId = 100,
int $available = 10,
): void
{
$this->setupEventMock($eventId);
$this->orderManagementService->shouldReceive('deleteExistingOrders');
$this->availabilityService->shouldReceive('getAvailableProductQuantities')
->with($eventId, true)
->andReturn(new AvailableProductQuantitiesResponseDTO(
productQuantities: collect([
AvailableProductQuantitiesDTO::fromArray([
'product_id' => $productId,
'price_id' => $priceId,
'product_title' => 'Test Product',
'price_label' => null,
'quantity_available' => $available,
'quantity_reserved' => 0,
'initial_quantity_available' => 100,
]),
]),
));
$order = Mockery::mock(OrderDomainObject::class);
$order->shouldReceive('getId')->andReturn(1);
$this->orderManagementService->shouldReceive('createNewOrder')->andReturn($order);
$orderItems = collect([Mockery::mock(OrderItemDomainObject::class)]);
$this->orderItemProcessingService->shouldReceive('process')->andReturn($orderItems);
$this->orderManagementService->shouldReceive('updateOrderTotals')->andReturn($order);
}
}