From a7c58ba1351fc7b2d1c2b5f01f757886fb461a64 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sat, 17 Nov 2018 14:48:48 +0100 Subject: [PATCH] Implemented tests for UserRepository and mapped data schema --- application/src/Entity/AbstractEntity.php | 1 + application/src/Entity/Meta/Law.php | 1 + application/src/Entity/Meta/Relation.php | 3 ++- .../src/Entity/Source/AbstractSource.php | 2 +- .../Entity/Source/Combination/UserSource.php | 2 +- application/src/Entity/User.php | 3 ++- .../tests/Unit/Entity/AbstractEntityTest.php | 2 +- .../tests/Unit/Entity/Meta/RelationTest.php | 1 + application/tests/Unit/Entity/UserTest.php | 1 + .../Unit/Repository/UserRepositoryTest.php | 17 +++++++++++++---- 10 files changed, 24 insertions(+), 9 deletions(-) diff --git a/application/src/Entity/AbstractEntity.php b/application/src/Entity/AbstractEntity.php index dec8020..2a8c413 100644 --- a/application/src/Entity/AbstractEntity.php +++ b/application/src/Entity/AbstractEntity.php @@ -31,6 +31,7 @@ abstract class AbstractEntity implements EntityInterface public function __construct() { + $this->version = 0; } public function __toString(): string diff --git a/application/src/Entity/Meta/Law.php b/application/src/Entity/Meta/Law.php index 51d919d..24710a6 100644 --- a/application/src/Entity/Meta/Law.php +++ b/application/src/Entity/Meta/Law.php @@ -25,6 +25,7 @@ final class Law extends AbstractMeta implements LawInterface public function __construct() { + parent::__construct(); $this->rights = new ArrayCollection(); } } diff --git a/application/src/Entity/Meta/Relation.php b/application/src/Entity/Meta/Relation.php index 9d50c31..054cd28 100644 --- a/application/src/Entity/Meta/Relation.php +++ b/application/src/Entity/Meta/Relation.php @@ -54,7 +54,7 @@ final class Relation extends AbstractMeta implements RelationInterface /** * @ORM\OneToOne(targetEntity="App\Entity\Source\AbstractSource",cascade={"persist", "remove"}) - * @ORM\JoinColumn(name="source_id", referencedColumnName="id") + * @ORM\JoinColumn(name="source_id", referencedColumnName="id",onDelete="CASCADE") * * @var SourceInterface */ @@ -70,6 +70,7 @@ final class Relation extends AbstractMeta implements RelationInterface public function __construct() { + parent::__construct(); $this->law = new Law(); $this->parents = new ArrayCollection(); $this->childs = new ArrayCollection(); diff --git a/application/src/Entity/Source/AbstractSource.php b/application/src/Entity/Source/AbstractSource.php index cbec1ba..e5ed454 100644 --- a/application/src/Entity/Source/AbstractSource.php +++ b/application/src/Entity/Source/AbstractSource.php @@ -32,7 +32,7 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface /** * @var RelationInterface * @ORM\OneToOne(targetEntity="App\Entity\Meta\Relation",cascade={"persist", "remove"}) - * @ORM\JoinColumn(name="relation_id", referencedColumnName="id") + * @ORM\JoinColumn(name="relation_id", referencedColumnName="id", onDelete="CASCADE") * @Exclude */ protected $relation; diff --git a/application/src/Entity/Source/Combination/UserSource.php b/application/src/Entity/Source/Combination/UserSource.php index 4e02f7e..acba653 100644 --- a/application/src/Entity/Source/Combination/UserSource.php +++ b/application/src/Entity/Source/Combination/UserSource.php @@ -18,7 +18,7 @@ class UserSource extends AbstractCombinationSource implements UserSourceInterfac /** * @ORM\OneToOne(targetEntity="App\Entity\User",cascade={"persist", "remove"}) - * @ORM\JoinColumn(name="user_id", referencedColumnName="id") + * @ORM\JoinColumn(name="user_id", referencedColumnName="id",onDelete="CASCADE") * * @var UserInterface */ diff --git a/application/src/Entity/User.php b/application/src/Entity/User.php index 11ba14b..096e163 100644 --- a/application/src/Entity/User.php +++ b/application/src/Entity/User.php @@ -22,7 +22,7 @@ class User extends BaseUser implements UserInterface /** * @var UserSourceInterface * @ORM\OneToOne(targetEntity="App\Entity\Source\Combination\UserSource",cascade={"persist", "remove"}) - * @ORM\JoinColumn(name="source_user_id", referencedColumnName="id") + * @ORM\JoinColumn(name="source_user_id", referencedColumnName="id", onDelete="CASCADE") */ protected $source; @@ -51,6 +51,7 @@ class User extends BaseUser implements UserInterface public function __construct() { parent::__construct(); + $this->version = 0; $this->isActive = true; $this->source = new UserSource(); $this->source->setUser($this); diff --git a/application/tests/Unit/Entity/AbstractEntityTest.php b/application/tests/Unit/Entity/AbstractEntityTest.php index 9e10942..186b2fe 100644 --- a/application/tests/Unit/Entity/AbstractEntityTest.php +++ b/application/tests/Unit/Entity/AbstractEntityTest.php @@ -21,9 +21,9 @@ class AbstractEntityTest extends TestCase public function testConstructor(): void { + $this->assertEquals(0, $this->entity->getVersion()); $this->expectException(\TypeError::class); $this->entity->getId(); - $this->entity->getVersion(); } public function testVersion(): void diff --git a/application/tests/Unit/Entity/Meta/RelationTest.php b/application/tests/Unit/Entity/Meta/RelationTest.php index cf52f84..e419d48 100644 --- a/application/tests/Unit/Entity/Meta/RelationTest.php +++ b/application/tests/Unit/Entity/Meta/RelationTest.php @@ -23,6 +23,7 @@ class RelationTest extends TestCase { $this->assertInstanceOf(Collection::class, $this->relation->getChilds()); $this->assertInstanceOf(Collection::class, $this->relation->getParents()); + $this->assertEquals(0, $this->relation->getVersion()); $this->expectException(\TypeError::class); $this->relation->getSource(); } diff --git a/application/tests/Unit/Entity/UserTest.php b/application/tests/Unit/Entity/UserTest.php index a9f0d0e..b3a1bfd 100644 --- a/application/tests/Unit/Entity/UserTest.php +++ b/application/tests/Unit/Entity/UserTest.php @@ -32,6 +32,7 @@ class UserTest extends TestCase public function testConstructor(): void { $this->assertInstanceOf(UserInterface::class, new User()); + $this->assertEquals(0, $this->user->getVersion()); } public function testUsername(): void diff --git a/application/tests/Unit/Repository/UserRepositoryTest.php b/application/tests/Unit/Repository/UserRepositoryTest.php index b34f751..75c6df6 100644 --- a/application/tests/Unit/Repository/UserRepositoryTest.php +++ b/application/tests/Unit/Repository/UserRepositoryTest.php @@ -10,7 +10,6 @@ use App\Entity\UserInterface; class UserRepositoryTest extends KernelTestCase { - const USER_ID = 123456789; /** * @var EntityManager */ @@ -28,17 +27,27 @@ class UserRepositoryTest extends KernelTestCase $this->userRepository = $this->entityManager->getRepository(User::class); } + /** + * @todo Test double username + * @todo Test double email + */ public function testCreation(): void { $user = new User(); - $user->setId(self::USER_ID); + $user->setUsername('Karl Marx'); + $user->setEmail('mew21@test.de'); + $user->setPassword('Friedrich ist kein Engel!:)'); $this->entityManager->persist($user); $this->entityManager->flush(); + $userId = $user->getId(); /** * @var UserInterface */ - $loadedUser = $this->userRepository->find(self::USER_ID); - $this->assertEquals(self::USER_ID, $loadedUser->getId()); + $loadedUser = $this->userRepository->find($userId); + $this->assertEquals($userId, $loadedUser->getId()); + $this->entityManager->remove($loadedUser); + $this->entityManager->flush(); + $this->assertNull($this->userRepository->find($userId)); } /**