* 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()
112 lines
3.7 KiB
PHP
112 lines
3.7 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\Entity\User as UserEntity;
|
|
use App\Tests\Security\TestUserEntity;
|
|
use App\User\UserService;
|
|
use App\Validator\Constraints\User;
|
|
use App\Validator\Constraints\UserValidator;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
|
|
|
/**
|
|
* @extends ConstraintValidatorTestCase<UserValidator>
|
|
*/
|
|
#[CoversClass(\App\Validator\Constraints\User::class)]
|
|
#[CoversClass(UserValidator::class)]
|
|
class UserValidatorTest extends ConstraintValidatorTestCase
|
|
{
|
|
protected function createValidator(): UserValidator
|
|
{
|
|
$userService = $this->createMock(UserService::class);
|
|
|
|
return new UserValidator($userService);
|
|
}
|
|
|
|
public function testConstraintIsInvalid(): void
|
|
{
|
|
$this->expectException(UnexpectedTypeException::class);
|
|
|
|
$this->validator->validate('foo', new NotBlank()); // @phpstan-ignore argument.type
|
|
}
|
|
|
|
public function testNullIsValid(): void
|
|
{
|
|
$this->validator->validate(null, new User(message: 'myMessage')); // @phpstan-ignore argument.type
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testNonUserIsValid(): void
|
|
{
|
|
$this->validator->validate(new TestUserEntity(), new User(message: 'myMessage')); // @phpstan-ignore argument.type
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testEmptyUserIsValid(): void
|
|
{
|
|
$user = new UserEntity();
|
|
$user->setUserIdentifier('foo');
|
|
$user->setEmail('test');
|
|
$this->validator->validate($user, new User(message: 'myMessage'));
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testUserIsValidWithEmptyRepository(): void
|
|
{
|
|
$user = new UserEntity();
|
|
$user->setUserIdentifier('foo');
|
|
$user->setEmail('foo@example.com');
|
|
|
|
$this->validator->validate($user, new User(message: 'myMessage'));
|
|
|
|
$this->assertNoViolation();
|
|
}
|
|
|
|
public function testUserIsInvalidWithRepository(): void
|
|
{
|
|
$existing = $this->createMock(UserEntity::class);
|
|
$existing->expects($this->exactly(4))->method('getId')->willReturn(123);
|
|
|
|
$userService = $this->createMock(UserService::class);
|
|
$userService->expects($this->exactly(2))->method('findUserByEmail')->willReturn($existing);
|
|
$userService->expects($this->exactly(2))->method('findUserByName')->willReturn($existing);
|
|
|
|
$this->validator = new UserValidator($userService);
|
|
$this->validator->initialize($this->context);
|
|
|
|
$user = new UserEntity();
|
|
$user->setUserIdentifier('foo');
|
|
$user->setEmail('foo@example.com');
|
|
|
|
$this->validator->validate($user, new User());
|
|
|
|
$this
|
|
->buildViolation('The email is already used.')
|
|
->atPath('property.path.email')
|
|
->setCode(User::USER_EXISTING_EMAIL)
|
|
->buildNextViolation('An equal username is already used.')
|
|
->atPath('property.path.email')
|
|
->setCode(User::USER_EXISTING_EMAIL_AS_NAME)
|
|
->buildNextViolation('An equal email is already used.')
|
|
->atPath('property.path.username')
|
|
->setCode(User::USER_EXISTING_NAME_AS_EMAIL)
|
|
->buildNextViolation('The username is already used.')
|
|
->atPath('property.path.username')
|
|
->setCode(User::USER_EXISTING_NAME)
|
|
->assertRaised();
|
|
}
|
|
}
|