Skip to content

Commit

Permalink
added test case for graphaware#169
Browse files Browse the repository at this point in the history
Duplicate initialization of LazyCollection

issue graphaware#169
  • Loading branch information
cebe committed Nov 13, 2017
1 parent 0d7bc53 commit 99d68d1
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/Integration/ManyToManyRelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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');
Expand Down
32 changes: 32 additions & 0 deletions tests/Integration/Models/ManyToManyRelationship/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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
*/
Expand Down
83 changes: 83 additions & 0 deletions tests/Integration/Models/ManyToManyRelationship/Permission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the GraphAware Neo4j PHP OGM package.
*
* (c) GraphAware Ltd <info@graphaware.com>
*
* 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;
}
}

0 comments on commit 99d68d1

Please sign in to comment.