API improvements (#1826)

This commit is contained in:
Kevin Papst
2020-07-17 20:30:16 +02:00
committed by GitHub
parent 5864c7e127
commit c843dba29a
77 changed files with 1745 additions and 1189 deletions

View File

@@ -10,6 +10,7 @@
namespace App\Tests\Entity;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Classes using this MUST extend \Symfony\Bundle\FrameworkBundle\Test\KernelTestCase
@@ -20,12 +21,12 @@ trait EntityValidationTestTrait
* @param object $entity
* @param array|string $fieldNames
*/
protected function assertHasViolationForField(object $entity, $fieldNames)
protected function assertHasViolationForField(object $entity, $fieldNames, $groups = null)
{
self::bootKernel();
/** @var ValidatorInterface $validator */
$validator = static::$kernel->getContainer()->get('validator');
$violations = $validator->validate($entity);
$violations = $validator->validate($entity, null, $groups);
if (!\is_array($fieldNames)) {
$fieldNames = [$fieldNames];
@@ -55,12 +56,13 @@ trait EntityValidationTestTrait
$this->assertEquals($expected, $countViolations, sprintf('Expected %s violations, found %s in %s.', $expected, $actual, implode(', ', array_keys($violatedFields))));
}
protected function assertHasNoViolations($entity)
protected function assertHasNoViolations($entity, $groups = null)
{
self::bootKernel();
/** @var ValidatorInterface $validator */
$validator = static::$kernel->getContainer()->get('validator');
$violations = $validator->validate($entity);
$violations = $validator->validate($entity, null, $groups);
$actual = $violations->count();
$this->assertEquals(0, $actual, sprintf('Expected 0 violations, found %s.', $actual));

View File

@@ -27,7 +27,6 @@ class UserValidationTest extends KernelTestCase
[null, null],
['x', 'test@'], // too short username
[str_pad('#', 61, '-'), 'test@x.'], // too long username
[str_pad('#', 61, '-'), 'test@x.', ['xxxxx']], // too short password and invalid role
];
}
@@ -48,14 +47,34 @@ class UserValidationTest extends KernelTestCase
$defaultFields[] = 'roles';
}
$this->assertHasViolationForField($user, $defaultFields);
$this->assertHasViolationForField($user, $defaultFields, ['Profile']);
}
public function testInvalidRoles()
{
$user = new User();
$user->setUsername('foo');
$user->setEmail('foo@example.com');
$user->setRoles(['xxxxxx']);
$this->assertHasViolationForField($user, ['roles'], ['RolesUpdate']);
}
public function testValidRoles()
{
$user = new User();
$user->setUsername('foo');
$user->setEmail('foo@example.com');
$user->setRoles(['ROLE_TEAMLEAD']);
$this->assertHasNoViolations($user, ['RolesUpdate']);
}
public function getValidTestData()
{
return [
[str_pad('#', 3, '-'), 'test@x.x'], // shortest possible username
[str_pad('#', 60, '-'), 'test@x.x', ['ROLE_TEAMLEAD']], // longest possible password and valid role
[str_pad('#', 8, '-'), 'test@x.x'], // shortest possible username
[str_pad('#', 60, '-'), 'test@x.x'], // longest possible username
];
}
@@ -71,6 +90,6 @@ class UserValidationTest extends KernelTestCase
$user->setRoles($roles);
}
$this->assertHasNoViolations($user);
$this->assertHasNoViolations($user, ['Profile']);
}
}