Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Apr 1, 2014
1 parent 1b9ad4f commit 52f9a2c
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
99 changes: 99 additions & 0 deletions tests/Unit/Repository/AuthorizationRepositoryTest.php
@@ -0,0 +1,99 @@
<?php

namespace Tests\MyCLabs\ACL\Unit\Repository;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use MyCLabs\ACL\ACLManager;
use MyCLabs\ACL\Doctrine\ACLSetup;
use MyCLabs\ACL\Model\Actions;
use MyCLabs\ACL\Model\Authorization;
use MyCLabs\ACL\Repository\AuthorizationRepository;
use Tests\MyCLabs\ACL\Unit\Repository\Model\File;
use Tests\MyCLabs\ACL\Unit\Repository\Model\FileOwnerRole;
use Tests\MyCLabs\ACL\Unit\Repository\Model\User;

/**
* @covers \MyCLabs\ACL\Repository\AuthorizationRepository
*/
class AuthorizationRepositoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var EntityManager
*/
private $em;

/**
* @var ACLManager
*/
private $aclManager;

public function setUp()
{
$paths = [
__DIR__ . '/../../../src/Model',
__DIR__ . '/Model',
];
$dbParams = [
'driver' => 'pdo_sqlite',
'memory' => true,
];

$setup = new ACLSetup();
$setup->setSecurityIdentityClass('Tests\MyCLabs\ACL\Unit\Repository\Model\User');
$setup->registerRoleClass('Tests\MyCLabs\ACL\Unit\Repository\Model\FileOwnerRole', 'fileOwner');

// Create the entity manager
$config = Setup::createAnnotationMetadataConfiguration($paths, true, null, new ArrayCache(), false);
$this->em = EntityManager::create($dbParams, $config);

$this->aclManager = new ACLManager($this->em);

$setup->setUpEntityManager($this->em, function () {
return $this->aclManager;
});

// Create the DB
$tool = new SchemaTool($this->em);
$tool->createSchema($this->em->getMetadataFactory()->getAllMetadata());
}

public function testInsertBulk()
{
$user = new User();
$this->em->persist($user);
$resource = new File();
$this->em->persist($resource);
$role = new FileOwnerRole($user, $resource);
$this->em->persist($role);

$this->em->flush();

$authorizations = [
Authorization::create($role, Actions::all(), $resource),
];

/** @var AuthorizationRepository $repository */
$repository = $this->em->getRepository('MyCLabs\ACL\Model\Authorization');

$repository->insertBulk($authorizations);

// Check that the authorization was inserted and can be retrieved
$inserted = $repository->findAll();

$this->assertCount(1, $inserted);

/** @var Authorization $authorization */
$authorization = $inserted[0];
$this->assertSame($role, $authorization->getRole());
$this->assertSame($user, $authorization->getSecurityIdentity());
$this->assertEquals($resource->getId(), $authorization->getEntityId());
$this->assertEquals('Tests\MyCLabs\ACL\Unit\Repository\Model\File', $authorization->getEntityClass());
$this->assertEquals(Actions::all(), $authorization->getActions());
$this->assertNull($authorization->getParentAuthorization());
$this->assertEquals(0, count($authorization->getChildAuthorizations()));
$this->assertTrue($authorization->isCascadable());
}
}
23 changes: 23 additions & 0 deletions tests/Unit/Repository/Model/File.php
@@ -0,0 +1,23 @@
<?php

namespace Tests\MyCLabs\ACL\Unit\Repository\Model;

use Doctrine\ORM\Mapping as ORM;
use MyCLabs\ACL\Model\EntityResource;

/**
* @ORM\Entity
*/
class File implements EntityResource
{
/**
* @ORM\Id @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;

public function getId()
{
return $this->id;
}
}
32 changes: 32 additions & 0 deletions tests/Unit/Repository/Model/FileOwnerRole.php
@@ -0,0 +1,32 @@
<?php

namespace Tests\MyCLabs\ACL\Unit\Repository\Model;

use Doctrine\ORM\Mapping as ORM;
use MyCLabs\ACL\ACLManager;
use MyCLabs\ACL\Model\Actions;
use MyCLabs\ACL\Model\Role;

/**
* @ORM\Entity(readOnly=true)
*/
class FileOwnerRole extends Role
{
/**
* @var File
* @ORM\ManyToOne(targetEntity="File", inversedBy="roles")
*/
protected $file;

public function __construct(User $identity, File $file)
{
$this->file = $file;

parent::__construct($identity);
}

public function createAuthorizations(ACLManager $aclManager)
{
$aclManager->allow($this, new Actions([Actions::VIEW, Actions::EDIT]), $this->file);
}
}
40 changes: 40 additions & 0 deletions tests/Unit/Repository/Model/User.php
@@ -0,0 +1,40 @@
<?php

namespace Tests\MyCLabs\ACL\Unit\Repository\Model;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use MyCLabs\ACL\Model\SecurityIdentityInterface;
use MyCLabs\ACL\Model\SecurityIdentityTrait;

/**
* @ORM\Entity
*/
class User implements SecurityIdentityInterface
{
use SecurityIdentityTrait;

/**
* @ORM\Id @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;

/**
* @var FileOwnerRole[]|Collection
* @ORM\OneToMany(targetEntity="FileOwnerRole", mappedBy="securityIdentity",
* cascade={"persist", "remove"}, orphanRemoval=true)
*/
protected $roles;

public function __construct()
{
$this->roles = new ArrayCollection();
}

public function getId()
{
return $this->id;
}
}

0 comments on commit 52f9a2c

Please sign in to comment.