mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-05-13 06:51:29 +00:00
Test some more edge cases in tests
This commit is contained in:
parent
47ab18175f
commit
112e962239
8 changed files with 690 additions and 41 deletions
|
|
@ -154,6 +154,33 @@ final class UniqueObjectCollectionValidatorTest extends ConstraintValidatorTestC
|
|||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testThirdElementDuplicatePointsToIndexTwo(): void
|
||||
{
|
||||
// First two elements are unique; only the third duplicates the first.
|
||||
$this->validator->validate(new ArrayCollection([
|
||||
new DummyUniqueValidatableObject(['a' => 1]),
|
||||
new DummyUniqueValidatableObject(['a' => 2]),
|
||||
new DummyUniqueValidatableObject(['a' => 1]), // duplicate of index 0
|
||||
]),
|
||||
new UniqueObjectCollection(fields: ['a']));
|
||||
|
||||
$this
|
||||
->buildViolation('This value is already used.')
|
||||
->setCode(UniqueObjectCollection::IS_NOT_UNIQUE)
|
||||
->setParameter('{{ object }}', 'objectString')
|
||||
->atPath('property.path[2].a')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
public function testAllNullsWithAllowNullProducesNoViolation(): void
|
||||
{
|
||||
$this->validator->validate(new ArrayCollection([
|
||||
new DummyUniqueValidatableObject(['a' => null]),
|
||||
new DummyUniqueValidatableObject(['a' => null]),
|
||||
new DummyUniqueValidatableObject(['a' => null]),
|
||||
]),
|
||||
new UniqueObjectCollection(fields: ['a'], allowNull: true));
|
||||
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,52 +24,54 @@ namespace App\Tests\Validator\Constraints;
|
|||
|
||||
use App\Validator\Constraints\ValidGTIN;
|
||||
use App\Validator\Constraints\ValidGTINValidator;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Validator\ConstraintValidatorInterface;
|
||||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
||||
|
||||
final class ValidGTINValidatorTest extends ConstraintValidatorTestCase
|
||||
{
|
||||
|
||||
public function testAllowNull(): void
|
||||
{
|
||||
$this->validator->validate(null, new ValidGTIN());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidGTIN8(): void
|
||||
{
|
||||
$this->validator->validate('12345670', new ValidGTIN());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidGTIN12(): void
|
||||
{
|
||||
$this->validator->validate('123456789012', new ValidGTIN());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidGTIN13(): void
|
||||
{
|
||||
$this->validator->validate('1234567890128', new ValidGTIN());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testValidGTIN14(): void
|
||||
{
|
||||
$this->validator->validate('12345678901231', new ValidGTIN());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
public function testInvalidGTIN(): void
|
||||
{
|
||||
$this->validator->validate('1234567890123', new ValidGTIN());
|
||||
$this->buildViolation('validator.invalid_gtin')
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
protected function createValidator(): ConstraintValidatorInterface
|
||||
{
|
||||
return new ValidGTINValidator();
|
||||
}
|
||||
|
||||
// --- values that must produce no violation ---
|
||||
|
||||
public static function validValuesProvider(): \Generator
|
||||
{
|
||||
yield 'null is skipped' => [null];
|
||||
yield 'empty string is skipped' => [''];
|
||||
yield 'valid GTIN-8' => ['12345670'];
|
||||
yield 'valid GTIN-12' => ['123456789012'];
|
||||
yield 'valid GTIN-13' => ['1234567890128'];
|
||||
yield 'valid GTIN-14' => ['12345678901231'];
|
||||
}
|
||||
|
||||
#[DataProvider('validValuesProvider')]
|
||||
public function testValidValue(mixed $value): void
|
||||
{
|
||||
$this->validator->validate($value, new ValidGTIN());
|
||||
$this->assertNoViolation();
|
||||
}
|
||||
|
||||
// --- values that must produce a violation ---
|
||||
|
||||
public static function invalidValuesProvider(): \Generator
|
||||
{
|
||||
yield 'wrong check digit (GTIN-13)' => ['1234567890123'];
|
||||
yield 'non-numeric string' => ['ABCDEFGHIJKLM'];
|
||||
yield 'wrong length — 9 digits' => ['123456789'];
|
||||
yield 'wrong length — 11 digits' => ['12345678901'];
|
||||
yield 'leading whitespace' => [' 1234567890128'];
|
||||
yield 'trailing whitespace' => ['1234567890128 '];
|
||||
}
|
||||
|
||||
#[DataProvider('invalidValuesProvider')]
|
||||
public function testInvalidValue(string $value): void
|
||||
{
|
||||
$this->validator->validate($value, new ValidGTIN());
|
||||
$this->buildViolation('validator.invalid_gtin')
|
||||
->assertRaised();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,4 +83,41 @@ final class ValidPartLotValidatorTest extends WebTestCase
|
|||
$this->expectException(\Symfony\Component\Form\Exception\UnexpectedTypeException::class);
|
||||
self::$validator->validate('not a part lot', new ValidPartLot());
|
||||
}
|
||||
|
||||
public function testPartLotWithFullLocationRaisesNamedViolation(): void
|
||||
{
|
||||
$lot = new PartLot();
|
||||
$lot->setPart(new Part());
|
||||
|
||||
$location = new StorageLocation();
|
||||
$location->setIsFull(true);
|
||||
$lot->setStorageLocation($location);
|
||||
|
||||
$violations = self::$validator->validate($lot, new ValidPartLot());
|
||||
// Expect exactly one violation on the storage_location path
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('storage_location', $violations[0]->getPropertyPath());
|
||||
$this->assertStringContainsString('location_full', $violations[0]->getMessageTemplate());
|
||||
}
|
||||
|
||||
public function testLimitToExistingPartsWithNewLotRaisesViolation(): void
|
||||
{
|
||||
$lot = new PartLot();
|
||||
$lot->setPart(new Part());
|
||||
|
||||
$location = new StorageLocation();
|
||||
$location->setLimitToExistingParts(true);
|
||||
$lot->setStorageLocation($location);
|
||||
|
||||
// New lot (no ID) → parts collection is empty → part is not in the list → violation
|
||||
$violations = self::$validator->validate($lot, new ValidPartLot());
|
||||
$this->assertCount(1, $violations);
|
||||
$this->assertSame('storage_location', $violations[0]->getPropertyPath());
|
||||
$this->assertSame('validator.part_lot.only_existing', $violations[0]->getMessageTemplate());
|
||||
}
|
||||
|
||||
// NOTE: The 'location_full.no_increase' violation (raised when a lot's amount
|
||||
// is increased while its storage location is marked full) requires the entity to
|
||||
// carry a real Doctrine originalEntityData snapshot, which is only set after an
|
||||
// actual persist+flush. Testing that path belongs in a database integration test.
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue