mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-05-12 22:41:38 +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
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Tests\Services\Parts;
|
||||
|
||||
use App\Entity\Parts\MeasurementUnit;
|
||||
use App\Entity\Parts\Part;
|
||||
use App\Entity\Parts\PartLot;
|
||||
use App\Entity\Parts\StorageLocation;
|
||||
|
|
@ -167,6 +168,223 @@ final class PartLotWithdrawAddHelperTest extends WebTestCase
|
|||
$this->service->stocktake($this->partLot2, 0, "Test");
|
||||
$this->assertEqualsWithDelta(0.0, $this->partLot2->getAmount(), PHP_FLOAT_EPSILON);
|
||||
$this->assertFalse($this->partLot2->isInstockUnknown()); //Instock unknown should be cleared
|
||||
}
|
||||
|
||||
// --- withdraw() error paths ---
|
||||
|
||||
public function testWithdrawZeroAmountThrows(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->service->withdraw($this->partLot1, 0, "Test");
|
||||
}
|
||||
|
||||
public function testWithdrawNegativeAmountThrows(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->service->withdraw($this->partLot1, -5, "Test");
|
||||
}
|
||||
|
||||
public function testWithdrawMoreThanStockThrows(): void
|
||||
{
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->service->withdraw($this->partLot1, 999, "Test");
|
||||
}
|
||||
|
||||
public function testWithdrawFromUnknownInstockLotThrows(): void
|
||||
{
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->service->withdraw($this->lotWithUnknownInstock, 1, "Test");
|
||||
}
|
||||
|
||||
// --- add() error paths ---
|
||||
|
||||
public function testAddZeroAmountThrows(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->service->add($this->partLot1, 0, "Test");
|
||||
}
|
||||
|
||||
public function testAddNegativeAmountThrows(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->service->add($this->partLot1, -3, "Test");
|
||||
}
|
||||
|
||||
public function testAddToFullLotThrows(): void
|
||||
{
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->service->add($this->fullLot, 1, "Test");
|
||||
}
|
||||
|
||||
public function testAddToUnknownInstockLotThrows(): void
|
||||
{
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->service->add($this->lotWithUnknownInstock, 1, "Test");
|
||||
}
|
||||
|
||||
// --- move() error paths ---
|
||||
|
||||
public function testMoveZeroAmountThrows(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->service->move($this->partLot1, $this->partLot2, 0, "Test");
|
||||
}
|
||||
|
||||
public function testMoveBetweenDifferentPartsThrows(): void
|
||||
{
|
||||
$otherPart = new Part();
|
||||
$otherLot = new TestPartLot();
|
||||
$otherLot->setPart($otherPart);
|
||||
$otherLot->setAmount(5);
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->service->move($this->partLot1, $otherLot, 5, "Test");
|
||||
}
|
||||
|
||||
public function testMoveMoreThanOriginStockThrows(): void
|
||||
{
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->service->move($this->partLot1, $this->partLot2, 999, "Test");
|
||||
}
|
||||
|
||||
public function testMoveFromUnwithdrawableLotThrows(): void
|
||||
{
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->service->move($this->lotWithUnknownInstock, $this->partLot2, 1, "Test");
|
||||
}
|
||||
|
||||
public function testMoveToUnavailableLotThrows(): void
|
||||
{
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->service->move($this->partLot1, $this->fullLot, 1, "Test");
|
||||
}
|
||||
|
||||
// --- stocktake() error paths ---
|
||||
|
||||
public function testStocktakeNegativeAmountThrows(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->service->stocktake($this->partLot1, -1, "Test");
|
||||
}
|
||||
|
||||
// --- integer-rounding (useFloatAmount() = false, no unit set) ---
|
||||
|
||||
public function testWithdrawRoundsAmountForIntegerPart(): void
|
||||
{
|
||||
// No unit → useFloatAmount() = false → fractional amounts are rounded
|
||||
$this->assertFalse($this->part->useFloatAmount());
|
||||
|
||||
$this->service->withdraw($this->partLot1, 1.7, "Test"); // rounds to 2
|
||||
$this->assertEqualsWithDelta(8.0, $this->partLot1->getAmount(), PHP_FLOAT_EPSILON);
|
||||
}
|
||||
|
||||
public function testAddRoundsAmountForIntegerPart(): void
|
||||
{
|
||||
$this->assertFalse($this->part->useFloatAmount());
|
||||
|
||||
$this->service->add($this->partLot3, 1.7, "Test"); // rounds to 2
|
||||
$this->assertEqualsWithDelta(2.0, $this->partLot3->getAmount(), PHP_FLOAT_EPSILON);
|
||||
}
|
||||
|
||||
public function testStocktakeRoundsAmountForIntegerPart(): void
|
||||
{
|
||||
$this->assertFalse($this->part->useFloatAmount());
|
||||
|
||||
$this->service->stocktake($this->partLot1, 7.6, "Test"); // rounds to 8
|
||||
$this->assertEqualsWithDelta(8.0, $this->partLot1->getAmount(), PHP_FLOAT_EPSILON);
|
||||
}
|
||||
|
||||
// --- float amounts are preserved when the unit allows floats ---
|
||||
|
||||
public function testAddPreservesFloatAmountForFloatUnit(): void
|
||||
{
|
||||
$unit = new MeasurementUnit();
|
||||
$unit->setIsInteger(false);
|
||||
|
||||
$floatPart = new Part();
|
||||
$floatPart->setPartUnit($unit);
|
||||
$this->assertTrue($floatPart->useFloatAmount());
|
||||
|
||||
$lot = new TestPartLot();
|
||||
$lot->setPart($floatPart);
|
||||
$lot->setAmount(1.0);
|
||||
|
||||
$this->service->add($lot, 1.3, "Test");
|
||||
$this->assertEqualsWithDelta(2.3, $lot->getAmount(), PHP_FLOAT_EPSILON);
|
||||
}
|
||||
|
||||
public function testWithdrawPreservesFloatAmountForFloatUnit(): void
|
||||
{
|
||||
$unit = new MeasurementUnit();
|
||||
$unit->setIsInteger(false);
|
||||
|
||||
$floatPart = new Part();
|
||||
$floatPart->setPartUnit($unit);
|
||||
|
||||
$lot = new TestPartLot();
|
||||
$lot->setPart($floatPart);
|
||||
$lot->setAmount(5.0);
|
||||
|
||||
$this->service->withdraw($lot, 1.3, "Test");
|
||||
$this->assertEqualsWithDelta(3.7, $lot->getAmount(), PHP_FLOAT_EPSILON);
|
||||
}
|
||||
|
||||
// --- delete_lot_if_empty ---
|
||||
|
||||
/**
|
||||
* Creates a PartLot that looks like a managed, persisted entity to Doctrine:
|
||||
* - has a non-null ID (required by AbstractLogEntry when creating stock-change log entries)
|
||||
* - is registered in the UnitOfWork as managed (required so EntityManager::remove() accepts it)
|
||||
*/
|
||||
private function makeManagedLot(float $amount, int $fakeId = 42): PartLot
|
||||
{
|
||||
$lot = new PartLot();
|
||||
$lot->setPart($this->part);
|
||||
$lot->setAmount($amount);
|
||||
|
||||
$ref = new \ReflectionProperty($lot, 'id');
|
||||
$ref->setValue($lot, $fakeId);
|
||||
|
||||
$em = self::getContainer()->get('doctrine.orm.entity_manager');
|
||||
$em->getUnitOfWork()->registerManaged($lot, ['id' => $fakeId], []);
|
||||
|
||||
return $lot;
|
||||
}
|
||||
|
||||
public function testWithdrawDeletesLotWhenEmptyAndFlagSet(): void
|
||||
{
|
||||
$lot = $this->makeManagedLot(10);
|
||||
|
||||
$this->service->withdraw($lot, 10, "Test", null, true);
|
||||
$this->assertEqualsWithDelta(0.0, $lot->getAmount(), PHP_FLOAT_EPSILON);
|
||||
|
||||
$em = self::getContainer()->get('doctrine.orm.entity_manager');
|
||||
$scheduled = $em->getUnitOfWork()->getScheduledEntityDeletions();
|
||||
$this->assertContains($lot, $scheduled);
|
||||
}
|
||||
|
||||
public function testWithdrawDoesNotDeleteLotWhenNotEmptyAndFlagSet(): void
|
||||
{
|
||||
$lot = $this->makeManagedLot(10);
|
||||
|
||||
$this->service->withdraw($lot, 5, "Test", null, true);
|
||||
$this->assertEqualsWithDelta(5.0, $lot->getAmount(), PHP_FLOAT_EPSILON);
|
||||
|
||||
$em = self::getContainer()->get('doctrine.orm.entity_manager');
|
||||
$scheduled = $em->getUnitOfWork()->getScheduledEntityDeletions();
|
||||
$this->assertNotContains($lot, $scheduled);
|
||||
}
|
||||
|
||||
public function testMoveDeletesOriginLotWhenEmptyAndFlagSet(): void
|
||||
{
|
||||
$origin = $this->makeManagedLot(10, 43);
|
||||
$target = $this->makeManagedLot(0, 44);
|
||||
|
||||
$this->service->move($origin, $target, 10, "Test", null, true);
|
||||
$this->assertEqualsWithDelta(0.0, $origin->getAmount(), PHP_FLOAT_EPSILON);
|
||||
|
||||
$em = self::getContainer()->get('doctrine.orm.entity_manager');
|
||||
$scheduled = $em->getUnitOfWork()->getScheduledEntityDeletions();
|
||||
$this->assertContains($origin, $scheduled);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,20 +43,52 @@ final class PartsTableActionHandlerTest extends WebTestCase
|
|||
$part = $this->createMock(Part::class);
|
||||
$part->method('getId')->willReturn(1);
|
||||
$part->method('getName')->willReturn('Test Part');
|
||||
|
||||
|
||||
$selected_parts = [$part];
|
||||
|
||||
// Test each export format, focusing on our new xlsx format
|
||||
$formats = ['json', 'csv', 'xml', 'yaml', 'xlsx'];
|
||||
|
||||
|
||||
foreach ($formats as $format) {
|
||||
$action = "export_{$format}";
|
||||
$result = $this->service->handleAction($action, $selected_parts, 1, '/test');
|
||||
|
||||
|
||||
$this->assertInstanceOf(RedirectResponse::class, $result);
|
||||
$this->assertStringContainsString('parts/export', $result->getTargetUrl());
|
||||
$this->assertStringContainsString("format={$format}", $result->getTargetUrl());
|
||||
}
|
||||
}
|
||||
|
||||
public function testExportUrlContainsPartIds(): void
|
||||
{
|
||||
$part1 = $this->createMock(Part::class);
|
||||
$part1->method('getId')->willReturn(42);
|
||||
|
||||
$part2 = $this->createMock(Part::class);
|
||||
$part2->method('getId')->willReturn(99);
|
||||
|
||||
$result = $this->service->handleAction('export_csv', [$part1, $part2], 1, '/test');
|
||||
|
||||
$this->assertInstanceOf(RedirectResponse::class, $result);
|
||||
// Commas in query-string values are not percent-encoded by Symfony's UrlGenerator
|
||||
$this->assertStringContainsString('ids=42,99', $result->getTargetUrl());
|
||||
}
|
||||
|
||||
public function testExportWithNoPartsProducesEmptyIds(): void
|
||||
{
|
||||
$result = $this->service->handleAction('export_json', [], 1, '/test');
|
||||
|
||||
$this->assertInstanceOf(RedirectResponse::class, $result);
|
||||
$this->assertStringContainsString('parts/export', $result->getTargetUrl());
|
||||
// ids parameter present but empty
|
||||
$this->assertStringContainsString('ids=', $result->getTargetUrl());
|
||||
}
|
||||
|
||||
public function testUnknownActionWithEmptyPartsReturnsNull(): void
|
||||
{
|
||||
// The unknown-action switch only runs inside the foreach loop, so an
|
||||
// empty parts list means the loop body never executes and no exception is thrown.
|
||||
$result = $this->service->handleAction('unknown_action_xyz', [], null, '/test');
|
||||
$this->assertNull($result);
|
||||
}
|
||||
}
|
||||
|
|
@ -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