-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
3,999 additions
and
912 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
name: CI | ||
|
||
on: [push, pull_request] | ||
|
||
env: | ||
PHPUNIT_FLAGS: "-v" | ||
|
||
jobs: | ||
test: | ||
name: ProgressiveBundle (PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}) | ||
runs-on: ${{ matrix.operating-system }} | ||
|
||
strategy: | ||
matrix: | ||
operating-system: ['ubuntu-latest', 'windows-latest', 'macos-latest'] | ||
php-versions: ['7.3', '7.4', '8.0'] | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php-versions }} | ||
tools: composer:v2 | ||
|
||
- name: Validate composer.json and composer.lock | ||
run: composer validate | ||
|
||
- name: "Set composer cache directory" | ||
id: composer-cache | ||
run: echo "::set-output name=dir::$(composer config cache-files-dir)" | ||
|
||
- name: "Cache composer" | ||
uses: actions/cache@v2 | ||
with: | ||
path: ${{ steps.composer-cache.outputs.dir }} | ||
key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: ${{ runner.os }}-${{ matrix.php-version }}-composer- | ||
|
||
- name: Setup problem matchers for PHPUnit | ||
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" | ||
|
||
- name: Install dependencies | ||
run: composer install --prefer-dist --optimize-autoloader | ||
|
||
- name: "Run tests" | ||
run: vendor/bin/phpunit ${{ env.PHPUNIT_FLAGS }} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: Lint | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
php-cs-fixer: | ||
name: PHP-CS-Fixer | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: "Checkout code" | ||
uses: actions/checkout@v2 | ||
|
||
- name: PHP-CS-Fixer | ||
uses: docker://oskarstark/php-cs-fixer-ga | ||
with: | ||
args: --diff --dry-run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
/vendor/ | ||
.php_cs.cache | ||
.phpunit.xml | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Af\ProgressiveBundle\DependencyInjection\Compiler; | ||
|
||
use Progressive\Rule\Store; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class RulePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->has(Store::class)) { | ||
return; | ||
} | ||
|
||
$definition = $container->findDefinition(Store::class); | ||
|
||
$taggedServices = $container->findTaggedServiceIds('af_progressive.rule'); | ||
|
||
foreach ($taggedServices as $id => $tags) { | ||
$definition->addMethodCall('add', [new Reference($id)]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Af\ProgressiveBundle\Rule; | ||
|
||
use Progressive\ParameterBagInterface; | ||
use Progressive\Rule\RuleInterface; | ||
|
||
class Environments implements RuleInterface | ||
{ | ||
/** @var string */ | ||
private $env; | ||
|
||
public function __construct($env) | ||
{ | ||
$this->env = $env; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function decide(ParameterBagInterface $bag, array $envs = []): bool | ||
{ | ||
return in_array($this->env, $envs); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName(): string | ||
{ | ||
return 'env'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Af\ProgressiveBundle\Rule; | ||
|
||
use Progressive\ParameterBagInterface; | ||
use Progressive\Rule\RuleInterface; | ||
use Symfony\Component\Security\Core\Security; | ||
|
||
class Roles implements RuleInterface | ||
{ | ||
/** @var Security */ | ||
private $security; | ||
|
||
public function __construct(Security $security) | ||
{ | ||
$this->security = $security; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function decide(ParameterBagInterface $bag, array $roles = []): bool | ||
{ | ||
if (null === $user = $this->security->getUser()) { | ||
return false; | ||
} | ||
|
||
$userRoles = $user->getRoles(); | ||
foreach ($userRoles as $role) { | ||
if (in_array($role, $roles, true)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName(): string | ||
{ | ||
return 'roles'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Af\ProgressiveBundle\Tests\Rule; | ||
|
||
use Af\ProgressiveBundle\Rule\Environments; | ||
use PHPUnit\Framework\TestCase; | ||
use Progressive\ParameterBagInterface; | ||
|
||
class EnvironmentsTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider valueProvider | ||
*/ | ||
public function testDecide(string $env, array $envs, bool $expected) | ||
{ | ||
$context = $this->createMock(ParameterBagInterface::class); | ||
|
||
$rule = new Environments($env); | ||
$response = $rule->decide($context, $envs); | ||
|
||
$this->assertSame($response, $expected); | ||
} | ||
|
||
public function valueProvider(): array | ||
{ | ||
return [ | ||
['dev', ['dev', 'test', 'prod'], true], | ||
['dev', ['preprod', 'dev'], true], | ||
['test', ['test'], true], | ||
['test', [], false], | ||
['prod', ['dev', 'test'], false], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Af\ProgressiveBundle\Tests\Rule; | ||
|
||
use Af\ProgressiveBundle\Rule\Roles; | ||
use PHPUnit\Framework\TestCase; | ||
use Progressive\ParameterBagInterface; | ||
use Symfony\Component\Security\Core\Security; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class RolesTest extends TestCase | ||
{ | ||
public function testNotAuthenticated() | ||
{ | ||
$security = $this->createMock(Security::class); | ||
$context = $this->createMock(ParameterBagInterface::class); | ||
|
||
$rule = new Roles($security); | ||
$this->assertFalse($rule->decide($context, [])); | ||
} | ||
|
||
/** | ||
* @dataProvider rolesProvider | ||
*/ | ||
public function testAuthenticatedUser(array $roles, bool $expected) | ||
{ | ||
$user = $this->createMock(UserInterface::class); | ||
$user->expects($this->once()) | ||
->method('getRoles') | ||
->willReturn(['ROLE_DEV', 'ROLE_ADMIN']) | ||
; | ||
|
||
$security = $this->createMock(Security::class); | ||
$security->expects($this->once()) | ||
->method('getUser') | ||
->willReturn($user) | ||
; | ||
$context = $this->createMock(ParameterBagInterface::class); | ||
|
||
$rule = new Roles($security); | ||
$response = $rule->decide($context, $roles); | ||
|
||
$this->assertSame($response, $expected); | ||
} | ||
|
||
public function rolesProvider(): array | ||
{ | ||
return [ | ||
[[], false], | ||
[['ROLE_SUPERADMIN', 'ROLE_TRANSLATOR'], false], | ||
[['ROLE_ADMIN', 'ROLE_DEV', 'ROLE_SUPERADMIN'], true], | ||
[['ROLE_DEV', 'ROLE_ADMIN'], true], | ||
[['ROLE_ADMIN'], true], | ||
[['ROLE_DEV'], true], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.