Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️
PHP attendize-alternativedoor-managementevent-check-inevent-managementevent-registrationevent-ticketingeventbrite-alternativeeventslaravelopen-sourcephpreactself-hostedsell-ticketssell-tickets-onlineticket-sellingticketing-systemtickettailor-alternativetito-alternative
<?php
namespace Tests\Unit\Services\Application\Handlers\PromoCode;
use HiEvents\DomainObjects\Enums\PromoCodeDiscountAppliesToEnum;
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 test_handle_throws_exception_when_promo_code_not_found_for_event(): 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,
discount_applies_to: PromoCodeDiscountAppliesToEnum::EACH_PRODUCT
);
$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 test_handle_verifies_promo_code_belongs_to_event(): 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,
discount_applies_to: PromoCodeDiscountAppliesToEnum::EACH_PRODUCT
);
$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 test_handle_successfully_updates_promo_code_when_ownership_verified(): 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,
discount_applies_to: PromoCodeDiscountAppliesToEnum::EACH_PRODUCT
);
$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);
}
public function test_handle_preserves_stored_discount_applies_to_when_omitted(): void
{
$promoCodeId = 1;
$eventId = 2;
$dto = new UpsertPromoCodeDTO(
code: 'testcode',
event_id: $eventId,
applicable_product_ids: [],
discount_type: PromoCodeDiscountTypeEnum::FIXED,
discount: 10.0,
expiry_date: null,
max_allowed_usages: null,
discount_applies_to: null
);
$existingPromoCode = m::mock(PromoCodeDomainObject::class);
$existingPromoCode->shouldReceive('getId')->andReturn($promoCodeId);
$existingPromoCode->shouldReceive('getDiscountAppliesTo')->andReturn(PromoCodeDiscountAppliesToEnum::ORDER->name);
$event = m::mock(EventDomainObject::class);
$event->shouldReceive('getTimezone')->andReturn('UTC');
$this->promoCodeRepository
->shouldReceive('findFirstWhere')
->twice()
->andReturn($existingPromoCode);
$this->eventProductValidationService
->shouldReceive('validateProductIds')
->once();
$this->eventRepository
->shouldReceive('findById')
->once()
->andReturn($event);
$this->promoCodeRepository
->shouldReceive('updateFromArray')
->once()
->with($promoCodeId, m::on(
static fn (array $attributes) => $attributes['discount_applies_to'] === PromoCodeDiscountAppliesToEnum::ORDER->name
))
->andReturn($existingPromoCode);
$this->assertSame($existingPromoCode, $this->handler->handle($promoCodeId, $dto));
}
protected function tearDown(): void
{
m::close();
parent::tearDown();
}
}