From 99d68d1422d9f87184d52d6f3daebf9f55f0b9c9 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Mon, 13 Nov 2017 16:08:13 +0100 Subject: [PATCH] added test case for #169 Duplicate initialization of LazyCollection issue #169 --- .../ManyToManyRelationshipTest.php | 29 +++++++ .../Models/ManyToManyRelationship/Group.php | 32 +++++++ .../ManyToManyRelationship/Permission.php | 83 +++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 tests/Integration/Models/ManyToManyRelationship/Permission.php diff --git a/tests/Integration/ManyToManyRelationshipTest.php b/tests/Integration/ManyToManyRelationshipTest.php index b6f0cd3b..166eaf79 100644 --- a/tests/Integration/ManyToManyRelationshipTest.php +++ b/tests/Integration/ManyToManyRelationshipTest.php @@ -12,6 +12,7 @@ namespace GraphAware\Neo4j\OGM\Tests\Integration; use GraphAware\Neo4j\OGM\Tests\Integration\Models\ManyToManyRelationship\Group; +use GraphAware\Neo4j\OGM\Tests\Integration\Models\ManyToManyRelationship\Permission; use GraphAware\Neo4j\OGM\Tests\Integration\Models\ManyToManyRelationship\User; /** @@ -63,6 +64,34 @@ public function testUserCanBeLoadedWithGroups() } } + public function testUserCanBeLoadedWithGroupsNested() + { + $user = new User('jim'); + $group1 = new Group('owners'); + $group2 = new Group('creators'); + $perm1 = new Permission('delete'); + $perm2 = new Permission('create'); + $group1->getPermissions()->add($perm1); + $group2->getPermissions()->add($perm2); + $this->em->persist($group1); + $this->em->persist($group2); + $user->getGroups()->add($group1); + $user->getGroups()->add($group2); + $this->em->persist($user); + $this->em->flush(); + $this->em->clear(); + + /** @var User $jim */ + $jim = $this->em->getRepository(User::class)->findOneBy(['login' => 'jim']); + + $this->assertEquals(2, $jim->getGroups()->count()); + /** @var $permission Permission */ + $permission = $jim->getGroups()->first()->getPermission(); + $this->assertEquals(1, $permission->getGroups()->count()); + $permission->getGroups()->first(); // trigger + $this->assertEquals(1, $permission->getGroups()->count(), 'groups count should not change after accessing groups permissions.'); + } + public function testUserCanHaveGroupAdded() { $user = new User('jim'); diff --git a/tests/Integration/Models/ManyToManyRelationship/Group.php b/tests/Integration/Models/ManyToManyRelationship/Group.php index 5e957613..cae14fd2 100644 --- a/tests/Integration/Models/ManyToManyRelationship/Group.php +++ b/tests/Integration/Models/ManyToManyRelationship/Group.php @@ -42,10 +42,26 @@ class Group */ protected $users; + /** + * @OGM\Relationship(type="HAS_PERMISSION", direction="OUTGOING", mappedBy="groups", collection=true, targetEntity="Permission") + * + * @var Collection|Permission[] + */ + protected $permissions; + + /** + * @OGM\Relationship(type="HAS_PERMISSION", direction="OUTGOING", mappedBy="groups", collection=false, targetEntity="Permission") + * + * @var Permission + */ + protected $permission; + + public function __construct($name) { $this->name = $name; $this->users = new Collection(); + $this->permissions = new Collection(); } /** @@ -72,6 +88,22 @@ public function getUsers() return $this->users; } + /** + * @return Collection|Permission[] + */ + public function getPermissions() + { + return $this->permissions; + } + + /** + * @return Permission + */ + public function getPermission() + { + return $this->permission; + } + /** * @param string $name */ diff --git a/tests/Integration/Models/ManyToManyRelationship/Permission.php b/tests/Integration/Models/ManyToManyRelationship/Permission.php new file mode 100644 index 00000000..8ec5b13a --- /dev/null +++ b/tests/Integration/Models/ManyToManyRelationship/Permission.php @@ -0,0 +1,83 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace GraphAware\Neo4j\OGM\Tests\Integration\Models\ManyToManyRelationship; + +use GraphAware\Neo4j\OGM\Annotations as OGM; +use GraphAware\Neo4j\OGM\Common\Collection; + +/** + * Class Permission. + * + * @OGM\Node(label="Permission") + */ +class Permission +{ + /** + * @OGM\GraphId() + * + * @var int + */ + protected $id; + + /** + * @OGM\Property() + * + * @var string + */ + protected $name; + + /** + * @OGM\Relationship(type="HAS_PERMISSION", direction="INCOMING", mappedBy="permissions", collection=true, targetEntity="Group") + * + * @var Collection|Group[] + */ + protected $groups; + + + public function __construct($name) + { + $this->name = $name; + $this->groups = new Collection(); + } + + /** + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * @return Collection|Group[] + */ + public function getGroups() + { + return $this->groups; + } + + /** + * @param string $name + */ + public function setName($name) + { + $this->name = $name; + } +}