2019-11-09 00:31:42 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-11-09 00:31:42 +01:00
|
|
|
namespace App\Tests\Entity\Parts;
|
|
|
|
|
|
|
|
use App\Entity\Parts\MeasurementUnit;
|
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
use App\Entity\Parts\PartLot;
|
2020-01-05 22:49:00 +01:00
|
|
|
use DateTime;
|
2019-11-09 00:31:42 +01:00
|
|
|
use Doctrine\Common\Collections\Collection;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class PartTest extends TestCase
|
|
|
|
{
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testAddRemovePartLot(): void
|
2019-11-09 00:31:42 +01:00
|
|
|
{
|
|
|
|
$part = new Part();
|
|
|
|
$this->assertInstanceOf(Collection::class, $part->getPartLots());
|
|
|
|
$this->assertTrue($part->getPartLots()->isEmpty());
|
|
|
|
|
|
|
|
//Add element
|
|
|
|
$lot = new PartLot();
|
|
|
|
$part->addPartLot($lot);
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame($part, $lot->getPart());
|
|
|
|
$this->assertSame(1, $part->getPartLots()->count());
|
2019-11-09 00:31:42 +01:00
|
|
|
|
|
|
|
//Remove element
|
|
|
|
$part->removePartLot($lot);
|
|
|
|
$this->assertTrue($part->getPartLots()->isEmpty());
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetSetMinamount(): void
|
2019-11-09 00:31:42 +01:00
|
|
|
{
|
|
|
|
$part = new Part();
|
|
|
|
$measurement_unit = new MeasurementUnit();
|
|
|
|
|
2023-04-15 23:14:53 +02:00
|
|
|
//Without a set measurement unit the part must return an int
|
2019-11-09 00:31:42 +01:00
|
|
|
$part->setMinAmount(1.345);
|
2024-06-22 00:31:43 +02:00
|
|
|
$this->assertEqualsWithDelta(1.0, $part->getMinAmount(), PHP_FLOAT_EPSILON);
|
2019-11-09 00:31:42 +01:00
|
|
|
|
2023-04-15 23:14:53 +02:00
|
|
|
//If a non-int-based unit is assigned, a float is returned
|
2019-11-09 00:31:42 +01:00
|
|
|
$part->setPartUnit($measurement_unit);
|
2024-06-22 00:31:43 +02:00
|
|
|
$this->assertEqualsWithDelta(1.345, $part->getMinAmount(), PHP_FLOAT_EPSILON);
|
2019-11-09 00:31:42 +01:00
|
|
|
|
|
|
|
//If an int-based unit is assigned an int is returned
|
|
|
|
$measurement_unit->setIsInteger(true);
|
2024-06-22 00:31:43 +02:00
|
|
|
$this->assertEqualsWithDelta(1.0, $part->getMinAmount(), PHP_FLOAT_EPSILON);
|
2019-11-09 00:31:42 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testUseFloatAmount(): void
|
2019-11-09 00:31:42 +01:00
|
|
|
{
|
|
|
|
$part = new Part();
|
|
|
|
$measurement_unit = new MeasurementUnit();
|
|
|
|
|
|
|
|
//Without an measurement unit int should be used
|
|
|
|
$this->assertFalse($part->useFloatAmount());
|
|
|
|
|
|
|
|
$part->setPartUnit($measurement_unit);
|
|
|
|
$this->assertTrue($part->useFloatAmount());
|
|
|
|
|
|
|
|
$measurement_unit->setIsInteger(true);
|
|
|
|
$this->assertFalse($part->useFloatAmount());
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetAmountSum(): void
|
2019-11-09 00:31:42 +01:00
|
|
|
{
|
|
|
|
$part = new Part();
|
|
|
|
$measurement_unit = new MeasurementUnit();
|
2020-01-05 22:49:00 +01:00
|
|
|
$datetime = new DateTime();
|
2019-11-09 00:31:42 +01:00
|
|
|
|
2024-06-22 00:31:43 +02:00
|
|
|
$this->assertEqualsWithDelta(0.0, $part->getAmountSum(), PHP_FLOAT_EPSILON);
|
2019-11-09 00:31:42 +01:00
|
|
|
|
|
|
|
$part->addPartLot((new PartLot())->setAmount(3.141));
|
|
|
|
$part->addPartLot((new PartLot())->setAmount(10.0));
|
|
|
|
$part->addPartLot((new PartLot())->setAmount(5)->setInstockUnknown(true));
|
|
|
|
$part->addPartLot(
|
|
|
|
(new PartLot())
|
|
|
|
->setAmount(6)
|
2024-06-22 17:36:54 +02:00
|
|
|
->setExpirationDate(new \DateTimeImmutable('-1 hour'))
|
2019-11-09 00:31:42 +01:00
|
|
|
);
|
|
|
|
|
2024-06-22 00:31:43 +02:00
|
|
|
$this->assertEqualsWithDelta(13.0, $part->getAmountSum(), PHP_FLOAT_EPSILON);
|
2019-11-09 00:31:42 +01:00
|
|
|
|
|
|
|
$part->setPartUnit($measurement_unit);
|
2024-06-22 00:31:43 +02:00
|
|
|
$this->assertEqualsWithDelta(13.141, $part->getAmountSum(), PHP_FLOAT_EPSILON);
|
2019-11-09 00:31:42 +01:00
|
|
|
|
|
|
|
//1 billion part lot
|
2023-06-11 14:18:53 +02:00
|
|
|
$part->addPartLot((new PartLot())->setAmount(1_000_000_000));
|
2024-06-22 00:31:43 +02:00
|
|
|
$this->assertEqualsWithDelta(1_000_000_013.141, $part->getAmountSum(), PHP_FLOAT_EPSILON);
|
2019-11-09 00:31:42 +01:00
|
|
|
$measurement_unit->setIsInteger(true);
|
2024-06-22 00:31:43 +02:00
|
|
|
$this->assertEqualsWithDelta(1_000_000_013.0, $part->getAmountSum(), PHP_FLOAT_EPSILON);
|
2019-11-09 00:31:42 +01:00
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|