-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathUpdatePromoCodeHandlerTest.php
More file actions
166 lines (138 loc) · 5.16 KB
/
Copy pathUpdatePromoCodeHandlerTest.php
File metadata and controls
166 lines (138 loc) · 5.16 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
<?php
namespace Tests\Unit\Services\Application\Handlers\PromoCode;
use HiEvents\DomainObjects\Enums\PromoCodeDiscountTypeEnum;
use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\PromoCodeDomainObject;
use HiEvents\Exceptions\ResourceNotFoundException;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Repository\Interfaces\PromoCodeRepositoryInterface;
use HiEvents\Services\Application\Handlers\PromoCode\DTO\UpsertPromoCodeDTO;
use HiEvents\Services\Application\Handlers\PromoCode\UpdatePromoCodeHandler;
use HiEvents\Services\Domain\Product\EventProductValidationService;
use Mockery as m;
use Tests\TestCase;
class UpdatePromoCodeHandlerTest extends TestCase
{
private PromoCodeRepositoryInterface $promoCodeRepository;
private EventProductValidationService $eventProductValidationService;
private EventRepositoryInterface $eventRepository;
private UpdatePromoCodeHandler $handler;
protected function setUp(): void
{
parent::setUp();
$this->promoCodeRepository = m::mock(PromoCodeRepositoryInterface::class);
$this->eventProductValidationService = m::mock(EventProductValidationService::class);
$this->eventRepository = m::mock(EventRepositoryInterface::class);
$this->handler = new UpdatePromoCodeHandler(
$this->promoCodeRepository,
$this->eventProductValidationService,
$this->eventRepository
);
}
public function testHandleThrowsExceptionWhenPromoCodeNotFoundForEvent(): void
{
$promoCodeId = 1;
$eventId = 2;
$dto = new UpsertPromoCodeDTO(
code: 'testcode',
event_id: $eventId,
applicable_product_ids: [],
discount_type: PromoCodeDiscountTypeEnum::PERCENTAGE,
discount: 10.0,
expiry_date: null,
max_allowed_usages: null
);
$this->promoCodeRepository
->shouldReceive('findFirstWhere')
->once()
->with([
'id' => $promoCodeId,
'event_id' => $eventId,
])
->andReturn(null);
$this->expectException(ResourceNotFoundException::class);
$this->expectExceptionMessage('Promo code not found');
$this->handler->handle($promoCodeId, $dto);
}
public function testHandleVerifiesPromoCodeBelongsToEvent(): void
{
$promoCodeId = 1;
$eventIdFromRequest = 2;
$attackerEventId = 999;
$dto = new UpsertPromoCodeDTO(
code: 'testcode',
event_id: $attackerEventId,
applicable_product_ids: [],
discount_type: PromoCodeDiscountTypeEnum::PERCENTAGE,
discount: 10.0,
expiry_date: null,
max_allowed_usages: null
);
$this->promoCodeRepository
->shouldReceive('findFirstWhere')
->once()
->with([
'id' => $promoCodeId,
'event_id' => $attackerEventId,
])
->andReturn(null);
$this->promoCodeRepository
->shouldNotReceive('updateFromArray');
$this->expectException(ResourceNotFoundException::class);
$this->handler->handle($promoCodeId, $dto);
}
public function testHandleSuccessfullyUpdatesPromoCodeWhenOwnershipVerified(): void
{
$promoCodeId = 1;
$eventId = 2;
$dto = new UpsertPromoCodeDTO(
code: 'testcode',
event_id: $eventId,
applicable_product_ids: [],
discount_type: PromoCodeDiscountTypeEnum::PERCENTAGE,
discount: 10.0,
expiry_date: null,
max_allowed_usages: null
);
$existingPromoCode = m::mock(PromoCodeDomainObject::class);
$existingPromoCode->shouldReceive('getId')->andReturn($promoCodeId);
$event = m::mock(EventDomainObject::class);
$event->shouldReceive('getTimezone')->andReturn('UTC');
$updatedPromoCode = m::mock(PromoCodeDomainObject::class);
$this->promoCodeRepository
->shouldReceive('findFirstWhere')
->once()
->with([
'id' => $promoCodeId,
'event_id' => $eventId,
])
->andReturn($existingPromoCode);
$this->eventProductValidationService
->shouldReceive('validateProductIds')
->once();
$this->promoCodeRepository
->shouldReceive('findFirstWhere')
->once()
->with([
'event_id' => $eventId,
'code' => 'testcode',
])
->andReturn($existingPromoCode);
$this->eventRepository
->shouldReceive('findById')
->once()
->with($eventId)
->andReturn($event);
$this->promoCodeRepository
->shouldReceive('updateFromArray')
->once()
->andReturn($updatedPromoCode);
$result = $this->handler->handle($promoCodeId, $dto);
$this->assertSame($updatedPromoCode, $result);
}
protected function tearDown(): void
{
m::close();
parent::tearDown();
}
}