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\Domain\Product;
use HiEvents\DomainObjects\Enums\ProductPriceType;
use HiEvents\DomainObjects\Enums\PromoCodeDiscountAppliesToEnum;
use HiEvents\DomainObjects\Enums\PromoCodeDiscountTypeEnum;
use HiEvents\DomainObjects\ProductDomainObject;
use HiEvents\DomainObjects\ProductPriceDomainObject;
use HiEvents\DomainObjects\ProductPriceOccurrenceOverrideDomainObject;
use HiEvents\DomainObjects\PromoCodeDomainObject;
use HiEvents\Repository\Interfaces\ProductPriceOccurrenceOverrideRepositoryInterface;
use HiEvents\Services\Domain\Product\DTO\OrderProductPriceDTO;
use HiEvents\Services\Domain\Product\ProductPriceService;
use Mockery;
use Mockery\MockInterface;
use Tests\TestCase;
class ProductPriceServiceTest extends TestCase
{
private ProductPriceOccurrenceOverrideRepositoryInterface|MockInterface $priceOverrideRepository;
private ProductPriceService $service;
protected function setUp(): void
{
parent::setUp();
$this->priceOverrideRepository = Mockery::mock(ProductPriceOccurrenceOverrideRepositoryInterface::class);
$this->service = new ProductPriceService($this->priceOverrideRepository);
}
public function test_get_price_uses_override_when_present(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 50.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100);
$override = Mockery::mock(ProductPriceOccurrenceOverrideDomainObject::class);
$override->shouldReceive('getPrice')->andReturn('35.00');
$this->priceOverrideRepository
->shouldReceive('findFirstWhere')
->with([
'event_occurrence_id' => 5,
'product_price_id' => 100,
])
->andReturn($override);
$result = $this->service->getPrice($product, $orderDetail, null, 5);
$this->assertEquals(35.00, $result->price);
}
public function test_get_price_falls_back_to_base_when_no_override(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 50.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100);
$this->priceOverrideRepository
->shouldReceive('findFirstWhere')
->with([
'event_occurrence_id' => 5,
'product_price_id' => 100,
])
->andReturn(null);
$result = $this->service->getPrice($product, $orderDetail, null, 5);
$this->assertEquals(50.00, $result->price);
}
public function test_get_price_skips_override_lookup_without_occurrence(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 50.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100);
$this->priceOverrideRepository->shouldNotReceive('findFirstWhere');
$result = $this->service->getPrice($product, $orderDetail, null);
$this->assertEquals(50.00, $result->price);
}
public function test_get_price_applies_promo_code_after_override(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 50.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100);
$override = Mockery::mock(ProductPriceOccurrenceOverrideDomainObject::class);
$override->shouldReceive('getPrice')->andReturn('40.00');
$this->priceOverrideRepository
->shouldReceive('findFirstWhere')
->andReturn($override);
$promoCode = Mockery::mock(PromoCodeDomainObject::class);
$promoCode->shouldReceive('appliesToProduct')->andReturn(true);
$promoCode->shouldReceive('getDiscountType')->andReturn(PromoCodeDiscountTypeEnum::PERCENTAGE->name);
$promoCode->shouldReceive('isFixedDiscount')->andReturn(false);
$promoCode->shouldReceive('isPercentageDiscount')->andReturn(true);
$promoCode->shouldReceive('isOrderLevelDiscount')->andReturn(false);
$promoCode->shouldReceive('getDiscount')->andReturn(10);
$result = $this->service->getPrice($product, $orderDetail, $promoCode, 5);
$this->assertEquals(36.00, $result->price);
$this->assertEquals(40.00, $result->price_before_discount);
}
public function test_get_price_applies_fixed_per_product_discount_to_unit_price(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 50.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100);
$result = $this->service->getPrice(
$product,
$orderDetail,
$this->createFixedPromoCode(PromoCodeDiscountAppliesToEnum::EACH_PRODUCT),
);
$this->assertEquals(40.00, $result->price);
$this->assertEquals(50.00, $result->price_before_discount);
}
public function test_get_price_ignores_order_level_fixed_discount(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 50.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100);
$result = $this->service->getPrice(
$product,
$orderDetail,
$this->createFixedPromoCode(PromoCodeDiscountAppliesToEnum::ORDER),
);
$this->assertEquals(50.00, $result->price);
$this->assertNull($result->price_before_discount);
}
public function test_percentage_discount_stored_as_order_level_still_discounts_unit_price(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 50.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100);
$promoCode = (new PromoCodeDomainObject)
->setDiscountType(PromoCodeDiscountTypeEnum::PERCENTAGE->name)
->setDiscountAppliesTo(PromoCodeDiscountAppliesToEnum::ORDER->name)
->setDiscount(10.00);
$result = $this->service->getPrice($product, $orderDetail, $promoCode);
$this->assertEquals(45.00, $result->price);
$this->assertEquals(50.00, $result->price_before_discount);
}
private function createFixedPromoCode(PromoCodeDiscountAppliesToEnum $appliesTo): PromoCodeDomainObject
{
return (new PromoCodeDomainObject)
->setDiscountType(PromoCodeDiscountTypeEnum::FIXED->name)
->setDiscountAppliesTo($appliesTo->name)
->setDiscount(10.00);
}
public function test_donation_keeps_donor_amount_above_override_minimum(): void
{
$product = $this->createProduct(ProductPriceType::DONATION->name, 10.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100, price: 50.00);
$override = Mockery::mock(ProductPriceOccurrenceOverrideDomainObject::class);
$override->shouldReceive('getPrice')->andReturn('25.00');
$this->priceOverrideRepository
->shouldReceive('findFirstWhere')
->with([
'event_occurrence_id' => 5,
'product_price_id' => 100,
])
->andReturn($override);
$result = $this->service->getPrice($product, $orderDetail, null, 5);
$this->assertEquals(50.00, $result->price);
}
public function test_donation_enforces_override_as_minimum(): void
{
$product = $this->createProduct(ProductPriceType::DONATION->name, 10.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100, price: 10.00);
$override = Mockery::mock(ProductPriceOccurrenceOverrideDomainObject::class);
$override->shouldReceive('getPrice')->andReturn('25.00');
$this->priceOverrideRepository
->shouldReceive('findFirstWhere')
->andReturn($override);
$result = $this->service->getPrice($product, $orderDetail, null, 5);
$this->assertEquals(25.00, $result->price);
}
public function test_donation_without_override_keeps_donor_amount(): void
{
$product = $this->createProduct(ProductPriceType::DONATION->name, 10.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100, price: 50.00);
$this->priceOverrideRepository
->shouldReceive('findFirstWhere')
->andReturn(null);
$result = $this->service->getPrice($product, $orderDetail, null, 5);
$this->assertEquals(50.00, $result->price);
}
public function test_donation_without_override_enforces_product_price_minimum(): void
{
$product = $this->createProduct(ProductPriceType::DONATION->name, 10.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100, price: 5.00);
$this->priceOverrideRepository
->shouldReceive('findFirstWhere')
->andReturn(null);
$result = $this->service->getPrice($product, $orderDetail, null, 5);
$this->assertEquals(10.00, $result->price);
}
public function test_donation_minimum_price_uses_override(): void
{
$product = $this->createProduct(ProductPriceType::DONATION->name, 10.00);
$override = Mockery::mock(ProductPriceOccurrenceOverrideDomainObject::class);
$override->shouldReceive('getPrice')->andReturn('25.00');
$this->priceOverrideRepository
->shouldReceive('findFirstWhere')
->with([
'event_occurrence_id' => 5,
'product_price_id' => 100,
])
->andReturn($override);
$this->assertEquals(25.00, $this->service->getDonationMinimumPrice($product, 100, 5));
}
public function test_donation_minimum_price_falls_back_to_product_price(): void
{
$product = $this->createProduct(ProductPriceType::DONATION->name, 10.00);
$this->priceOverrideRepository
->shouldReceive('findFirstWhere')
->andReturn(null);
$this->assertEquals(10.00, $this->service->getDonationMinimumPrice($product, 100, 5));
}
public function test_donation_minimum_price_without_occurrence_skips_override_lookup(): void
{
$product = $this->createProduct(ProductPriceType::DONATION->name, 10.00);
$this->priceOverrideRepository->shouldNotReceive('findFirstWhere');
$this->assertEquals(10.00, $this->service->getDonationMinimumPrice($product, 100, null));
}
public function test_get_price_returns_free_for_free_product(): void
{
$product = $this->createProduct(ProductPriceType::FREE->name, 0.0);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100);
$this->priceOverrideRepository->shouldReceive('findFirstWhere')->andReturn(null);
$result = $this->service->getPrice($product, $orderDetail, null, 5);
$this->assertEquals(0.00, $result->price);
}
public function test_a_band_adjustment_is_added_to_the_base_price(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 30.00);
$price = $this->service->getPrice(
$product,
new OrderProductPriceDTO(quantity: 1, price_id: 100, band_key: 'b_stalls'),
null,
bandPriceAdjustment: 15.00,
);
$this->assertSame(45.00, $price->price);
}
public function test_a_zero_band_adjustment_leaves_the_price_untouched(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 30.00);
$orderDetail = new OrderProductPriceDTO(quantity: 1, price_id: 100);
$this->assertSame(
$this->service->getPrice($product, $orderDetail, null)->price,
$this->service->getPrice($product, $orderDetail, null, bandPriceAdjustment: 0.0)->price,
);
}
public function test_a_negative_band_adjustment_never_produces_a_price_below_zero(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 10.00);
$price = $this->service->getPrice(
$product,
new OrderProductPriceDTO(quantity: 1, price_id: 100, band_key: 'b_balcony'),
null,
bandPriceAdjustment: -25.00,
);
$this->assertSame(0.0, $price->price);
}
public function test_a_box_office_price_override_wins_outright_over_the_band_adjustment(): void
{
$product = $this->createProduct(ProductPriceType::PAID->name, 30.00);
$product->shouldReceive('isDonationType')->andReturnFalse();
$price = $this->service->getPrice(
$product,
new OrderProductPriceDTO(quantity: 1, price_id: 100, price: 20.00, band_key: 'b_stalls'),
null,
allowClientPrice: true,
bandPriceAdjustment: 15.00,
);
$this->assertSame(20.00, $price->price);
}
private function createProduct(string $type, float $price): ProductDomainObject
{
$productPrice = Mockery::mock(ProductPriceDomainObject::class);
$productPrice->shouldReceive('getId')->andReturn(100);
$productPrice->shouldReceive('getPrice')->andReturn($price);
$product = Mockery::mock(ProductDomainObject::class);
$product->shouldReceive('getType')->andReturn($type);
$product->shouldReceive('getPrice')->andReturn($price);
$product->shouldReceive('getProductPrices')->andReturn(collect([$productPrice]));
$product->shouldReceive('getPriceById')->with(100)->andReturn($productPrice);
return $product;
}
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
}