Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

added test case for #169 #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}