Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leodido committed Oct 28, 2014
1 parent eb75040 commit 3dbb82d
Show file tree
Hide file tree
Showing 31 changed files with 327 additions and 336 deletions.
4 changes: 2 additions & 2 deletions library/Assertion/AssertionPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
namespace AclMan\Assertion;

use AlcMan\Exception\InvalidAssertException;
use AclMan\Exception\InvalidAssertException;
use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Zend\ServiceManager\AbstractPluginManager;

Expand All @@ -33,7 +33,7 @@ public function validatePlugin($plugin)
}

throw new InvalidAssertException(sprintf(
'Plugin of type %s is invalid; must implement Zend\Permissions\Acl\Assertion\AssertionInterface',
'Plugin of type "%s" is invalid; must implement Zend\Permissions\Acl\Assertion\AssertionInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
}
Expand Down
2 changes: 1 addition & 1 deletion library/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @copyright Copyright (c) 2014, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AlcMan\Exception;
namespace AclMan\Exception;

/**
* Interface ExceptionInterface
Expand Down
4 changes: 2 additions & 2 deletions library/Exception/InvalidAssertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* @copyright Copyright (c) 2014, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AlcMan\Exception;
namespace AclMan\Exception;

/**
* Class InvalidAssertException
*/
class InvalidAssertException extends \Exception implements ExceptionInterface
class InvalidAssertException extends \RuntimeException implements ExceptionInterface
{
}
2 changes: 0 additions & 2 deletions library/Exception/InvalidParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
namespace AclMan\Exception;

use AlcMan\Exception\ExceptionInterface;

/**
* Class InvalidParameterException
*/
Expand Down
4 changes: 1 addition & 3 deletions library/Exception/ResourceAlreadyExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
* @copyright Copyright (c) 2014, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace AclMan\Storage\Exception;

use AlcMan\Exception\ExceptionInterface;
namespace AclMan\Exception;

/**
* Class ResourceAlreadyExistException
Expand Down
2 changes: 0 additions & 2 deletions library/Exception/ResourceNotExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
namespace AclMan\Exception;

use AlcMan\Exception\ExceptionInterface;

/**
* Class ResourceNotExistException
*/
Expand Down
2 changes: 0 additions & 2 deletions library/Exception/RoleAlreadyExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
namespace AclMan\Exception;

use AlcMan\Exception\ExceptionInterface;

/**
* Class RoleAlreadyExistException
*/
Expand Down
2 changes: 0 additions & 2 deletions library/Exception/RoleNotExistException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
namespace AclMan\Exception;

use AlcMan\Exception\ExceptionInterface;

/**
* Class RoleNotExistException
*/
Expand Down
3 changes: 1 addition & 2 deletions library/Exception/ServiceNotCreatedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
* @copyright Copyright (c) 2014, RipaClub
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/
namespace Exception;
namespace AclMan\Exception;

use AlcMan\Exception\ExceptionInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException as ZendServiceNotCreatedException;

/**
Expand Down
36 changes: 16 additions & 20 deletions library/Permission/GenericPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,22 @@ class GenericPermission implements PermissionInterface
*/
public function __construct($options = null)
{
switch(true) {
case isset($options['role']):
$role = $this->checkRole($options['role']);
$this->roleId = $role->getRoleId();
break;
case isset($options['resource']):
$resource = $this->checkResource($options['resource']);
$this->resourceId = $resource->getResourceId();
break;
case isset($options['privilege']):
$this->setPrivilege($options['privilege']);
break;
case isset($options['allow']):
$this->allow = (bool) $options['allow'];
break;
case isset($options['assert']):
$this->setAssertion($options['assert']);
break;
default:
break;
if (isset($options['role'])) {
$role = $this->checkRole($options['role']);
$this->roleId = $role->getRoleId();
}
if (isset($options['resource'])) {
$resource = $this->checkResource($options['resource']);
$this->resourceId = $resource->getResourceId();
}
if (isset($options['privilege'])) {
$this->setPrivilege($options['privilege']);
}
if (isset($options['allow'])) {
$this->allow = (bool) $options['allow'];
}
if (isset($options['assert'])) {
$this->setAssertion($options['assert']);
}
}

Expand Down
4 changes: 2 additions & 2 deletions library/Service/ServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace AclMan\Service;

use AclMan\Storage\StorageInterface;
use Exception\ServiceNotCreatedException;
use AclMan\Exception\ServiceNotCreatedException;
use Zend\Permissions\Acl\Acl;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand Down Expand Up @@ -86,7 +86,7 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $
$storage = $serviceLocator->get($config['storage']);
if (!$storage instanceof StorageInterface) {
throw new ServiceNotCreatedException(sprintf(
'"%s" expectes a AclMan\Storage\StorageInterface; received "%s"',
'"%s" expectes a AclMan\Storage\StorageInterface is set in the config; received "%s"',
__METHOD__,
is_object($storage) ? get_class($storage) : gettype($storage)
));
Expand Down
14 changes: 9 additions & 5 deletions library/Storage/Adapter/ArrayAdapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use AclMan\Permission\PermissionInterface;
use AclMan\Resource\ResourceCheckTrait;
use AclMan\Role\RoleCheckTrait;
use AclMan\Storage\Exception\ResourceAlreadyExistException;
use AclMan\Exception\ResourceAlreadyExistException;
use AclMan\Storage\StorageInterface;
use Zend\Permissions\Acl\Resource\GenericResource;
use Zend\Permissions\Acl\Resource\ResourceInterface;
Expand Down Expand Up @@ -267,23 +267,27 @@ public function getPermissions($role = null, $resource = null)

if ($resource) {
$resource = $this->checkResource($resource);
$listPermission = $this->permission[$role->getRoleId()][self::NODE_RESOURCES][$resource->getResourceId()];
if (isset($listPermission[self::NODE_PERMISSION])) {
$listPermission = $listPermission[self::NODE_PERMISSION];
if(isset($this->permission[$role->getRoleId()][self::NODE_RESOURCES][$resource->getResourceId()][self::NODE_PERMISSION])) {
$listPermission = $this->permission[$role->getRoleId()][self::NODE_RESOURCES][$resource->getResourceId()][self::NODE_PERMISSION];
foreach ($listPermission as $permission) {

$permission['role'] = $role->getRoleId();
$permission['resource'] = $resource->getResourceId();

$obj = new GenericPermission($permission);
array_push($result, $obj);
}

}
} else {
if (isset($this->permission[$role->getRoleId()][self::NODE_RESOURCES])) {
if(isset($this->permission[$role->getRoleId()][self::NODE_RESOURCES])) {
$listResource = $this->permission[$role->getRoleId()][self::NODE_RESOURCES];
foreach ($listResource as $keyResource => $listPermission) {
foreach ($listPermission[self::NODE_PERMISSION] as $permission) {

$permission['role'] = $role->getRoleId();
$permission['resource'] = ($keyResource != self::ALL_RESOURCES) ? $keyResource : null;

$obj = new GenericPermission($permission);
array_push($result, $obj);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Assertion/AssertionAwareTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

use AclManTest\AclManTestCase;

/**
* Class AssertionAwareTraitTest
*/
class AssertionAwareTraitTest extends AclManTestCase
{
/**
Expand Down
11 changes: 6 additions & 5 deletions tests/Assertion/AssertionManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace AclManTest\Assertion;

use AclManTest\AclManTestCase;
use AclManTest\Assertion\TestAsset\MockAssertionPluginManager;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager;

Expand All @@ -24,11 +25,11 @@ class AssertionManagerFactoryTest extends AclManTestCase

public function setUp()
{
$config = array(
$config = [
'factories' => [
'assertManager' => 'AclMan\Assertion\AssertionManagerFactory',
]
);
];

$sm = $this->serviceManager = new ServiceManager\ServiceManager(
new ServiceManagerConfig($config)
Expand All @@ -45,22 +46,22 @@ public function testAssertPluginManager()

public function testAssetPluginManagerCustom()
{
$config = array(
$config = [
'factories' => [
'assertManager' => 'AclMan\Assertion\AssertionManagerFactory',
],
'invokables' => [
'AclMan\Plugin\Manager' => 'AclManTest\Assertion\TestAsset\MockAssertionPluginManager'
]
);
];

$sm = new ServiceManager\ServiceManager(
new ServiceManagerConfig($config)
);

$sm->setService('Config', $config);

/** @var $pluginManager \AclManTest\Assert\TestAsset\MockAssertionPluginManager */
/** @var $pluginManager MockAssertionPluginManager */
$pluginManager = $sm->get('assertManager');
$this->assertInstanceOf('AclManTest\Assertion\TestAsset\MockAssertionPluginManager', $pluginManager);
}
Expand Down
10 changes: 8 additions & 2 deletions tests/Assertion/AssertionPluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use AclMan\Assertion\AssertionPluginManager;
use AclManTest\AclManTestCase;

/**
* Class AssertionPluginManagerTest
*/
class AssertionPluginManagerTest extends AclManTestCase
{
protected $manager;
Expand All @@ -24,11 +27,14 @@ public function setUp()

public function testAssertionPluginManager()
{
$this->assertInstanceOf('AclManTest\Assertion\TestAsset\Assertion\MockAssertion1', $this->manager->get('assert1'));
$this->assertInstanceOf(
'AclManTest\Assertion\TestAsset\Assertion\MockAssertion1',
$this->manager->get('assert1')
);
}

/**
* @expectedException \AclMan\Assertion\Exception\InvalidAssertException
* @expectedException \AclMan\Exception\InvalidAssertException
*/
public function testAssertionPluginManagerException()
{
Expand Down
26 changes: 4 additions & 22 deletions tests/Assertion/TestAsset/Assertion/MockAssertion1.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,16 @@
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

/**
* Class MockAssertion1
*/
class MockAssertion1 implements AssertionInterface, ServiceLocatorAwareInterface
{
/**
* TRAIT
******************************************************************************************************************/

use ServiceLocatorAwareTrait;

/**
* METHOD
******************************************************************************************************************/

/**
* Returns true if and only if the assertion conditions are met
*
* This method is passed the ACL, Role, Resource, and privilege to which the authorization query applies. If the
* $role, $resource, or $privilege parameters are null, it means that the query applies to all Roles, Resources, or
* privileges, respectively.
*
* @param Acl $acl
* @param RoleInterface $role
* @param ResourceInterface $resource
* @param string $privilege
* @return bool
*/
public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
{
// TODO: Implement assert() method.
// TODO: implement assert() method.
}

}
5 changes: 3 additions & 2 deletions tests/Assertion/TestAsset/Assertion/MockWrongAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
*/
namespace AclManTest\Assertion\TestAsset\Assertion;


/**
* Class MockWrongAssertion
*/
class MockWrongAssertion
{

}
18 changes: 6 additions & 12 deletions tests/Assertion/TestAsset/MockAssertionPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
*/
namespace AclManTest\Assertion\TestAsset;

use AclMan\Exception\InvalidAssertException;
use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Zend\ServiceManager\AbstractPluginManager;

/**
* Class MockAssertionPluginManager
*/
class MockAssertionPluginManager extends AbstractPluginManager
{
/**
Expand All @@ -23,25 +27,15 @@ class MockAssertionPluginManager extends AbstractPluginManager
];

/**
* METHOD
******************************************************************************************************************/

/**
* Validate the plugin
*
* Checks that the filter loaded is either a valid callback or an instance
* of FilterInterface.
*
* @param mixed $plugin
* @throws \Zend\ServiceManager\Exception\InvalidAssertException
* {@inheritdoc}
*/
public function validatePlugin($plugin)
{
if ($plugin instanceof AssertionInterface) {
return;
}

throw new \Exception(sprintf(
throw new InvalidAssertException(sprintf(
'Plugin of type %s is invalid; must implement Zend\Permissions\Acl\Assertion\AssertionInterface',
(is_object($plugin) ? get_class($plugin) : gettype($plugin))
));
Expand Down
Loading

0 comments on commit 3dbb82d

Please sign in to comment.