From d67ffb2a506abf47d92845c33edbcf811b7181da Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Thu, 22 Nov 2018 21:54:57 +0100 Subject: [PATCH] Made person identity optional for user --- .../src/Entity/Source/Complex/UserSource.php | 6 ++- .../Source/Complex/UserSourceInterface.php | 6 +++ .../Entity/Source/Complex/UserSourceTest.php | 17 +++++++ .../Unit/Repository/UserRepositoryTest.php | 44 +++++++++++++------ 4 files changed, 58 insertions(+), 15 deletions(-) diff --git a/application/src/Entity/Source/Complex/UserSource.php b/application/src/Entity/Source/Complex/UserSource.php index 6f869e1..7ad54bb 100644 --- a/application/src/Entity/Source/Complex/UserSource.php +++ b/application/src/Entity/Source/Complex/UserSource.php @@ -34,7 +34,11 @@ class UserSource extends AbstractComplexSource implements UserSourceInterface public function __construct() { - $this->personIdentitySource = new PersonIdentitySource(); parent::__construct(); } + + public function hasPersonIdentitySource(): bool + { + return isset($this->personIdentitySource); + } } diff --git a/application/src/Entity/Source/Complex/UserSourceInterface.php b/application/src/Entity/Source/Complex/UserSourceInterface.php index f743f8d..cb85b57 100644 --- a/application/src/Entity/Source/Complex/UserSourceInterface.php +++ b/application/src/Entity/Source/Complex/UserSourceInterface.php @@ -10,4 +10,10 @@ use App\Entity\Attribut\PersonIdentitySourceAttributInterface; */ interface UserSourceInterface extends ComplexSourceInterface, UserAttributInterface, PersonIdentitySourceAttributInterface { + /** + * Checks if the user has an identity source. + * + * @return bool + */ + public function hasPersonIdentitySource(): bool; } diff --git a/application/tests/Unit/Entity/Source/Complex/UserSourceTest.php b/application/tests/Unit/Entity/Source/Complex/UserSourceTest.php index ba74dde..f8e4e6a 100644 --- a/application/tests/Unit/Entity/Source/Complex/UserSourceTest.php +++ b/application/tests/Unit/Entity/Source/Complex/UserSourceTest.php @@ -23,7 +23,24 @@ class UserSourceTest extends TestCase public function testConstructor(): void { $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()); + } + + public function testInitPersonIdentitySource(): void + { + $this->expectException(\TypeError::class); + $this->userSource->getPersonIdentitySource(); + } + + public function testInitUser(): void + { $this->expectException(\TypeError::class); $this->userSource->getUser(); } diff --git a/application/tests/Unit/Repository/UserRepositoryTest.php b/application/tests/Unit/Repository/UserRepositoryTest.php index b82e40e..d3632d9 100644 --- a/application/tests/Unit/Repository/UserRepositoryTest.php +++ b/application/tests/Unit/Repository/UserRepositoryTest.php @@ -8,6 +8,7 @@ use App\Repository\UserRepository; use App\Entity\User; use App\Entity\UserInterface; use App\Entity\Source\Combination\PersonIdentitySourceInterface; +use App\Entity\Source\Complex\PersonIdentitySource; class UserRepositoryTest extends KernelTestCase { @@ -26,11 +27,20 @@ class UserRepositoryTest extends KernelTestCase */ protected $loadedUser; + /** + * @var UserInterface + */ + protected $user; + public function setUp(): void { $kernel = self::bootKernel(); $this->entityManager = $kernel->getContainer()->get('doctrine')->getManager(); $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 { - $user = new 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->persist($this->user); $this->entityManager->flush(); - $userId = $user->getId(); + $userId = $this->user->getId(); /* * @var UserInterface */ $this->loadedUser = $this->userRepository->find($userId); $this->assertEquals($userId, $this->loadedUser->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()->getFullPersonNameSource()->getId()); $this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getFullPersonNameSource()->getFirstNameSource()->getId()); $this->assertGreaterThan(0, $this->loadedUser->getSource()->getPersonIdentitySource()->getFullPersonNameSource()->getSurnameSource()->getId()); $this->deleteUser(); - $this->assertNull($this->userRepository->find($userId)); - $this->loadedUser = null; } private function deleteUser(): void