Skip to content

Commit

Permalink
Merge pull request #8 from zf-fr/factory
Browse files Browse the repository at this point in the history
Add factory
  • Loading branch information
bakura10 committed Jan 13, 2014
2 parents e81ce2b + 3d04183 commit c349084
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 29 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
"php": ">=5.4"
},
"require-dev": {
"zendframework/zend-servicemanager": "~2.2",
"phpunit/phpunit": "~3.7",
"squizlabs/php_codesniffer": "1.4.*",
"satooshi/php-coveralls": "~0.6"
},
"suggest": {
"zendframework/zend-servicemanager": "If you want to use RBAC factory"
},
"autoload": {
"psr-0": {
"Rbac\\": "src/"
Expand Down
46 changes: 46 additions & 0 deletions src/Rbac/Factory/RbacFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace Rbac\Factory;

use Rbac\Rbac;
use Rbac\Traversal\Strategy\GeneratorStrategy;
use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* @author Michaël Gallego <[email protected]>
* @licence MIT
*/
class RbacFactory implements FactoryInterface
{
/**
* {@inheritDoc}
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
$traversalStrategy = new GeneratorStrategy();
} else {
$traversalStrategy = new RecursiveRoleIteratorStrategy();
}

return new Rbac($traversalStrategy);
}
}
12 changes: 3 additions & 9 deletions src/Rbac/Rbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@ class Rbac
protected $traversalStrategy;

/**
* @param null|TraversalStrategyInterface $strategy
* @param TraversalStrategyInterface $strategy
*/
public function __construct(TraversalStrategyInterface $strategy = null)
public function __construct(TraversalStrategyInterface $strategy)
{
if (null !== $strategy) {
$this->traversalStrategy = $strategy;
} elseif (version_compare(PHP_VERSION, '5.5.0', '>=')) {
$this->traversalStrategy = new GeneratorStrategy();
} else {
$this->traversalStrategy = new RecursiveRoleIteratorStrategy();
}
$this->traversalStrategy = $strategy;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/RbacTest/Factory/RbacFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace RbacTest\Factory;
use Rbac\Factory\RbacFactory;
use Zend\ServiceManager\ServiceManager;

/**
* @covers Rbac\Factory\RbacFactory
* @group Coverage
*/
class RbacFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCanCreateFromFactory()
{
$serviceManager = new ServiceManager();
$factory = new RbacFactory();

$rbac = $factory->createService($serviceManager);

$this->assertInstanceOf('Rbac\Rbac', $rbac);
$this->assertInstanceOf('Rbac\Traversal\Strategy\TraversalStrategyInterface', $rbac->getTraversalStrategy());
}
}
25 changes: 5 additions & 20 deletions tests/RbacTest/RbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPUnit_Framework_TestCase as TestCase;
use Rbac\Rbac;
use Rbac\Role\Role;
use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy;

/**
* @covers Rbac\Rbac
Expand All @@ -31,28 +32,12 @@ public function testConstructorAcceptCustomTraversalStrategy()
$this->assertAttributeSame($customStrategy, 'traversalStrategy', $rbac);
}

/**
* @covers Rbac\Rbac::__construct
*/
public function testConstructorWillSetDefaultTraversalStrategyIfNoneIsSpecified()
{
$rbac = new Rbac;

if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
$default = 'Rbac\Traversal\Strategy\GeneratorStrategy';
} else {
$default = 'Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy';
}

$this->assertAttributeInstanceOf($default, 'traversalStrategy', $rbac);
}

/**
* @covers Rbac\Rbac::isGranted
*/
public function testCastPermissionToString()
{
$rbac = new Rbac;
$rbac = new Rbac(new RecursiveRoleIteratorStrategy());

$permission = $this->getMock('Rbac\Permission\PermissionInterface');
$permission->expects($this->once())->method('__toString')->will($this->returnValue('permission'));
Expand Down Expand Up @@ -105,7 +90,7 @@ public function testTraverseRoles()
->will($this->returnValue(false));

$roles = [$role, $role, $role];
$rbac = new Rbac;
$rbac = new Rbac(new RecursiveRoleIteratorStrategy());

$rbac->isGranted($roles, 'permission');
}
Expand All @@ -125,15 +110,15 @@ public function testReturnTrueWhenRoleHasPermission()
$nextRole->expects($this->never())->method('hasPermission');

$roles = [$grantedRole, $nextRole];
$rbac = new Rbac;
$rbac = new Rbac(new RecursiveRoleIteratorStrategy());

$this->assertTrue($rbac->isGranted($roles, 'permission'));
}

public function testReturnFalseIfNoRoleHasPermission()
{
$roles = [new Role('Foo'), new Role('Bar')];
$rbac = new Rbac;
$rbac = new Rbac(new RecursiveRoleIteratorStrategy());

$this->assertFalse($rbac->isGranted($roles, 'permission'));
}
Expand Down

0 comments on commit c349084

Please sign in to comment.