Skip to content

Commit

Permalink
Fixed PHPUnit deprecations. Added GH actions for CI
Browse files Browse the repository at this point in the history
  • Loading branch information
visto9259 committed Aug 8, 2024
1 parent 1b793a8 commit 1dbb77c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 84 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Continuous Integration

on:
push:
# Avoid running tests on changes to documentation
paths-ignore:
- 'docs/**'
pull_request:
paths-ignore:
- 'docs/**'

env:
COMPOSER_ARGS: '--no-progress'

jobs:
build:
strategy:
matrix:
php_version: ['8.1', '8.2', '8.3']
deps: ['--prefer-lowest --prefer-dist', '']
include:
- code-coverage: 'yes'
php_version: '8.3'
deps: ''
runs-on: ubuntu-latest

steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{matrix.php_version}}

- name: Show PHP version
run: php -v

- uses: actions/checkout@v4
name: Checkout branch

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php_version }}-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php--${{matrix.php_version}}-
- name: Install/update dependencies
run: composer update ${{matrix.deps}} $COMPOSER_ARGS

- name: Run PHPUnit test suite
if: ${{ matrix.code-coverage != 'yes' }}
run: composer run-script test

- name: Run PHPUnit test suite with coverage
if: ${{ matrix.code-coverage == 'yes' }}
run: composer run-script test-coverage

- name: Upload coverage results to Coverall
if: ${{ matrix.code-coverage == 'yes' }}
uses: coverallsapp/github-action@v2


4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"@cs",
"@test"
],
"cs": "php-cs-fixer fix -v --diff --dry-run",
"cs-fix": "php-cs-fixer fix -v --diff",
"cs": "phpcs -n --standard=PSR2 ./src/",
"cs-fix": "phpcbf ./src/",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"test-coverage-html": "phpunit --colors=always --coverage-html ./build/html"
Expand Down
27 changes: 5 additions & 22 deletions tests/RbacTest/RbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@
namespace RbacTest;

use ArrayIterator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Rbac\Rbac;
use Rbac\Role\Role;
use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy;

/**
* @covers \Rbac\Rbac
* @group Coverage
*/
#[CoversClass('\Rbac\Rbac')]
class RbacTest extends TestCase
{
/**
* @covers \Rbac\Rbac::__construct
*/
/*
public function testConstructorAcceptCustomTraversalStrategy()
{
Expand All @@ -34,9 +29,6 @@ public function testConstructorAcceptCustomTraversalStrategy()
}
*/

/**
* @covers \Rbac\Rbac::isGranted
*/
public function testInjectSingleRoleToArray()
{
$role = new Role('Foo');
Expand All @@ -45,31 +37,25 @@ public function testInjectSingleRoleToArray()
$traversalStrategy->expects($this->once())
->method('getRolesIterator')
->with($this->equalTo([$role]))
->will($this->returnValue(new ArrayIterator([])));
->willReturn(new ArrayIterator([]));

$rbac = new Rbac($traversalStrategy);

$rbac->isGranted($role, 'permission');
}

/**
* @covers \Rbac\Rbac::isGranted
*/
public function testFetchIteratorFromTraversalStrategy()
{
$traversalStrategy = $this->createMock('Rbac\Traversal\Strategy\TraversalStrategyInterface');
$traversalStrategy->expects($this->once())
->method('getRolesIterator')
->will($this->returnValue(new ArrayIterator([])));
->willReturn(new ArrayIterator([]));

$rbac = new Rbac($traversalStrategy);

$rbac->isGranted([], 'permission');
}

/**
* @covers \Rbac\Rbac::isGranted
*/
public function testTraverseRoles()
{
$role = $this->createMock('Rbac\Role\RoleInterface');
Expand All @@ -82,16 +68,13 @@ public function testTraverseRoles()
$rbac->isGranted($roles, 'permission');
}

/**
* @covers \Rbac\Rbac::isGranted
*/
public function testReturnTrueWhenRoleHasPermission()
{
$grantedRole = $this->createMock('Rbac\Role\RoleInterface');
$grantedRole->expects($this->once())
->method('hasPermission')
->with('permission')
->will($this->returnValue(true));
->willReturn(true);

$nextRole = $this->createMock('Rbac\Role\RoleInterface');
$nextRole->expects($this->never())->method('hasPermission');
Expand Down
15 changes: 2 additions & 13 deletions tests/RbacTest/Role/HierarchicalRoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@

namespace RbacTest;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Rbac\Role\HierarchicalRole;

/**
* @covers \Rbac\Role\HierarchicalRole
* @group Coverage
*/
#[CoversClass('\Rbac\Role\HierarchicalRole')]
class HierarchicalRoleTest extends TestCase
{
/**
* @covers \Rbac\Role\HierarchicalRole::addChild
*/
public function testCanAddChild()
{
$role = new HierarchicalRole('role');
Expand All @@ -31,9 +26,6 @@ public function testCanAddChild()
$this->assertCount(1, $role->getChildren());
}

/**
* @covers \Rbac\Role\HierarchicalRole::hasChildren
*/
public function testHasChildren()
{
$role = new HierarchicalRole('role');
Expand All @@ -45,9 +37,6 @@ public function testHasChildren()
$this->assertTrue($role->hasChildren());
}

/**
* @covers \Rbac\Role\HierarchicalRole::getChildren
*/
public function testCanGetChildren()
{
$role = new HierarchicalRole('role');
Expand Down
11 changes: 3 additions & 8 deletions tests/RbacTest/Role/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

namespace RbacTest;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Rbac\Role\Role;

/**
* @covers \Rbac\Role\Role
* @group Coverage
*/
#[CoversClass('\Rbac\Role\Role')]
class RoleTest extends TestCase
{
public function testSetNameByConstructor()
Expand All @@ -25,9 +23,6 @@ public function testSetNameByConstructor()
$this->assertEquals('phpIsHell', $role->getName());
}

/**
* @covers \Rbac\Role\Role::addPermission
*/
public function testRoleCanAddPermission()
{
$role = new Role('php');
Expand All @@ -36,7 +31,7 @@ public function testRoleCanAddPermission()
$this->assertTrue($role->hasPermission('debug'));

$permission = $this->createMock('Rbac\Permission\PermissionInterface');
$permission->expects($this->once())->method('__toString')->will($this->returnValue('interface'));
$permission->expects($this->once())->method('__toString')->willReturn('interface');
$role->addPermission($permission);

$this->assertTrue($role->hasPermission('interface'));
Expand Down
24 changes: 2 additions & 22 deletions tests/RbacTest/Traversal/RecursiveRoleIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,16 @@
namespace RbacTest\Traversal;

use ArrayIterator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Rbac\Role\HierarchicalRole;
use Rbac\Role\Role;
use Rbac\Traversal\RecursiveRoleIterator;
use stdClass;

/**
* @covers \Rbac\Traversal\RecursiveRoleIterator
* @group Coverage
*/
#[CoversClass('\Rbac\Traversal\RecursiveRoleIterator')]
class RecursiveRoleIteratorTest extends TestCase
{
/**
* @covers \Rbac\Traversal\RecursiveRoleIterator::__construct
*/
public function testAcceptTraversable()
{
$roles = new ArrayIterator([new Role('foo'), new Role('bar')]);
Expand All @@ -33,9 +28,6 @@ public function testAcceptTraversable()
$this->assertEquals($iterator->getArrayCopy(), $roles->getArrayCopy());
}

/**
* @covers \Rbac\Traversal\RecursiveRoleIterator::valid
*/
public function testValidateRoleInterface()
{
$foo = new Role('Foo');
Expand All @@ -50,9 +42,6 @@ public function testValidateRoleInterface()
$this->assertFalse($iterator->valid());
}

/**
* @covers \Rbac\Traversal\RecursiveRoleIterator::hasChildren
*/
public function testHasChildrenReturnsFalseIfCurrentRoleIsNotHierarchical()
{
$foo = new Role('Foo');
Expand All @@ -62,9 +51,6 @@ public function testHasChildrenReturnsFalseIfCurrentRoleIsNotHierarchical()
$this->assertFalse($iterator->hasChildren());
}

/**
* @covers \Rbac\Traversal\RecursiveRoleIterator::hasChildren
*/
public function testHasChildrenReturnsFalseIfCurrentRoleHasNotChildren()
{
$role = $this->createMock('Rbac\Role\HierarchicalRoleInterface');
Expand All @@ -75,9 +61,6 @@ public function testHasChildrenReturnsFalseIfCurrentRoleHasNotChildren()
$this->assertFalse($iterator->hasChildren());
}

/**
* @covers \Rbac\Traversal\RecursiveRoleIterator::hasChildren
*/
public function testHasChildrenReturnsTrueIfCurrentRoleHasChildren()
{
$role = $this->createMock('Rbac\Role\HierarchicalRoleInterface');
Expand All @@ -88,9 +71,6 @@ public function testHasChildrenReturnsTrueIfCurrentRoleHasChildren()
$this->assertTrue($iterator->hasChildren());
}

/**
* @covers \Rbac\Traversal\RecursiveRoleIterator::getChildren
*/
public function testGetChildrenReturnsAnRecursiveRoleIteratorOfRoleChildren()
{
$baz = new HierarchicalRole('Baz');
Expand Down
12 changes: 2 additions & 10 deletions tests/RbacTest/Traversal/Strategy/GeneratorStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@

namespace RbacTest\Traversal\Strategy;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Rbac\Role\HierarchicalRole;
use Rbac\Role\Role;
use Rbac\Traversal\Strategy\GeneratorStrategy;

/**
* @covers \Rbac\Traversal\Strategy\GeneratorStrategy
* @group Coverage
*/
#[CoversClass('\Rbac\Traversal\Strategy\GeneratorStrategy')]
class GeneratorStrategyTest extends TestCase
{
/**
* @covers \Rbac\Traversal\Strategy\GeneratorStrategy::getRolesIterator
*/
public function testTraverseFlatRoles()
{
$strategy = new GeneratorStrategy;
Expand All @@ -34,9 +29,6 @@ public function testTraverseFlatRoles()
);
}

/**
* @covers \Rbac\Traversal\Strategy\GeneratorStrategy::getRolesIterator
*/
public function testTraverseHierarchicalRoles()
{
$strategy = new GeneratorStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@

namespace RbacTest\Traversal\Strategy;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Rbac\Role\Role;
use Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy;

/**
* @covers \Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy
* @group Coverage
*/
#[CoversClass('\Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy')]
class RecursiveRoleIteratorStrategyTest extends TestCase
{
/**
* @covers \Rbac\Traversal\Strategy\RecursiveRoleIteratorStrategy::getRolesIterator
*/
public function testGetIterator()
{
$strategy = new RecursiveRoleIteratorStrategy;
Expand Down

0 comments on commit 1dbb77c

Please sign in to comment.