Issues (257)

tests/Helpers/Projects/ProjectBuildRequestTest.php (1 issue)

Labels
Severity
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\Helpers\Projects;
22
23
use App\Entity\Parts\MeasurementUnit;
24
use App\Entity\Parts\Part;
25
use App\Entity\Parts\PartLot;
26
use App\Entity\ProjectSystem\Project;
27
use App\Entity\ProjectSystem\ProjectBOMEntry;
28
use App\Helpers\Projects\ProjectBuildRequest;
29
use PHPUnit\Framework\TestCase;
0 ignored issues
show
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
31
class ProjectBuildRequestTest extends TestCase
32
{
33
34
    /** @var MeasurementUnit $float_unit */
35
    private MeasurementUnit $float_unit;
36
37
    /** @var Project */
38
    private $project1;
39
    /** @var ProjectBOMEntry */
40
    private $bom_entry1a;
41
    /** @var ProjectBOMEntry */
42
    private $bom_entry1b;
43
    /** @var ProjectBOMEntry */
44
    private $bom_entry1c;
45
46
    /** @var PartLot $lot1a */
47
    private $lot1a;
48
    /** @var PartLot $lot1b */
49
    private $lot1b;
50
    private $lot2;
51
52
    /** @var Part */
53
    private $part1;
54
    /** @var Part */
55
    private $part2;
56
57
58
    public function setUp(): void
59
    {
60
        $this->float_unit = new MeasurementUnit();
61
        $this->float_unit->setName('float');
62
        $this->float_unit->setUnit('f');
63
        $this->float_unit->setIsInteger(false);
64
        $this->float_unit->setUseSIPrefix(true);
65
66
        //Setup some example parts and part lots
67
        $this->part1 = new Part();
68
        $this->part1->setName('Part 1');
69
        $this->lot1a = new class extends PartLot {
70
            public function getID(): ?int
71
            {
72
                return 1;
73
            }
74
        };
75
        $this->part1->addPartLot($this->lot1a);
76
        $this->lot1a->setAmount(10);
77
        $this->lot1a->setDescription('Lot 1a');
78
79
        $this->lot1b = new class extends PartLot {
80
            public function getID(): ?int
81
            {
82
                return 2;
83
            }
84
        };
85
        $this->part1->addPartLot($this->lot1b);
86
        $this->lot1b->setAmount(20);
87
        $this->lot1b->setDescription('Lot 1b');
88
89
        $this->part2 = new Part();
90
91
        $this->part2->setName('Part 2');
92
        $this->part2->setPartUnit($this->float_unit);
93
        $this->lot2 = new PartLot();
94
        $this->part2->addPartLot($this->lot2);
95
        $this->lot2->setAmount(2.5);
96
        $this->lot2->setDescription('Lot 2');
97
98
        $this->bom_entry1a = new ProjectBOMEntry();
99
        $this->bom_entry1a->setPart($this->part1);
100
        $this->bom_entry1a->setQuantity(2);
101
102
        $this->bom_entry1b = new ProjectBOMEntry();
103
        $this->bom_entry1b->setPart($this->part2);
104
        $this->bom_entry1b->setQuantity(1.5);
105
106
        $this->bom_entry1c = new ProjectBOMEntry();
107
        $this->bom_entry1c->setName('Non-part BOM entry');
108
        $this->bom_entry1c->setQuantity(4);
109
110
111
        $this->project1 = new Project();
112
        $this->project1->setName('Project 1');
113
        $this->project1->addBomEntry($this->bom_entry1a);
114
        $this->project1->addBomEntry($this->bom_entry1b);
115
        $this->project1->addBomEntry($this->bom_entry1c);
116
    }
117
118
    public function testInitialization()
119
    {
120
        //The values should be already prefilled correctly
121
        $request = new ProjectBuildRequest($this->project1, 10);
122
        //We need totally 20: Take 10 from the first (maximum 10) and 10 from the second (maximum 20)
123
        $this->assertEquals(10, $request->getLotWithdrawAmount($this->lot1a));
124
        $this->assertEquals(10, $request->getLotWithdrawAmount($this->lot1b));
125
126
        //If the needed amount is higher than the maximum, we should get the maximum
127
        $this->assertEquals(2.5, $request->getLotWithdrawAmount($this->lot2));
128
    }
129
130
    public function testGetNumberOfBuilds()
131
    {
132
        $build_request = new ProjectBuildRequest($this->project1, 5);
133
        $this->assertEquals(5, $build_request->getNumberOfBuilds());
134
    }
135
136
    public function testGetProject()
137
    {
138
        $build_request = new ProjectBuildRequest($this->project1, 5);
139
        $this->assertEquals($this->project1, $build_request->getProject());
140
    }
141
142
    public function testGetNeededAmountForBOMEntry()
143
    {
144
        $build_request = new ProjectBuildRequest($this->project1, 5);
145
        $this->assertEquals(10, $build_request->getNeededAmountForBOMEntry($this->bom_entry1a));
146
        $this->assertEquals(7.5, $build_request->getNeededAmountForBOMEntry($this->bom_entry1b));
147
        $this->assertEquals(20, $build_request->getNeededAmountForBOMEntry($this->bom_entry1c));
148
    }
149
150
    public function testGetSetLotWithdrawAmount()
151
    {
152
        $build_request = new ProjectBuildRequest($this->project1, 5);
153
154
        //We can set the amount for a lot either via the lot object or via the ID
155
        $build_request->setLotWithdrawAmount($this->lot1a, 2);
156
        $build_request->setLotWithdrawAmount($this->lot1b->getID(), 3);
157
158
        //And it should be possible to get the amount via the lot object or via the ID
159
        $this->assertEquals(2, $build_request->getLotWithdrawAmount($this->lot1a->getID()));
160
        $this->assertEquals(3, $build_request->getLotWithdrawAmount($this->lot1b));
161
    }
162
163
    public function testGetWithdrawAmountSum()
164
    {
165
        //The sum of all withdraw amounts for an BOM entry (over all lots of the associated part) should be correct
166
        $build_request = new ProjectBuildRequest($this->project1, 5);
167
168
        $build_request->setLotWithdrawAmount($this->lot1a, 2);
169
        $build_request->setLotWithdrawAmount($this->lot1b, 3);
170
171
        $this->assertEquals(5, $build_request->getWithdrawAmountSum($this->bom_entry1a));
172
        $build_request->setLotWithdrawAmount($this->lot2, 1.5);
173
        $this->assertEquals(1.5, $build_request->getWithdrawAmountSum($this->bom_entry1b));
174
    }
175
176
177
}
178