mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-05-13 06:51:29 +00:00
Test some more edge cases in tests
This commit is contained in:
parent
47ab18175f
commit
112e962239
8 changed files with 690 additions and 41 deletions
|
|
@ -24,10 +24,12 @@ namespace App\Tests\Services\Parts;
|
|||
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Entity\PriceInformations\Orderdetail;
|
||||
use App\Entity\PriceInformations\Pricedetail;
|
||||
use App\Services\Formatters\AmountFormatter;
|
||||
use App\Services\Parts\PricedetailHelper;
|
||||
use Brick\Math\BigDecimal;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
final class PricedetailHelperTest extends WebTestCase
|
||||
|
|
@ -87,4 +89,181 @@ final class PricedetailHelperTest extends WebTestCase
|
|||
{
|
||||
$this->assertSame($expected_result, $this->service->getMaxDiscountAmount($part), $message);
|
||||
}
|
||||
|
||||
// --- getMinOrderAmount ---
|
||||
|
||||
public static function minOrderAmountDataProvider(): \Generator
|
||||
{
|
||||
$part = new Part();
|
||||
yield [$part, null, 'No orderdetails'];
|
||||
|
||||
$part = new Part();
|
||||
$part->addOrderdetail(new Orderdetail()); // orderdetail with no pricedetails
|
||||
yield [$part, null, 'Empty orderdetail'];
|
||||
|
||||
$part = new Part();
|
||||
$od = new Orderdetail();
|
||||
$od->addPricedetail((new Pricedetail())->setMinDiscountQuantity(5));
|
||||
$part->addOrderdetail($od);
|
||||
yield [$part, 5.0, 'Single pricedetail'];
|
||||
|
||||
// The service reads $pricedetails[0] assuming the collection is sorted ascending
|
||||
// (which Doctrine does automatically for persistent collections). For in-memory
|
||||
// collections we must insert in ascending order ourselves.
|
||||
$part = new Part();
|
||||
$od = new Orderdetail();
|
||||
$od->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1));
|
||||
$od->addPricedetail((new Pricedetail())->setMinDiscountQuantity(3));
|
||||
$od->addPricedetail((new Pricedetail())->setMinDiscountQuantity(10));
|
||||
$part->addOrderdetail($od);
|
||||
yield [$part, 1.0, 'Multiple pricedetails — picks minimum (first in ascending order)'];
|
||||
|
||||
$part = new Part();
|
||||
$od1 = new Orderdetail();
|
||||
$od1->addPricedetail((new Pricedetail())->setMinDiscountQuantity(5));
|
||||
$od2 = new Orderdetail();
|
||||
$od2->addPricedetail((new Pricedetail())->setMinDiscountQuantity(2));
|
||||
$part->addOrderdetail($od1);
|
||||
$part->addOrderdetail($od2);
|
||||
yield [$part, 2.0, 'Multiple orderdetails — picks global minimum'];
|
||||
}
|
||||
|
||||
#[DataProvider('minOrderAmountDataProvider')]
|
||||
public function testGetMinOrderAmount(Part $part, ?float $expected, string $message): void
|
||||
{
|
||||
$this->assertSame($expected, $this->service->getMinOrderAmount($part), $message);
|
||||
}
|
||||
|
||||
// --- calculateAvgPrice ---
|
||||
|
||||
private static function makePartWithPrice(float $pricePerUnit, float $minQty = 1.0): Part
|
||||
{
|
||||
$part = new Part();
|
||||
$od = new Orderdetail();
|
||||
$pd = (new Pricedetail())
|
||||
->setMinDiscountQuantity($minQty)
|
||||
->setPrice(BigDecimal::of((string) $pricePerUnit));
|
||||
$od->addPricedetail($pd);
|
||||
$part->addOrderdetail($od);
|
||||
return $part;
|
||||
}
|
||||
|
||||
public function testCalculateAvgPriceNoOrderdetailsReturnsNull(): void
|
||||
{
|
||||
$this->assertNull($this->service->calculateAvgPrice(new Part()));
|
||||
}
|
||||
|
||||
public function testCalculateAvgPriceExplicitAmount(): void
|
||||
{
|
||||
$part = self::makePartWithPrice(2.00);
|
||||
$result = $this->service->calculateAvgPrice($part, 1.0);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertTrue(BigDecimal::of('2.00000')->isEqualTo($result));
|
||||
}
|
||||
|
||||
public function testCalculateAvgPriceUsesMinOrderAmountWhenAmountIsNull(): void
|
||||
{
|
||||
// Min order amount is 5; the price applies for qty >= 5
|
||||
$part = self::makePartWithPrice(3.00, 5.0);
|
||||
$result = $this->service->calculateAvgPrice($part, null);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertTrue(BigDecimal::of('3.00000')->isEqualTo($result));
|
||||
}
|
||||
|
||||
public function testCalculateAvgPriceAveragesMultipleSuppliers(): void
|
||||
{
|
||||
$part = new Part();
|
||||
|
||||
$od1 = new Orderdetail();
|
||||
$od1->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1)->setPrice(BigDecimal::of('2.00')));
|
||||
$part->addOrderdetail($od1);
|
||||
|
||||
$od2 = new Orderdetail();
|
||||
$od2->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1)->setPrice(BigDecimal::of('4.00')));
|
||||
$part->addOrderdetail($od2);
|
||||
|
||||
// Average of 2.00 and 4.00 = 3.00
|
||||
$result = $this->service->calculateAvgPrice($part, 1.0);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertTrue(BigDecimal::of('3.00000')->isEqualTo($result));
|
||||
}
|
||||
|
||||
public function testCalculateAvgPriceSkipsSupplierWithNoCoverageForAmount(): void
|
||||
{
|
||||
// Only one supplier covers qty=1, the other requires qty >= 100
|
||||
$part = new Part();
|
||||
$od1 = new Orderdetail();
|
||||
$od1->addPricedetail((new Pricedetail())->setMinDiscountQuantity(1)->setPrice(BigDecimal::of('5.00')));
|
||||
$part->addOrderdetail($od1);
|
||||
|
||||
$od2 = new Orderdetail();
|
||||
$od2->addPricedetail((new Pricedetail())->setMinDiscountQuantity(100)->setPrice(BigDecimal::of('1.00')));
|
||||
$part->addOrderdetail($od2);
|
||||
|
||||
$result = $this->service->calculateAvgPrice($part, 1.0);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertTrue(BigDecimal::of('5.00000')->isEqualTo($result));
|
||||
}
|
||||
|
||||
// --- convertMoneyToCurrency ---
|
||||
|
||||
public function testConvertMoneyToCurrencyIdentityBothNull(): void
|
||||
{
|
||||
// Both currencies null = base currency; same currency, no conversion
|
||||
$value = BigDecimal::of('10.00');
|
||||
$result = $this->service->convertMoneyToCurrency($value, null, null);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertTrue($value->isEqualTo($result));
|
||||
}
|
||||
|
||||
public function testConvertMoneyToCurrencyFromForeignToBase(): void
|
||||
{
|
||||
// EUR → base (null): exchange rate = 1.2 means 1 foreign = 1.2 base
|
||||
$currency = new Currency();
|
||||
$currency->setExchangeRate(BigDecimal::of('1.2'));
|
||||
|
||||
$result = $this->service->convertMoneyToCurrency(BigDecimal::of('10.00'), $currency, null);
|
||||
$this->assertNotNull($result);
|
||||
// 10 * 1.2 = 12
|
||||
$this->assertTrue(BigDecimal::of('12.00000')->isEqualTo($result));
|
||||
}
|
||||
|
||||
public function testConvertMoneyToCurrencyNullExchangeRateReturnsNull(): void
|
||||
{
|
||||
$currency = new Currency();
|
||||
// exchange rate not set → null
|
||||
|
||||
$result = $this->service->convertMoneyToCurrency(BigDecimal::of('10.00'), $currency, null);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
public function testConvertMoneyToCurrencyZeroExchangeRateReturnsNull(): void
|
||||
{
|
||||
$currency = new Currency();
|
||||
$currency->setExchangeRate(BigDecimal::zero());
|
||||
|
||||
$result = $this->service->convertMoneyToCurrency(BigDecimal::of('10.00'), $currency, null);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
public function testConvertMoneyToCurrencyTargetNullExchangeRateReturnsNull(): void
|
||||
{
|
||||
$target = new Currency();
|
||||
// exchange rate not set → getInverseExchangeRate() returns null
|
||||
|
||||
$result = $this->service->convertMoneyToCurrency(BigDecimal::of('10.00'), null, $target);
|
||||
$this->assertNull($result);
|
||||
}
|
||||
|
||||
public function testConvertMoneyToCurrencySameCurrencyInstanceIsIdentity(): void
|
||||
{
|
||||
$currency = new Currency();
|
||||
$currency->setExchangeRate(BigDecimal::of('2.0'));
|
||||
|
||||
$value = BigDecimal::of('5.00');
|
||||
// origin === target → no conversion at all
|
||||
$result = $this->service->convertMoneyToCurrency($value, $currency, $currency);
|
||||
$this->assertNotNull($result);
|
||||
$this->assertTrue($value->isEqualTo($result));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue