Files
kimai2/tests/Validator/Constraints/TimeFormatValidatorTest.php
2025-08-11 18:57:42 +02:00

93 lines
2.6 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\TimeFormat;
use App\Validator\Constraints\TimeFormatValidator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
/**
* @extends ConstraintValidatorTestCase<TimeFormatValidator>
*/
#[CoversClass(TimeFormat::class)]
#[CoversClass(TimeFormatValidator::class)]
class TimeFormatValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): TimeFormatValidator
{
return new TimeFormatValidator();
}
public function testConstraintIsInvalid(): void
{
$this->expectException(UnexpectedTypeException::class);
$this->validator->validate('foo', new NotBlank());
}
public function testWrongValueThrowsException(): void
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Expected argument of type "string", "stdClass" given');
$this->validator->validate(new \stdClass(), new TimeFormat());
}
#[DataProvider('getValidTimes')]
public function testValidationSucceeds(?string $value): void
{
$this->validator->validate($value, new TimeFormat());
$this->assertNoViolation();
}
public static function getValidTimes(): array
{
return [
[''],
[null],
['00:00'],
['00:01'],
['23:00'],
['23:10'],
['23:01'],
['23:59'],
];
}
#[DataProvider('getInvalidTimes')]
public function testValidationProblem(?string $value): void
{
$this->validator->validate($value, new TimeFormat());
$this->buildViolation('The given value is not a valid time.')
->setParameter('{{ value }}', '"' . $value . '"')
->setCode(TimeFormat::INVALID_FORMAT)
->assertRaised();
}
public static function getInvalidTimes(): array
{
return [
['a'],
['1:00'],
['01:1'],
['00:60'],
['23:60'],
['23:1'],
['24:00'],
];
}
}