diff --git a/src/Helpers/Projects/ProjectBuildRequest.php b/src/Helpers/Projects/ProjectBuildRequest.php index 3ae4db10..093581f4 100644 --- a/src/Helpers/Projects/ProjectBuildRequest.php +++ b/src/Helpers/Projects/ProjectBuildRequest.php @@ -50,6 +50,10 @@ final class ProjectBuildRequest */ public function __construct(Project $project, int $number_of_builds) { + if ($number_of_builds < 1) { + throw new \InvalidArgumentException('Number of builds must be at least 1!'); + } + $this->project = $project; $this->number_of_builds = $number_of_builds; diff --git a/tests/Helpers/Projects/ProjectBuildRequestTest.php b/tests/Helpers/Projects/ProjectBuildRequestTest.php new file mode 100644 index 00000000..0af0aa72 --- /dev/null +++ b/tests/Helpers/Projects/ProjectBuildRequestTest.php @@ -0,0 +1,177 @@ +. + */ + +namespace App\Tests\Helpers\Projects; + +use App\Entity\Parts\MeasurementUnit; +use App\Entity\Parts\Part; +use App\Entity\Parts\PartLot; +use App\Entity\ProjectSystem\Project; +use App\Entity\ProjectSystem\ProjectBOMEntry; +use App\Helpers\Projects\ProjectBuildRequest; +use PHPUnit\Framework\TestCase; + +class ProjectBuildRequestTest extends TestCase +{ + + /** @var MeasurementUnit $float_unit */ + private MeasurementUnit $float_unit; + + /** @var Project */ + private $project1; + /** @var ProjectBOMEntry */ + private $bom_entry1a; + /** @var ProjectBOMEntry */ + private $bom_entry1b; + /** @var ProjectBOMEntry */ + private $bom_entry1c; + + /** @var PartLot $lot1a */ + private $lot1a; + /** @var PartLot $lot1b */ + private $lot1b; + private $lot2; + + /** @var Part */ + private $part1; + /** @var Part */ + private $part2; + + + public function setUp(): void + { + $this->float_unit = new MeasurementUnit(); + $this->float_unit->setName('float'); + $this->float_unit->setUnit('f'); + $this->float_unit->setIsInteger(false); + $this->float_unit->setUseSIPrefix(true); + + //Setup some example parts and part lots + $this->part1 = new Part(); + $this->part1->setName('Part 1'); + $this->lot1a = new class extends PartLot { + public function getID(): ?int + { + return 1; + } + }; + $this->part1->addPartLot($this->lot1a); + $this->lot1a->setAmount(10); + $this->lot1a->setDescription('Lot 1a'); + + $this->lot1b = new class extends PartLot { + public function getID(): ?int + { + return 2; + } + }; + $this->part1->addPartLot($this->lot1b); + $this->lot1b->setAmount(20); + $this->lot1b->setDescription('Lot 1b'); + + $this->part2 = new Part(); + + $this->part2->setName('Part 2'); + $this->part2->setPartUnit($this->float_unit); + $this->lot2 = new PartLot(); + $this->part2->addPartLot($this->lot2); + $this->lot2->setAmount(2.5); + $this->lot2->setDescription('Lot 2'); + + $this->bom_entry1a = new ProjectBOMEntry(); + $this->bom_entry1a->setPart($this->part1); + $this->bom_entry1a->setQuantity(2); + + $this->bom_entry1b = new ProjectBOMEntry(); + $this->bom_entry1b->setPart($this->part2); + $this->bom_entry1b->setQuantity(1.5); + + $this->bom_entry1c = new ProjectBOMEntry(); + $this->bom_entry1c->setName('Non-part BOM entry'); + $this->bom_entry1c->setQuantity(4); + + + $this->project1 = new Project(); + $this->project1->setName('Project 1'); + $this->project1->addBomEntry($this->bom_entry1a); + $this->project1->addBomEntry($this->bom_entry1b); + $this->project1->addBomEntry($this->bom_entry1c); + } + + public function testInitialization() + { + //The values should be already prefilled correctly + $request = new ProjectBuildRequest($this->project1, 10); + //We need totally 20: Take 10 from the first (maximum 10) and 10 from the second (maximum 20) + $this->assertEquals(10, $request->getLotWithdrawAmount($this->lot1a)); + $this->assertEquals(10, $request->getLotWithdrawAmount($this->lot1b)); + + //If the needed amount is higher than the maximum, we should get the maximum + $this->assertEquals(2.5, $request->getLotWithdrawAmount($this->lot2)); + } + + public function testGetNumberOfBuilds() + { + $build_request = new ProjectBuildRequest($this->project1, 5); + $this->assertEquals(5, $build_request->getNumberOfBuilds()); + } + + public function testGetProject() + { + $build_request = new ProjectBuildRequest($this->project1, 5); + $this->assertEquals($this->project1, $build_request->getProject()); + } + + public function testGetNeededAmountForBOMEntry() + { + $build_request = new ProjectBuildRequest($this->project1, 5); + $this->assertEquals(10, $build_request->getNeededAmountForBOMEntry($this->bom_entry1a)); + $this->assertEquals(7.5, $build_request->getNeededAmountForBOMEntry($this->bom_entry1b)); + $this->assertEquals(20, $build_request->getNeededAmountForBOMEntry($this->bom_entry1c)); + } + + public function testGetSetLotWithdrawAmount() + { + $build_request = new ProjectBuildRequest($this->project1, 5); + + //We can set the amount for a lot either via the lot object or via the ID + $build_request->setLotWithdrawAmount($this->lot1a, 2); + $build_request->setLotWithdrawAmount($this->lot1b->getID(), 3); + + //And it should be possible to get the amount via the lot object or via the ID + $this->assertEquals(2, $build_request->getLotWithdrawAmount($this->lot1a->getID())); + $this->assertEquals(3, $build_request->getLotWithdrawAmount($this->lot1b)); + } + + public function testGetWithdrawAmountSum() + { + //The sum of all withdraw amounts for an BOM entry (over all lots of the associated part) should be correct + $build_request = new ProjectBuildRequest($this->project1, 5); + + $build_request->setLotWithdrawAmount($this->lot1a, 2); + $build_request->setLotWithdrawAmount($this->lot1b, 3); + + $this->assertEquals(5, $build_request->getWithdrawAmountSum($this->bom_entry1a)); + $build_request->setLotWithdrawAmount($this->lot2, 1.5); + $this->assertEquals(1.5, $build_request->getWithdrawAmountSum($this->bom_entry1b)); + } + + +}