Skip to content

Commit

Permalink
Merge pull request #390 from svycka/refactor-config
Browse files Browse the repository at this point in the history
make config ready for zend-expressive and ZF
  • Loading branch information
basz authored Jun 18, 2019
2 parents c8e14d7 + d3d32ae commit 5473608
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ script:
- if [[ $EXECUTE_CS_CHECK == 'true' ]]; then ./vendor/bin/docheader check src/ tests/; fi

after_success:
- if [[ $TEST_COVERAGE == 'true' ]]; then php vendor/bin/coveralls -v; fi
- if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry php vendor/bin/php-coveralls -v; fi

notifications:
webhooks:
Expand Down
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
{
"name": "Jean-Marie Leroux",
"email": "[email protected]"
},
{
"name": "Florent Blaison",
"email": "[email protected]"
},
{
"name": "Bas Kamer",
"email": "[email protected]"
}
],
"require": {
Expand Down Expand Up @@ -52,6 +60,10 @@
"branch-alias": {
"dev-master": "2.4-dev",
"dev-develop": "3.0-dev"
},
"zf": {
"component": "ZfcRbac",
"config-provider": "ZfcRbac\\ConfigProvider"
}
},
"scripts": {
Expand Down
38 changes: 0 additions & 38 deletions config/dependencies.global.php

This file was deleted.

61 changes: 61 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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.
*/

declare(strict_types=1);

namespace ZfcRbac;

/**
* The configuration provider for the ZfcRbac module
*
* @see https://docs.zendframework.com/zend-component-installer/
*/
final class ConfigProvider
{
public function __invoke()
{
return [
'dependencies' => $this->getDependencyConfig(),
'zfc_rbac' => $this->getModuleConfig(),
];
}

public function getDependencyConfig(): array
{
return [
'factories' => [
\ZfcRbac\Assertion\AssertionContainerInterface::class => \ZfcRbac\Container\AssertionContainerFactory::class,
\ZfcRbac\Options\ModuleOptions::class => \ZfcRbac\Container\ModuleOptionsFactory::class,
\ZfcRbac\Role\InMemoryRoleProvider::class => \ZfcRbac\Container\InMemoryRoleProviderFactory::class,
\ZfcRbac\Role\ObjectRepositoryRoleProvider::class => \ZfcRbac\Container\ObjectRepositoryRoleProviderFactory::class,
\ZfcRbac\Service\AuthorizationServiceInterface::class => \ZfcRbac\Container\AuthorizationServiceFactory::class,
\ZfcRbac\Service\RoleServiceInterface::class => \ZfcRbac\Container\RoleServiceFactory::class,
\ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
],
];
}

public function getModuleConfig(): array
{
return [
// Assertion plugin manager
'assertion_manager' => [],
];
}
}
22 changes: 10 additions & 12 deletions src/ModuleConfig.php → src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@

namespace ZfcRbac;

/**
* Class ModuleConfig
*
* @author Florent Blaison <[email protected]>
* @licence MIT
*/
final class ModuleConfig
final class Module
{
public function __invoke(): array
/**
* Return default ZfcRbac configuration for zend-mvc applications.
*/
public function getConfig()
{
$config = [];
$config = array_merge_recursive($config, require __DIR__ . '/../config/config.global.php');
$config = array_merge_recursive($config, require __DIR__ . '/../config/dependencies.global.php');
$provider = new ConfigProvider();

return $config;
return [
'service_manager' => $provider->getDependencyConfig(),
'zfc_rbac' => $provider->getModuleConfig(),
];
}
}
67 changes: 67 additions & 0 deletions test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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.
*/

declare(strict_types=1);

namespace ZfcRbacTest;

use PHPUnit\Framework\TestCase;
use ZfcRbac\ConfigProvider;

/**
* @covers \ZfcRbac\ConfigProvider
*/
class ConfigProviderTest extends TestCase
{
public function testProvidesExpectedConfiguration()
{
$provider = new ConfigProvider();
$expected = [
'factories' => [
\ZfcRbac\Assertion\AssertionContainerInterface::class => \ZfcRbac\Container\AssertionContainerFactory::class,
\ZfcRbac\Options\ModuleOptions::class => \ZfcRbac\Container\ModuleOptionsFactory::class,
\ZfcRbac\Role\InMemoryRoleProvider::class => \ZfcRbac\Container\InMemoryRoleProviderFactory::class,
\ZfcRbac\Role\ObjectRepositoryRoleProvider::class => \ZfcRbac\Container\ObjectRepositoryRoleProviderFactory::class,
\ZfcRbac\Service\AuthorizationServiceInterface::class => \ZfcRbac\Container\AuthorizationServiceFactory::class,
\ZfcRbac\Service\RoleServiceInterface::class => \ZfcRbac\Container\RoleServiceFactory::class,
\ZfcRbac\Rbac::class => \Zend\ServiceManager\Factory\InvokableFactory::class,
],
];
$this->assertEquals($expected, $provider->getDependencyConfig());
}

public function testProvidesExpectedModuleConfiguration()
{
$provider = new ConfigProvider();
$expected = [
'assertion_manager' => [],
];
$this->assertEquals($expected, $provider->getModuleConfig());
}

public function testInvocationProvidesDependencyConfiguration()
{
$provider = new ConfigProvider();
$expected = [
'dependencies' => $provider->getDependencyConfig(),
'zfc_rbac' => $provider->getModuleConfig(),
];
$this->assertEquals($expected, $provider());
}
}
30 changes: 12 additions & 18 deletions test/ModuleConfigTest.php → test/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,22 @@
namespace ZfcRbacTest;

use PHPUnit\Framework\TestCase;
use ZfcRbac\ModuleConfig;
use ZfcRbac\ConfigProvider;
use ZfcRbac\Module;

/**
* @author Bas Kamer <[email protected]>
* @covers \ZfcRbac\ModuleConfig
* @covers \ZfcRbac\Module
*/
class ModuleConfigTest extends TestCase
class ModuleTest extends TestCase
{
public function testCanBeInvoked(): void
public function testProvidesExpectedConfiguration()
{
$moduleConfig = new ModuleConfig();

static::assertTrue(is_callable($moduleConfig));
}

public function testGetArrayWith(): void
{
$moduleConfig = new ModuleConfig();
$config = $moduleConfig->__invoke();

$this->assertInternalType('array', $config);
$this->assertArrayHasKey('zfc_rbac', $config);
$this->assertArrayHasKey('dependencies', $config);
$provider = new ConfigProvider();
$module = new Module();
$expected = [
'service_manager' => $provider->getDependencyConfig(),
'zfc_rbac' => $provider->getModuleConfig(),
];
$this->assertEquals($expected, $module->getConfig());
}
}
27 changes: 27 additions & 0 deletions test/Role/HierarchicalRoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,31 @@ public function testCanGetChildren(): void
$this->assertCount(2, $children);
$this->assertContainsOnlyInstancesOf(HierarchicalRoleInterface::class, $children);
}

/**
* @covers \ZfcRbac\Role\HierarchicalRole::addPermission
*/
public function testRoleCanAddPermission(): void
{
$role = new HierarchicalRole('php');
$role->addPermission('debug');
$this->assertTrue($role->hasPermission('debug'));
$role->addPermission('delete');
$this->assertTrue($role->hasPermission('delete'));
}

/**
* @covers \ZfcRbac\Role\HierarchicalRole::getPermissions
*/
public function testRoleCanGetPermissions(): void
{
$role = new HierarchicalRole('php');
$role->addPermission('foo');
$role->addPermission('bar');
$expectedPermissions = [
'foo' => 'foo',
'bar' => 'bar',
];
$this->assertEquals($expectedPermissions, $role->getPermissions());
}
}

0 comments on commit 5473608

Please sign in to comment.