Passed
Push — master ( 3340d7...94d7f4 )
by Jan
07:25
created

ProjectBuildHelperTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetMaximumBuildableCount() 0 37 1
A setUp() 0 5 1
A testGetMaximumBuildableCountForBOMEntryNonPartBomEntry() 0 9 1
A testGetMaximumBuildableCountForBOMEntry() 0 22 1
1
<?php
2
/*
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 *  Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 *  This program is free software: you can redistribute it and/or modify
8
 *  it under the terms of the GNU Affero General Public License as published
9
 *  by the Free Software Foundation, either version 3 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU Affero General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU Affero General Public License
18
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Tests\Services\ProjectSystem;
22
23
use App\Entity\Parts\Part;
24
use App\Entity\Parts\PartLot;
25
use App\Entity\ProjectSystem\Project;
26
use App\Entity\ProjectSystem\ProjectBOMEntry;
27
use App\Services\ProjectSystem\ProjectBuildHelper;
28
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
29
30
class ProjectBuildHelperTest extends WebTestCase
31
{
32
    /** @var ProjectBuildHelper */
33
    protected $service;
34
35
    protected function setUp(): void
36
    {
37
        parent::setUp();
38
        self::bootKernel();
39
        $this->service = self::getContainer()->get(ProjectBuildHelper::class);
40
    }
41
42
    public function testGetMaximumBuildableCountForBOMEntryNonPartBomEntry()
43
    {
44
        $bom_entry = new ProjectBOMEntry();
45
        $bom_entry->setPart(null);
46
        $bom_entry->setQuantity(10);
47
        $bom_entry->setName('Test');
48
49
        $this->expectException(\InvalidArgumentException::class);
50
        $this->service->getMaximumBuildableCountForBOMEntry($bom_entry);
51
    }
52
53
    public function testGetMaximumBuildableCountForBOMEntry()
54
    {
55
        $project_bom_entry = new ProjectBOMEntry();
56
        $project_bom_entry->setQuantity(10);
57
58
        $part = new Part();
59
        $lot1 = new PartLot();
60
        $lot1->setAmount(120);
61
        $lot2 = new PartLot();
62
        $lot2->setAmount(5);
63
        $part->addPartLot($lot1);
64
        $part->addPartLot($lot2);
65
66
        $project_bom_entry->setPart($part);
67
68
        //We have 125 parts in stock, so we can build 12 times the project (125 / 10 = 12.5)
69
        $this->assertEquals(12, $this->service->getMaximumBuildableCountForBOMEntry($project_bom_entry));
70
71
72
        $lot1->setAmount(0);
73
        //We have 5 parts in stock, so we can build 0 times the project (5 / 10 = 0.5)
74
        $this->assertEquals(0, $this->service->getMaximumBuildableCountForBOMEntry($project_bom_entry));
75
    }
76
77
    public function testGetMaximumBuildableCount()
78
    {
79
        $project = new Project();
80
81
        $project_bom_entry1 = new ProjectBOMEntry();
82
        $project_bom_entry1->setQuantity(10);
83
        $part = new Part();
84
        $lot1 = new PartLot();
85
        $lot1->setAmount(120);
86
        $lot2 = new PartLot();
87
        $lot2->setAmount(5);
88
        $part->addPartLot($lot1);
89
        $part->addPartLot($lot2);
90
        $project_bom_entry1->setPart($part);
91
        $project->addBomEntry($project_bom_entry1);
92
93
        $project_bom_entry2 = new ProjectBOMEntry();
94
        $project_bom_entry2->setQuantity(5);
95
        $part2 = new Part();
96
        $lot3 = new PartLot();
97
        $lot3->setAmount(10);
98
        $part2->addPartLot($lot3);
99
        $project_bom_entry2->setPart($part2);
100
        $project->addBomEntry($project_bom_entry2);
101
102
        $project->addBomEntry((new ProjectBOMEntry())->setName('Non part entry')->setQuantity(1));
103
104
        //Restricted by the few parts in stock of part2
105
        $this->assertEquals(2, $this->service->getMaximumBuildableCount($project));
106
107
        $lot3->setAmount(1000);
108
        //Now the build count is restricted by the few parts in stock of part1
109
        $this->assertEquals(12, $this->service->getMaximumBuildableCount($project));
110
111
        $lot3->setAmount(0);
112
        //Now the build count must be 0, as we have no parts in stock
113
        $this->assertEquals(0, $this->service->getMaximumBuildableCount($project));
114
115
    }
116
}
117