Added more tests

This commit is contained in:
Jan Böhmer 2026-05-11 21:50:33 +02:00
parent f3f93a8205
commit 7d27bff062
8 changed files with 756 additions and 1 deletions

View file

@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 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/>.
*/
namespace App\Tests\Validator\Constraints;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Parts\Part;
use App\Settings\MiscSettings\IpnSuggestSettings;
use App\Validator\Constraints\UniquePartIpnConstraint;
use App\Validator\Constraints\UniquePartIpnValidator;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
final class UniquePartIpnValidatorTest extends ConstraintValidatorTestCase
{
private EntityManagerInterface&MockObject $em;
private IpnSuggestSettings&MockObject $ipnSettings;
protected function createValidator(): ConstraintValidatorInterface
{
$this->em = $this->createMock(EntityManagerInterface::class);
// createMock() bypasses the ForbidConstructorTrait; public properties are accessible directly
$this->ipnSettings = $this->createMock(IpnSuggestSettings::class);
$this->ipnSettings->autoAppendSuffix = false;
return new UniquePartIpnValidator($this->em, $this->ipnSettings);
}
public function testNullValueIsValid(): void
{
$this->validator->validate(null, new UniquePartIpnConstraint());
$this->assertNoViolation();
}
public function testEmptyStringIsValid(): void
{
$this->validator->validate('', new UniquePartIpnConstraint());
$this->assertNoViolation();
}
public function testAutoAppendSuffixSkipsValidation(): void
{
$this->ipnSettings->autoAppendSuffix = true;
$this->validator->validate('IPN-001', new UniquePartIpnConstraint());
$this->assertNoViolation();
}
public function testUniqueIpnIsValid(): void
{
$repo = $this->createMock(\Doctrine\ORM\EntityRepository::class);
$repo->method('findBy')->willReturn([]);
$this->em->method('getRepository')->willReturn($repo);
$part = new Part();
$this->setObject($part);
$this->validator->validate('UNIQUE-IPN', new UniquePartIpnConstraint());
$this->assertNoViolation();
}
public function testDuplicateIpnRaisesViolation(): void
{
$existingPart = new Part();
$ref = new \ReflectionProperty(AbstractDBElement::class, 'id');
$ref->setValue($existingPart, 99);
$repo = $this->createMock(\Doctrine\ORM\EntityRepository::class);
$repo->method('findBy')->willReturn([$existingPart]);
$this->em->method('getRepository')->willReturn($repo);
// Validated part has no ID (new, unsaved part)
$part = new Part();
$this->setObject($part);
$constraint = new UniquePartIpnConstraint();
$this->validator->validate('DUPLICATE-IPN', $constraint);
$this->buildViolation($constraint->message)
->setParameter('{{ value }}', 'DUPLICATE-IPN')
->assertRaised();
}
}

View file

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 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/>.
*/
namespace App\Tests\Validator\Constraints;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Entity\Parts\StorageLocation;
use App\Validator\Constraints\ValidPartLot;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final class ValidPartLotValidatorTest extends WebTestCase
{
private static ValidatorInterface $validator;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$validator = self::getContainer()->get('validator');
}
public function testPartLotWithoutStorageLocationIsValid(): void
{
$lot = new PartLot();
$lot->setPart(new Part());
// No storage location set → validation should pass without any location checks
$violations = self::$validator->validate($lot, new ValidPartLot());
$this->assertCount(0, $violations);
}
public function testPartLotWithNonFullNonRestrictedStorageLocationIsValid(): void
{
$lot = new PartLot();
$lot->setPart(new Part());
$location = new StorageLocation();
// Default: not full, not limited — should be valid
$lot->setStorageLocation($location);
$violations = self::$validator->validate($lot, new ValidPartLot());
$this->assertCount(0, $violations);
}
public function testPartLotWithFullLocationAndNewLotRaisesViolation(): void
{
$lot = new PartLot();
$lot->setPart(new Part());
$location = new StorageLocation();
$location->setIsFull(true);
$lot->setStorageLocation($location);
// The lot has no ID (new entity), so "parts" is empty, and a full location will reject it
$violations = self::$validator->validate($lot, new ValidPartLot());
// Should raise a violation because the location is full and the part is not in the existing parts list
$this->assertGreaterThan(0, count($violations));
}
public function testNonPartLotValueThrowsException(): void
{
$this->expectException(\Symfony\Component\Form\Exception\UnexpectedTypeException::class);
self::$validator->validate('not a part lot', new ValidPartLot());
}
}