|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Services\Application\Handlers\PromoCode; |
| 4 | + |
| 5 | +use HiEvents\DomainObjects\Enums\PromoCodeDiscountTypeEnum; |
| 6 | +use HiEvents\DomainObjects\EventDomainObject; |
| 7 | +use HiEvents\DomainObjects\PromoCodeDomainObject; |
| 8 | +use HiEvents\Exceptions\ResourceNotFoundException; |
| 9 | +use HiEvents\Repository\Interfaces\EventRepositoryInterface; |
| 10 | +use HiEvents\Repository\Interfaces\PromoCodeRepositoryInterface; |
| 11 | +use HiEvents\Services\Application\Handlers\PromoCode\DTO\UpsertPromoCodeDTO; |
| 12 | +use HiEvents\Services\Application\Handlers\PromoCode\UpdatePromoCodeHandler; |
| 13 | +use HiEvents\Services\Domain\Product\EventProductValidationService; |
| 14 | +use Mockery as m; |
| 15 | +use Tests\TestCase; |
| 16 | + |
| 17 | +class UpdatePromoCodeHandlerTest extends TestCase |
| 18 | +{ |
| 19 | + private PromoCodeRepositoryInterface $promoCodeRepository; |
| 20 | + private EventProductValidationService $eventProductValidationService; |
| 21 | + private EventRepositoryInterface $eventRepository; |
| 22 | + private UpdatePromoCodeHandler $handler; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->promoCodeRepository = m::mock(PromoCodeRepositoryInterface::class); |
| 29 | + $this->eventProductValidationService = m::mock(EventProductValidationService::class); |
| 30 | + $this->eventRepository = m::mock(EventRepositoryInterface::class); |
| 31 | + |
| 32 | + $this->handler = new UpdatePromoCodeHandler( |
| 33 | + $this->promoCodeRepository, |
| 34 | + $this->eventProductValidationService, |
| 35 | + $this->eventRepository |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + public function testHandleThrowsExceptionWhenPromoCodeNotFoundForEvent(): void |
| 40 | + { |
| 41 | + $promoCodeId = 1; |
| 42 | + $eventId = 2; |
| 43 | + $dto = new UpsertPromoCodeDTO( |
| 44 | + code: 'testcode', |
| 45 | + event_id: $eventId, |
| 46 | + applicable_product_ids: [], |
| 47 | + discount_type: PromoCodeDiscountTypeEnum::PERCENTAGE, |
| 48 | + discount: 10.0, |
| 49 | + expiry_date: null, |
| 50 | + max_allowed_usages: null |
| 51 | + ); |
| 52 | + |
| 53 | + $this->promoCodeRepository |
| 54 | + ->shouldReceive('findFirstWhere') |
| 55 | + ->once() |
| 56 | + ->with([ |
| 57 | + 'id' => $promoCodeId, |
| 58 | + 'event_id' => $eventId, |
| 59 | + ]) |
| 60 | + ->andReturn(null); |
| 61 | + |
| 62 | + $this->expectException(ResourceNotFoundException::class); |
| 63 | + $this->expectExceptionMessage('Promo code not found'); |
| 64 | + |
| 65 | + $this->handler->handle($promoCodeId, $dto); |
| 66 | + } |
| 67 | + |
| 68 | + public function testHandleVerifiesPromoCodeBelongsToEvent(): void |
| 69 | + { |
| 70 | + $promoCodeId = 1; |
| 71 | + $eventIdFromRequest = 2; |
| 72 | + $attackerEventId = 999; |
| 73 | + |
| 74 | + $dto = new UpsertPromoCodeDTO( |
| 75 | + code: 'testcode', |
| 76 | + event_id: $attackerEventId, |
| 77 | + applicable_product_ids: [], |
| 78 | + discount_type: PromoCodeDiscountTypeEnum::PERCENTAGE, |
| 79 | + discount: 10.0, |
| 80 | + expiry_date: null, |
| 81 | + max_allowed_usages: null |
| 82 | + ); |
| 83 | + |
| 84 | + $this->promoCodeRepository |
| 85 | + ->shouldReceive('findFirstWhere') |
| 86 | + ->once() |
| 87 | + ->with([ |
| 88 | + 'id' => $promoCodeId, |
| 89 | + 'event_id' => $attackerEventId, |
| 90 | + ]) |
| 91 | + ->andReturn(null); |
| 92 | + |
| 93 | + $this->promoCodeRepository |
| 94 | + ->shouldNotReceive('updateFromArray'); |
| 95 | + |
| 96 | + $this->expectException(ResourceNotFoundException::class); |
| 97 | + |
| 98 | + $this->handler->handle($promoCodeId, $dto); |
| 99 | + } |
| 100 | + |
| 101 | + public function testHandleSuccessfullyUpdatesPromoCodeWhenOwnershipVerified(): void |
| 102 | + { |
| 103 | + $promoCodeId = 1; |
| 104 | + $eventId = 2; |
| 105 | + $dto = new UpsertPromoCodeDTO( |
| 106 | + code: 'testcode', |
| 107 | + event_id: $eventId, |
| 108 | + applicable_product_ids: [], |
| 109 | + discount_type: PromoCodeDiscountTypeEnum::PERCENTAGE, |
| 110 | + discount: 10.0, |
| 111 | + expiry_date: null, |
| 112 | + max_allowed_usages: null |
| 113 | + ); |
| 114 | + |
| 115 | + $existingPromoCode = m::mock(PromoCodeDomainObject::class); |
| 116 | + $existingPromoCode->shouldReceive('getId')->andReturn($promoCodeId); |
| 117 | + |
| 118 | + $event = m::mock(EventDomainObject::class); |
| 119 | + $event->shouldReceive('getTimezone')->andReturn('UTC'); |
| 120 | + |
| 121 | + $updatedPromoCode = m::mock(PromoCodeDomainObject::class); |
| 122 | + |
| 123 | + $this->promoCodeRepository |
| 124 | + ->shouldReceive('findFirstWhere') |
| 125 | + ->once() |
| 126 | + ->with([ |
| 127 | + 'id' => $promoCodeId, |
| 128 | + 'event_id' => $eventId, |
| 129 | + ]) |
| 130 | + ->andReturn($existingPromoCode); |
| 131 | + |
| 132 | + $this->eventProductValidationService |
| 133 | + ->shouldReceive('validateProductIds') |
| 134 | + ->once(); |
| 135 | + |
| 136 | + $this->promoCodeRepository |
| 137 | + ->shouldReceive('findFirstWhere') |
| 138 | + ->once() |
| 139 | + ->with([ |
| 140 | + 'event_id' => $eventId, |
| 141 | + 'code' => 'testcode', |
| 142 | + ]) |
| 143 | + ->andReturn($existingPromoCode); |
| 144 | + |
| 145 | + $this->eventRepository |
| 146 | + ->shouldReceive('findById') |
| 147 | + ->once() |
| 148 | + ->with($eventId) |
| 149 | + ->andReturn($event); |
| 150 | + |
| 151 | + $this->promoCodeRepository |
| 152 | + ->shouldReceive('updateFromArray') |
| 153 | + ->once() |
| 154 | + ->andReturn($updatedPromoCode); |
| 155 | + |
| 156 | + $result = $this->handler->handle($promoCodeId, $dto); |
| 157 | + |
| 158 | + $this->assertSame($updatedPromoCode, $result); |
| 159 | + } |
| 160 | + |
| 161 | + protected function tearDown(): void |
| 162 | + { |
| 163 | + m::close(); |
| 164 | + parent::tearDown(); |
| 165 | + } |
| 166 | +} |
0 commit comments