Made person identity optional for user

This commit is contained in:
Kevin Frantz 2018-11-22 21:54:57 +01:00
parent e63b265ee4
commit d67ffb2a50
4 changed files with 58 additions and 15 deletions

View File

@ -34,7 +34,11 @@ class UserSource extends AbstractComplexSource implements UserSourceInterface
public function __construct() public function __construct()
{ {
$this->personIdentitySource = new PersonIdentitySource();
parent::__construct(); parent::__construct();
} }
public function hasPersonIdentitySource(): bool
{
return isset($this->personIdentitySource);
}
} }

View File

@ -10,4 +10,10 @@ use App\Entity\Attribut\PersonIdentitySourceAttributInterface;
*/ */
interface UserSourceInterface extends ComplexSourceInterface, UserAttributInterface, PersonIdentitySourceAttributInterface interface UserSourceInterface extends ComplexSourceInterface, UserAttributInterface, PersonIdentitySourceAttributInterface
{ {
/**
* Checks if the user has an identity source.
*
* @return bool
*/
public function hasPersonIdentitySource(): bool;
} }

View File

@ -23,7 +23,24 @@ class UserSourceTest extends TestCase
public function testConstructor(): void public function testConstructor(): void
{ {
$this->assertInstanceOf(Collection::class, $this->userSource->getMemberships()); $this->assertInstanceOf(Collection::class, $this->userSource->getMemberships());
}
public function testHasPersonIdentitySource(): void
{
$this->assertFalse($this->userSource->hasPersonIdentitySource());
$this->userSource->setPersonIdentitySource($this->createMock(PersonIdentitySourceInterface::class));
$this->assertTrue($this->userSource->hasPersonIdentitySource());
$this->assertInstanceOf(PersonIdentitySourceInterface::class, $this->userSource->getPersonIdentitySource()); $this->assertInstanceOf(PersonIdentitySourceInterface::class, $this->userSource->getPersonIdentitySource());
}
public function testInitPersonIdentitySource(): void
{
$this->expectException(\TypeError::class);
$this->userSource->getPersonIdentitySource();
}
public function testInitUser(): void
{
$this->expectException(\TypeError::class); $this->expectException(\TypeError::class);
$this->userSource->getUser(); $this->userSource->getUser();
} }

View File

@ -8,6 +8,7 @@ use App\Repository\UserRepository;
use App\Entity\User; use App\Entity\User;
use App\Entity\UserInterface; use App\Entity\UserInterface;
use App\Entity\Source\Combination\PersonIdentitySourceInterface; use App\Entity\Source\Combination\PersonIdentitySourceInterface;
use App\Entity\Source\Complex\PersonIdentitySource;
class UserRepositoryTest extends KernelTestCase class UserRepositoryTest extends KernelTestCase
{ {
@ -26,11 +27,20 @@ class UserRepositoryTest extends KernelTestCase
*/ */
protected $loadedUser; protected $loadedUser;
/**
* @var UserInterface
*/
protected $user;
public function setUp(): void public function setUp(): void
{ {
$kernel = self::bootKernel(); $kernel = self::bootKernel();
$this->entityManager = $kernel->getContainer()->get('doctrine')->getManager(); $this->entityManager = $kernel->getContainer()->get('doctrine')->getManager();
$this->userRepository = $this->entityManager->getRepository(User::class); $this->userRepository = $this->entityManager->getRepository(User::class);
$this->user = new User();
$this->user->setUsername('Karl Marx');
$this->user->setEmail('mew21@test.de');
$this->user->setPassword('Die Philosophen haben die Welt nur verschieden interpretiert; es kommt aber darauf an, sie zu verändern.');
} }
/** /**
@ -39,32 +49,38 @@ class UserRepositoryTest extends KernelTestCase
*/ */
public function testCreation(): void public function testCreation(): void
{ {
$user = new User(); $this->entityManager->persist($this->user);
$user->setUsername('Karl Marx');
$user->setEmail('mew21@test.de');
$user->setPassword('Friedrich ist kein Engel!:)');
/**
* @var PersonIdentitySourceInterface
*/
$personIdentity = $user->getSource()->getPersonIdentitySource();
$personIdentity->getFullPersonNameSource()->getFirstNameSource()->setName('Karl');
$personIdentity->getFullPersonNameSource()->getSurnameSource()->setName('Marx');
$this->entityManager->persist($user);
$this->entityManager->flush(); $this->entityManager->flush();
$userId = $user->getId(); $userId = $this->user->getId();
/* /*
* @var UserInterface * @var UserInterface
*/ */
$this->loadedUser = $this->userRepository->find($userId); $this->loadedUser = $this->userRepository->find($userId);
$this->assertEquals($userId, $this->loadedUser->getId()); $this->assertEquals($userId, $this->loadedUser->getId());
$this->assertGreaterThan(0, $this->loadedUser->getSource()->getId()); $this->assertGreaterThan(0, $this->loadedUser->getSource()->getId());
$this->deleteUser();
$this->assertNull($this->userRepository->find($userId));
$this->loadedUser = null;
}
public function testUserWithPersonIdentitySource(): void
{
/**
* @var PersonIdentitySourceInterface
*/
$personIdentity = new PersonIdentitySource();
$personIdentity->getFullPersonNameSource()->getFirstNameSource()->setName('Karl');
$personIdentity->getFullPersonNameSource()->getSurnameSource()->setName('Marx');
$this->user->getSource()->setPersonIdentitySource($personIdentity);
$this->entityManager->persist($this->user);
$this->entityManager->flush();
$userId = $this->user->getId();
$this->loadedUser = $this->userRepository->find($userId);
$this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getId()); $this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getId());
$this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getFullPersonNameSource()->getId()); $this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getFullPersonNameSource()->getId());
$this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getFullPersonNameSource()->getFirstNameSource()->getId()); $this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getFullPersonNameSource()->getFirstNameSource()->getId());
$this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getFullPersonNameSource()->getSurnameSource()->getId()); $this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getFullPersonNameSource()->getSurnameSource()->getId());
$this->deleteUser(); $this->deleteUser();
$this->assertNull($this->userRepository->find($userId));
$this->loadedUser = null;
} }
private function deleteUser(): void private function deleteUser(): void