Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.github export-ignore
/Tests export-ignore
/phpunit.xml.dist export-ignore
59 changes: 59 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CI

on:
push:
branches:
- master
pull_request:

permissions:
contents: read

jobs:
tests:
name: PHP ${{ matrix.php }}, ${{ matrix.name }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- name: lowest deps
php: '7.4'
flags: --prefer-lowest --prefer-stable
- name: Symfony 3.4
php: '7.4'
pins: symfony/config:3.4.* symfony/dependency-injection:3.4.* symfony/http-kernel:3.4.* symfony/security-core:3.4.* symfony/yaml:3.4.*
- name: Symfony 4.4
php: '7.4'
pins: symfony/config:4.4.* symfony/dependency-injection:4.4.* symfony/http-kernel:4.4.* symfony/security-core:4.4.* symfony/yaml:4.4.*
- name: Symfony 4.4
php: '8.1'
pins: symfony/config:4.4.* symfony/dependency-injection:4.4.* symfony/http-kernel:4.4.* symfony/security-core:4.4.* symfony/yaml:4.4.*
- name: Symfony 5.4
php: '8.1'
pins: symfony/config:5.4.* symfony/dependency-injection:5.4.* symfony/http-kernel:5.4.* symfony/security-core:5.4.* symfony/yaml:5.4.*
- name: Symfony 6.4
php: '8.2'
pins: symfony/config:6.4.* symfony/dependency-injection:6.4.* symfony/http-kernel:6.4.* symfony/security-core:6.4.* symfony/yaml:6.4.*
- name: latest deps
php: '8.3'
env:
PINS: ${{ matrix.pins }}
COMPOSER_NO_SECURITY_BLOCKING: 1
steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none

- name: Install dependencies
run: |
if [ -n "$PINS" ]; then composer require --no-update --no-interaction $PINS; fi
composer update --no-interaction --no-progress ${{ matrix.flags }}
composer show | grep -E '^(docplanner/gatekeeper|symfony/(config|dependency-injection|http-kernel|security|security-core|yaml)) '

- name: PHPUnit
run: vendor/bin/phpunit
97 changes: 97 additions & 0 deletions Tests/GateKeeperBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace GateKeeperBundle\Tests;

use GateKeeper\GateKeeper;
use GateKeeper\Model\ModelInterface;
use GateKeeper\Repository\RepositoryInterface;
use GateKeeperBundle\GateKeeperBundle;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;

class GateKeeperBundleTest extends TestCase
{
/** @var ContainerBuilder */
private $container;

protected function setUp(): void
{
$bundle = new GateKeeperBundle();
$this->container = new ContainerBuilder();
$this->container->registerExtension($bundle->getContainerExtension());
$this->container->loadFromExtension('gate_keeper', ['repository_service' => 'test.repository']);
$this->container->register('test.repository', InMemoryRepository::class);
$bundle->build($this->container);
$this->container->addCompilerPass(new PublicVoterPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
$this->container->compile();
}

public function testContainerDumps(): void
{
$this->assertStringContainsString('class GateKeeperTestContainer', (new PhpDumper($this->container))->dump(['class' => 'GateKeeperTestContainer']));
}

public function testGateKeeperServiceIsPublic(): void
{
$gateKeeper = $this->container->get(GateKeeper::class);

$this->assertInstanceOf(GateKeeper::class, $gateKeeper);
$this->assertTrue($gateKeeper->hasAccess('GATE_ON'));
$this->assertFalse($gateKeeper->hasAccess('GATE_OFF'));
$this->assertFalse($gateKeeper->hasAccess('GATE_MISSING'));
}

public function testVoter(): void
{
$this->assertArrayHasKey('gatekeeper.voter', $this->container->findTaggedServiceIds('security.voter'));

$voter = $this->container->get('gatekeeper.voter');
$token = new TestToken();

$this->assertSame(VoterInterface::ACCESS_GRANTED, $voter->vote($token, null, ['GATE_ON']));
$this->assertSame(VoterInterface::ACCESS_DENIED, $voter->vote($token, null, ['GATE_OFF']));
$this->assertSame(VoterInterface::ACCESS_ABSTAIN, $voter->vote($token, null, ['ROLE_ADMIN']));
}
}

class InMemoryRepository implements RepositoryInterface
{
public function save(ModelInterface $gateKeeperModel) {}
public function update(ModelInterface $gateKeeperModel) {}
public function delete(ModelInterface $gateKeeperModel) {}

public function get($name)
{
$accesses = ['GATE_ON' => 'allow-all', 'GATE_OFF' => 'deny-all'];

return isset($accesses[$name]) ? new Model($accesses[$name]) : null;
}
}

class Model implements ModelInterface
{
private $access;

public function __construct($access) { $this->access = $access; }
public function getAccess() { return $this->access; }
public function getGate() { return ''; }
}

class TestToken extends AbstractToken
{
public function __construct() { parent::__construct([]); }
public function getCredentials() { return ''; }
}

class PublicVoterPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container->getDefinition('gatekeeper.voter')->setPublic(true);
}
}
19 changes: 13 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,26 @@
"require": {
"php": "^5.4 || ^7.4 || ^8.0",
"docplanner/gatekeeper": "2.*",
"symfony/dependency-injection": "^2.8 || ^3.0 || ^4.0",
"symfony/http-kernel": "^2.8 || ^3.0 || ^4.0",
"symfony/config": "^2.8 || ^3.0 || ^4.0",
"symfony/security": "^2.8 || ^3.0 || ^4.0"
"symfony/dependency-injection": "^2.8 || ^3.0 || ^4.0 || ^5.4 || ^6.4",
"symfony/http-kernel": "^2.8 || ^3.0 || ^4.0 || ^5.4 || ^6.4",
"symfony/config": "^2.8 || ^3.0 || ^4.0 || ^5.4 || ^6.4",
"symfony/security-core": "^2.8 || ^3.0 || ^4.0 || ^5.4 || ^6.4",
"symfony/yaml": "^2.8 || ^3.0 || ^4.0 || ^5.4 || ^6.4"
},
"autoload": {
"psr-4": {
"GateKeeperBundle\\": ""
}
},
"exclude-from-classmap": [
"/Tests/"
]
},
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
"dev-master": "3.1.x-dev"
}
},
"require-dev": {
"phpunit/phpunit": "^9.6"
}
}
12 changes: 12 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
failOnWarning="true">
<testsuites>
<testsuite name="GateKeeperBundle">
<directory>Tests</directory>
</testsuite>
</testsuites>
</phpunit>
Loading