Files
kimai/tests/Validator/Constraints/DurationValidatorTest.php
Kevin Papst 677f8d776c [3.0] Update Symfony to 7.4 and dependencies, removed deprecated code, removed API password (#5622)
* deactivate deprecated auto-mapping
* use native month-picker with fallback on old browser
* bump dependencies to new major versions
* replace deprecated ARRAY column type with JSON
* named arguments
* nullable arguments
* native lazy ghosts
* update routes to use php config
* disable PHP < 8.4
* use static RTL configuration
* new qr code package
* clear out env file
* recipe updates
* fix deprecations
* preset new env variable if not existing
* bump maria db version examples
* remove deprecated api-token support
* fix validator deprecations
* remove timesheet category column
* fix broken validator autoconfiguration
* stabilize tests
* fix migration syntax compatibility
* fix doctrine deprecation
* fix datetime format
* remove constructor dependency
* only include visible preferences in invoice templates
* fix test container dependency
* removed ProjectConstraint multi-constraint logic
* allow to disable plugins in certain environments
* refactor TimesheetConstraint to non-service
* activate new interface methods
* prevent update issues with .env
* improved customer fixture
* removed $user->isExportDecimal()
2026-01-04 15:04:25 +01:00

131 lines
4.0 KiB
PHP

<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Validator\Constraints;
use App\Validator\Constraints\Duration;
use App\Validator\Constraints\DurationValidator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* @extends ConstraintValidatorTestCase<DurationValidator>
*/
#[CoversClass(Duration::class)]
#[CoversClass(DurationValidator::class)]
class DurationValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): DurationValidator
{
return new DurationValidator();
}
/**
* @return array<array<string|int|null>>
*/
public static function getValidData(): array
{
return [
['2h'],
['38m'],
['99s'],
['2h38m'],
['2h38s'],
['2m38s'],
['2h38m17s'],
['1h96m137s'],
[''],
['0'],
['1.2'],
['2,3'],
[null],
[0],
[11257200],
['13:27'],
['13:27:54'],
['12:87:54'],
['3127:00:00'],
['3127:00'],
[48474],
];
}
public function testConstraintIsInvalid(): void
{
$this->expectException(UnexpectedTypeException::class);
$this->validator->validate('foo', new NotBlank());
}
#[DataProvider('getValidData')]
public function testConstraintWithValidData(string|int|null $input): void
{
$constraint = new Duration();
$this->validator->validate($input, $constraint);
if ($input !== null) {
$this->validator->validate(strtoupper($input), $constraint);
}
$this->assertNoViolation();
}
/**
* @return array<array<string>>
*/
public static function getInvalidData(): array
{
return [
['13-13'],
['2m3m'],
['2s3s'],
['2h3h'],
['2m3h'],
['2s3h'],
['2s3m'],
['3127::00'],
['3127:00:'],
[':3127:00'],
['::3127'],
['foo'],
];
}
#[DataProvider('getInvalidData')]
public function testValidationError(string $input): void
{
$constraint = new Duration(message: 'myMessage');
$this->validator->validate($input, $constraint);
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"' . $input . '"')
->setParameter('{{ pattern }}', '/^-?[0-9]{1,}$|^-?[0-9]{1,}[,.]{1}[0-9]{1,}$|^-?[0-9]{1,}:[0-9]{1,}:[0-9]{1,}$|^-?[0-9]{1,}:[0-9]{1,}$|^[0-9]{1,}[hHmMsS]{1}$|^[0-9]{1,}[hH]{1}[0-9]{1,}[mM]{1}$|^[0-9]{1,}[hHmM]{1}[0-9]{1,}[sS]{1}$|^[0-9]{1,}[mM]{1}[0-9]{1,}[sS]{1}$|^[0-9]{1,}[hH]{1}[0-9]{1,}[mM]{1}[0-9]{1,}[sS]{1}$/')
->setCode(Regex::REGEX_FAILED_ERROR)
->assertRaised();
}
#[DataProvider('getInvalidData')]
public function testValidationErrorUpperCase(string $input): void
{
$input = strtoupper($input);
$constraint = new Duration(message: 'myMessage');
$this->validator->validate($input, $constraint);
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"' . $input . '"')
->setParameter('{{ pattern }}', '/^-?[0-9]{1,}$|^-?[0-9]{1,}[,.]{1}[0-9]{1,}$|^-?[0-9]{1,}:[0-9]{1,}:[0-9]{1,}$|^-?[0-9]{1,}:[0-9]{1,}$|^[0-9]{1,}[hHmMsS]{1}$|^[0-9]{1,}[hH]{1}[0-9]{1,}[mM]{1}$|^[0-9]{1,}[hHmM]{1}[0-9]{1,}[sS]{1}$|^[0-9]{1,}[mM]{1}[0-9]{1,}[sS]{1}$|^[0-9]{1,}[hH]{1}[0-9]{1,}[mM]{1}[0-9]{1,}[sS]{1}$/')
->setCode(Regex::REGEX_FAILED_ERROR)
->assertRaised();
}
}