Integrated FOSUserBundle (#216)
This commit is contained in:
@@ -9,49 +9,61 @@
|
||||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Validator\ConstraintViolationInterface;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
|
||||
/**
|
||||
* @covers \App\Entity\Timesheet
|
||||
*/
|
||||
abstract class AbstractEntityTest extends TestCase
|
||||
abstract class AbstractEntityTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
* @param $value
|
||||
* @param $entity
|
||||
* @param array|string $fieldNames
|
||||
*/
|
||||
protected function assertHasViolationForField($value, $fieldNames)
|
||||
protected function assertHasViolationForField($entity, $fieldNames)
|
||||
{
|
||||
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
|
||||
$validations = $validator->validate($value);
|
||||
self::bootKernel();
|
||||
$validator = static::$kernel->getContainer()->get('validator');
|
||||
|
||||
$violations = $validator->validate($entity);
|
||||
|
||||
if (!is_array($fieldNames)) {
|
||||
$fieldNames = [$fieldNames];
|
||||
}
|
||||
|
||||
$expected = count($fieldNames);
|
||||
$actual = $violations->count();
|
||||
|
||||
$this->assertEquals($expected, $actual, sprintf('Expected %s violations, found %s.', $expected, $actual));
|
||||
|
||||
$violatedFields = [];
|
||||
/** @var ConstraintViolationInterface $validation */
|
||||
foreach ($validations as $validation) {
|
||||
$violatedFields[] = $validation->getPropertyPath();
|
||||
foreach ($violations as $validation) {
|
||||
$violatedFields[$validation->getPropertyPath()] = $validation->getPropertyPath();
|
||||
}
|
||||
|
||||
foreach ($fieldNames as $id => $propertyPath) {
|
||||
$foundField = false;
|
||||
if (in_array($propertyPath, $violatedFields)) {
|
||||
$foundField = true;
|
||||
unset($violatedFields[$id]);
|
||||
unset($violatedFields[$propertyPath]);
|
||||
}
|
||||
|
||||
$this->assertTrue($foundField, 'Failed finding violation for field: ' . $propertyPath);
|
||||
}
|
||||
|
||||
$this->assertEmpty($violatedFields, sprintf('Unexpected violations found: %s', implode(', ', $violatedFields)));
|
||||
}
|
||||
|
||||
$expected = count($fieldNames);
|
||||
$actual = $validations->count();
|
||||
protected function assertHasNoViolations($entity)
|
||||
{
|
||||
self::bootKernel();
|
||||
$validator = static::$kernel->getContainer()->get('validator');
|
||||
|
||||
$this->assertEquals($expected, $actual, sprintf('Expected %s violations, found %s.', $expected, $actual));
|
||||
$violations = $validator->validate($entity);
|
||||
$actual = $violations->count();
|
||||
|
||||
$this->assertEquals(0, $actual, sprintf('Expected 0 violations, found %s.', $actual));
|
||||
}
|
||||
}
|
||||
|
||||
110
tests/Entity/UserTest.php
Normal file
110
tests/Entity/UserTest.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?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\Entity;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Entity\UserPreference;
|
||||
|
||||
/**
|
||||
* @covers \App\Entity\User
|
||||
*/
|
||||
class UserTest extends AbstractEntityTest
|
||||
{
|
||||
public function getInvalidTestData()
|
||||
{
|
||||
return [
|
||||
['', ''],
|
||||
[null, null],
|
||||
['test', 'test@'], // too short username
|
||||
[str_pad('#', 61, '-'), 'test@x.'], // too long username
|
||||
[str_pad('#', 61, '-'), 'test@x.', ['xxxxx']], // too short password and invalid role
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidTestData
|
||||
*/
|
||||
public function testInvalidValues($username, $email, $roles = [])
|
||||
{
|
||||
$defaultFields = [
|
||||
'username', 'email'
|
||||
];
|
||||
|
||||
$user = new User();
|
||||
$user->setUsername($username);
|
||||
$user->setEmail($email);
|
||||
if (!empty($roles)) {
|
||||
$user->setRoles($roles);
|
||||
$defaultFields[] = 'roles';
|
||||
}
|
||||
|
||||
$this->assertHasViolationForField($user, $defaultFields);
|
||||
}
|
||||
|
||||
public function getValidTestData()
|
||||
{
|
||||
return [
|
||||
[str_pad('#', 6, '-'), 'test@x.x'], // shortest possible username
|
||||
[str_pad('#', 60, '-'), 'test@x.x', ['ROLE_CUSTOMER']], // longest possible password and valid role
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getValidTestData
|
||||
*/
|
||||
public function testValidValues($username, $email, $roles = [])
|
||||
{
|
||||
$user = new User();
|
||||
$user->setUsername($username);
|
||||
$user->setEmail($email);
|
||||
if (!empty($roles)) {
|
||||
$user->setRoles($roles);
|
||||
}
|
||||
|
||||
$this->assertHasNoViolations($user);
|
||||
}
|
||||
|
||||
public function testDatetime()
|
||||
{
|
||||
$date = new \DateTime('+1 day');
|
||||
$user = new User();
|
||||
$user->setRegisteredAt($date);
|
||||
$this->assertEquals($date, $user->getRegisteredAt());
|
||||
}
|
||||
|
||||
public function testPreferences()
|
||||
{
|
||||
$user = new User();
|
||||
$this->assertNull($user->getPreference('test'));
|
||||
$this->assertNull($user->getPreferenceValue('test'));
|
||||
$this->assertEquals('foo', $user->getPreferenceValue('test', 'foo'));
|
||||
|
||||
$preference = new UserPreference();
|
||||
$preference
|
||||
->setName('test')
|
||||
->setValue('foobar');
|
||||
$user->addPreference($preference);
|
||||
$this->assertEquals('foobar', $user->getPreferenceValue('test', 'foo'));
|
||||
$this->assertEquals($preference, $user->getPreference('test'));
|
||||
}
|
||||
|
||||
public function testToString()
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$user->setUsername('bar');
|
||||
$this->assertEquals('bar', (string) $user);
|
||||
$this->assertEquals('bar', $user->getUsername());
|
||||
|
||||
$user->setAlias('foo');
|
||||
$this->assertEquals('foo', (string) $user);
|
||||
$this->assertEquals('foo', $user->getAlias());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user