|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Services\Domain\Order; |
| 4 | + |
| 5 | +use HiEvents\DomainObjects\AccountConfigurationDomainObject; |
| 6 | +use HiEvents\DomainObjects\OrderDomainObject; |
| 7 | +use HiEvents\DomainObjects\OrderItemDomainObject; |
| 8 | +use HiEvents\Services\Domain\Order\OrderApplicationFeeCalculationService; |
| 9 | +use HiEvents\Services\Infrastructure\CurrencyConversion\CurrencyConversionClientInterface; |
| 10 | +use HiEvents\Values\MoneyValue; |
| 11 | +use Illuminate\Config\Repository; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +class OrderApplicationFeeCalculationServiceTest extends TestCase |
| 15 | +{ |
| 16 | + private Repository $config; |
| 17 | + private CurrencyConversionClientInterface $currencyConversionClient; |
| 18 | + private OrderApplicationFeeCalculationService $service; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + $this->config = $this->createMock(Repository::class); |
| 23 | + $this->currencyConversionClient = $this->createMock(CurrencyConversionClientInterface::class); |
| 24 | + $this->service = new OrderApplicationFeeCalculationService($this->config, $this->currencyConversionClient); |
| 25 | + } |
| 26 | + |
| 27 | + private function createOrderWithItems(array $items, string $currency = 'USD'): OrderDomainObject |
| 28 | + { |
| 29 | + $order = (new OrderDomainObject()) |
| 30 | + ->setCurrency($currency) |
| 31 | + ->setOrderItems(collect($items)); |
| 32 | + |
| 33 | + // Calculate gross manually for test accuracy |
| 34 | + $total = collect($items)->reduce(fn($carry, $item) => $carry + ($item->getPrice() * $item->getQuantity()), 0); |
| 35 | + $order->setTotalGross($total); |
| 36 | + |
| 37 | + return $order; |
| 38 | + } |
| 39 | + |
| 40 | + private function createItem(float $price, int $quantity): OrderItemDomainObject |
| 41 | + { |
| 42 | + $item = $this->createMock(OrderItemDomainObject::class); |
| 43 | + $item->method('getPrice')->willReturn($price); |
| 44 | + $item->method('getQuantity')->willReturn($quantity); |
| 45 | + return $item; |
| 46 | + } |
| 47 | + |
| 48 | + private function createAccountConfig(float $fixedFee = 0, float $percentageFee = 0): AccountConfigurationDomainObject |
| 49 | + { |
| 50 | + $config = $this->getMockBuilder(AccountConfigurationDomainObject::class) |
| 51 | + ->disableOriginalConstructor() |
| 52 | + ->onlyMethods(['getFixedApplicationFee', 'getPercentageApplicationFee']) |
| 53 | + ->getMock(); |
| 54 | + |
| 55 | + $config->method('getFixedApplicationFee')->willReturn($fixedFee); |
| 56 | + $config->method('getPercentageApplicationFee')->willReturn($percentageFee); |
| 57 | + |
| 58 | + return $config; |
| 59 | + } |
| 60 | + |
| 61 | + public function testNoFeeWhenSaasModeDisabled(): void |
| 62 | + { |
| 63 | + $this->config->method('get')->with('app.saas_mode_enabled')->willReturn(false); |
| 64 | + |
| 65 | + $order = $this->createOrderWithItems([$this->createItem(10, 2)]); |
| 66 | + $account = $this->createAccountConfig(1, 1); |
| 67 | + |
| 68 | + $fee = $this->service->calculateApplicationFee($account, $order); |
| 69 | + |
| 70 | + $this->assertEquals(0.0, $fee->toFloat()); |
| 71 | + } |
| 72 | + |
| 73 | + public function testNoFeeForFreeOrder(): void |
| 74 | + { |
| 75 | + $this->config->method('get')->willReturn(true); |
| 76 | + |
| 77 | + $order = $this->createOrderWithItems([$this->createItem(0, 3)]); |
| 78 | + $account = $this->createAccountConfig(1, 1); |
| 79 | + |
| 80 | + $fee = $this->service->calculateApplicationFee($account, $order); |
| 81 | + |
| 82 | + $this->assertEquals(0.0, $fee->toFloat()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testFixedAndPercentageFeeSameCurrency(): void |
| 86 | + { |
| 87 | + $this->config->method('get')->willReturn(true); |
| 88 | + |
| 89 | + $order = $this->createOrderWithItems([ |
| 90 | + $this->createItem(10, 1), // chargeable |
| 91 | + $this->createItem(0, 5), // free |
| 92 | + $this->createItem(20, 2), // chargeable |
| 93 | + ]); |
| 94 | + |
| 95 | + $account = $this->createAccountConfig(2.00, 1); // 2 USD fixed, 1% percentage |
| 96 | + |
| 97 | + // 3 chargeable items × $2 fixed = $6 |
| 98 | + // $10 + $40 = $50 gross → 1% of $50 = $0.50 |
| 99 | + // Total = $6.50 |
| 100 | + $fee = $this->service->calculateApplicationFee($account, $order); |
| 101 | + |
| 102 | + $this->assertEquals(6.50, $fee->toFloat()); |
| 103 | + } |
| 104 | + |
| 105 | + public function testCurrencyConversionForFixedFee(): void |
| 106 | + { |
| 107 | + $this->config->method('get')->willReturn(true); |
| 108 | + |
| 109 | + $order = $this->createOrderWithItems([ |
| 110 | + $this->createItem(15, 1), // chargeable |
| 111 | + $this->createItem(0, 3), // free |
| 112 | + ], 'EUR'); |
| 113 | + |
| 114 | + $account = $this->createAccountConfig(1.50, 20); |
| 115 | + |
| 116 | + $this->currencyConversionClient->method('convert') |
| 117 | + ->willReturn(MoneyValue::fromFloat(2.00, 'EUR')); // fixed fee per chargeable = €2.00 |
| 118 | + |
| 119 | + // 1 chargeable × €2 fixed = €2 |
| 120 | + // 15 × 20% = €3 percentage |
| 121 | + // Total = €5 |
| 122 | + $fee = $this->service->calculateApplicationFee($account, $order); |
| 123 | + |
| 124 | + $this->assertEquals(5.00, $fee->toFloat()); |
| 125 | + } |
| 126 | +} |
0 commit comments