Added additional tests

This commit is contained in:
Jan Böhmer 2026-05-11 20:28:40 +02:00
parent cb669ad4ec
commit 65a6f46369
12 changed files with 2392 additions and 1441 deletions

View file

@ -0,0 +1,95 @@
<?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\BigDecimal;
use App\Validator\Constraints\BigDecimal\BigDecimalGreaterThanValidator;
use App\Validator\Constraints\BigDecimal\BigDecimalPositive;
use Brick\Math\BigDecimal;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* Tests BigDecimalGreaterThanValidator via the BigDecimalPositive constraint (value > 0).
*/
final class BigDecimalGreaterThanValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): ConstraintValidatorInterface
{
return new BigDecimalGreaterThanValidator();
}
public function testNullIsValid(): void
{
$this->validator->validate(null, new BigDecimalPositive());
$this->assertNoViolation();
}
public function testPositiveIntegerIsValid(): void
{
$this->validator->validate(1, new BigDecimalPositive());
$this->assertNoViolation();
}
public function testPositiveStringIsValid(): void
{
$this->validator->validate('0.01', new BigDecimalPositive());
$this->assertNoViolation();
}
public function testPositiveBigDecimalIsValid(): void
{
$this->validator->validate(BigDecimal::of('1.5'), new BigDecimalPositive());
$this->assertNoViolation();
}
public function testZeroIsInvalid(): void
{
$constraint = new BigDecimalPositive();
$this->validator->validate(0, $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '0', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThan::TOO_LOW_ERROR)
->assertRaised();
}
public function testZeroBigDecimalIsInvalid(): void
{
$constraint = new BigDecimalPositive();
$this->validator->validate(BigDecimal::of('0.00'), $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '0.00', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThan::TOO_LOW_ERROR)
->assertRaised();
}
public function testNegativeIsInvalid(): void
{
$constraint = new BigDecimalPositive();
$this->validator->validate(-1, $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '-1', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThan::TOO_LOW_ERROR)
->assertRaised();
}
}

View file

@ -0,0 +1,91 @@
<?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\BigDecimal;
use App\Validator\Constraints\BigDecimal\BigDecimalGreaterThenOrEqualValidator;
use App\Validator\Constraints\BigDecimal\BigDecimalPositiveOrZero;
use Brick\Math\BigDecimal;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* Tests BigDecimalGreaterThenOrEqualValidator via the BigDecimalPositiveOrZero constraint (value >= 0).
*/
final class BigDecimalGreaterThenOrEqualValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): ConstraintValidatorInterface
{
return new BigDecimalGreaterThenOrEqualValidator();
}
public function testNullIsValid(): void
{
$this->validator->validate(null, new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testPositiveIntegerIsValid(): void
{
$this->validator->validate(1, new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testZeroIsValid(): void
{
$this->validator->validate(0, new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testZeroBigDecimalIsValid(): void
{
$this->validator->validate(BigDecimal::of('0.00'), new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testPositiveBigDecimalIsValid(): void
{
$this->validator->validate(BigDecimal::of('3.14'), new BigDecimalPositiveOrZero());
$this->assertNoViolation();
}
public function testNegativeIsInvalid(): void
{
$constraint = new BigDecimalPositiveOrZero();
$this->validator->validate(-1, $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '-1', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThanOrEqual::TOO_LOW_ERROR)
->assertRaised();
}
public function testNegativeBigDecimalIsInvalid(): void
{
$constraint = new BigDecimalPositiveOrZero();
$this->validator->validate(BigDecimal::of('-0.01'), $constraint);
$this->buildViolation($constraint->message)
->setParameters(['{{ value }}' => '-0.01', '{{ compared_value }}' => '0', '{{ compared_value_type }}' => 'int'])
->setCode(\Symfony\Component\Validator\Constraints\GreaterThanOrEqual::TOO_LOW_ERROR)
->assertRaised();
}
}

View file

@ -0,0 +1,81 @@
<?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\Validator\Constraints\ValidFileFilter;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final class ValidFileFilterValidatorTest extends WebTestCase
{
private static ValidatorInterface $validator;
public static function setUpBeforeClass(): void
{
self::bootKernel();
self::$validator = self::getContainer()->get('validator');
}
public function testNullIsValid(): void
{
$violations = self::$validator->validate(null, new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testEmptyStringIsValid(): void
{
$violations = self::$validator->validate('', new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testValidExtensionFilterIsValid(): void
{
$violations = self::$validator->validate('.jpg,.png', new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testValidMimeTypeFilterIsValid(): void
{
$violations = self::$validator->validate('image/*', new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testMixedValidFilterIsValid(): void
{
$violations = self::$validator->validate('image/*, .pdf, video/mp4', new ValidFileFilter());
$this->assertCount(0, $violations);
}
public function testInvalidFilterRaisesViolation(): void
{
$violations = self::$validator->validate('*.notvalid', new ValidFileFilter());
$this->assertCount(1, $violations);
}
public function testFullFilenameRaisesViolation(): void
{
$violations = self::$validator->validate('test.png', new ValidFileFilter());
$this->assertCount(1, $violations);
}
}

View file

@ -0,0 +1,73 @@
<?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\Validator\Constraints\Year2038BugWorkaround;
use App\Validator\Constraints\Year2038BugWorkaroundValidator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
final class Year2038BugWorkaroundValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): ConstraintValidatorInterface
{
// Disable validation by default so tests run on both 32- and 64-bit systems
return new Year2038BugWorkaroundValidator(disable_validation: true);
}
public function testIsNotActivatedWhenDisabled(): void
{
$validator = new Year2038BugWorkaroundValidator(disable_validation: true);
$this->assertFalse($validator->isActivated());
}
public function testIsNotActivatedOn64Bit(): void
{
// On any normal 64-bit CI/dev system PHP_INT_SIZE === 8, so activation requires 32-bit
if (PHP_INT_SIZE !== 8) {
$this->markTestSkipped('This test is only meaningful on 64-bit systems.');
}
$validator = new Year2038BugWorkaroundValidator(disable_validation: false);
$this->assertFalse($validator->isActivated());
}
public function testNullValueProducesNoViolation(): void
{
$this->validator->validate(null, new Year2038BugWorkaround());
$this->assertNoViolation();
}
public function testDateBefore2038ProducesNoViolationWhenDisabled(): void
{
$this->validator->validate(new \DateTime('2037-01-01'), new Year2038BugWorkaround());
$this->assertNoViolation();
}
public function testDateAfter2038ProducesNoViolationWhenDisabled(): void
{
// Validation disabled → even a "bad" date causes no violation
$this->validator->validate(new \DateTime('2039-01-01'), new Year2038BugWorkaround());
$this->assertNoViolation();
}
}