From f279bd6d7278432ee1fe05e5e7fdc3b80644ecaf Mon Sep 17 00:00:00 2001 From: poprazvan17 Date: Thu, 20 Jul 2023 13:57:47 +0300 Subject: [PATCH 1/4] implemented phpUnit test, psalm, phpcs and static analysis --- .github/workflows/cs-tests.yml | 46 ++++++++++++++++++++++++ .github/workflows/static-analysis.yml | 46 ++++++++++++++++++++++++ .github/workflows/unit-tests.yaml | 47 +++++++++++++++++++++++++ .gitignore | 2 ++ README.md | 5 +++ composer.json | 21 ++++++++--- phpcs.xml | 20 +++++++++++ phpunit.xml | 10 ++++++ psalm.xml | 15 ++++++++ src/AuthorizationInterface.php | 13 ++----- src/Exception/ExceptionInterface.php | 9 +---- src/Exception/ForbiddenException.php | 15 ++------ src/Exception/RuntimeException.php | 12 +------ src/Identity/IdentityInterface.php | 8 +---- src/Role/RoleInterface.php | 9 ++--- test/AuthorizationInterfaceTest.php | 34 ++++++++++++++++++ test/Identity/IdentityInterfaceTest.php | 36 +++++++++++++++++++ test/Role/RoleInterfaceTest.php | 35 ++++++++++++++++++ 18 files changed, 323 insertions(+), 60 deletions(-) create mode 100644 .github/workflows/cs-tests.yml create mode 100644 .github/workflows/static-analysis.yml create mode 100644 .github/workflows/unit-tests.yaml create mode 100644 phpcs.xml create mode 100644 phpunit.xml create mode 100644 psalm.xml create mode 100644 test/AuthorizationInterfaceTest.php create mode 100644 test/Identity/IdentityInterfaceTest.php create mode 100644 test/Role/RoleInterfaceTest.php diff --git a/.github/workflows/cs-tests.yml b/.github/workflows/cs-tests.yml new file mode 100644 index 0000000..3da9965 --- /dev/null +++ b/.github/workflows/cs-tests.yml @@ -0,0 +1,46 @@ +on: + - push + +name: Run phpcs checks + +jobs: + mutation: + name: PHP ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + tools: composer:v2, cs2pr + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run phpcs checks + run: vendor/bin/phpcs diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..74550fc --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,46 @@ +on: + - push + +name: Run static analysis + +jobs: + mutation: + name: PHP ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + tools: composer:v2, cs2pr + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + - name: Install dependencies with composer + run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run static analysis + run: vendor/bin/psalm --no-cache --output-format=github --show-info=false --threads=4 diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml new file mode 100644 index 0000000..d2ab8e7 --- /dev/null +++ b/.github/workflows/unit-tests.yaml @@ -0,0 +1,47 @@ +on: + - push + +name: Run PHPUnit tests + +jobs: + mutation: + name: PHP ${{ matrix.php }}-${{ matrix.os }} + + runs-on: ${{ matrix.os }} + + strategy: + matrix: + os: + - ubuntu-latest + + php: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "${{ matrix.php }}" + tools: composer:v2, cs2pr + coverage: none + + - name: Determine composer cache directory + run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV + + - name: Cache dependencies installed with composer + uses: actions/cache@v3 + with: + path: ${{ env.COMPOSER_CACHE_DIR }} + key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: | + php${{ matrix.php }}-composer- + + - name: Install dependencies with composer + run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi + + - name: Run PHPUnit tests + run: vendor/bin/phpunit --colors=always diff --git a/.gitignore b/.gitignore index 6000c91..860882a 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ crashlytics.properties crashlytics-build.properties fabric.properties # Created by .ignore support plugin (hsz.mobi) +.phpunit.result.cache +.phpcs-cache \ No newline at end of file diff --git a/README.md b/README.md index 06f6bd5..da6e645 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ Authorization base package defining interfaces for authorization services to be [![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-authorization)](https://github.com/dotkernel/dot-authorization/stargazers) [![GitHub license](https://img.shields.io/github/license/dotkernel/dot-authorization)](https://github.com/dotkernel/dot-authorization/blob/3.0/LICENSE.md) +[![Build Static](https://github.com/dotkernel/dot-authorization/actions/workflows/static-analysis.yml/badge.svg?branch=3.0)](https://github.com/dotkernel/dot-authorization/actions/workflows/static-analysis.yml) + +[![SymfonyInsight](https://insight.symfony.com/projects/014df510-1cf7-4876-b1a8-303fbef2f364/big.svg)](https://insight.symfony.com/projects/014df510-1cf7-4876-b1a8-303fbef2f364) + + ## Installation Run the following command in you project directory diff --git a/composer.json b/composer.json index 542e2a4..3896f86 100644 --- a/composer.json +++ b/composer.json @@ -16,11 +16,12 @@ "laminas-dependency" ], "require": { - "php": "^7.4 || ~8.0.0 || ~8.1.0" + "php": "~8.1.0 || ~8.2.0" }, "require-dev": { - "phpunit/phpunit": "^9.1", - "squizlabs/php_codesniffer": "^3.5" + "laminas/laminas-coding-standard": "^2.5", + "phpunit/phpunit": "10.2", + "vimeo/psalm": "^5.13" }, "autoload": { "psr-4": { @@ -33,8 +34,20 @@ } }, "config": { + "sort-packages": true, "allow-plugins": { - "laminas/laminas-dependency-plugin": true + "dealerdirect/phpcodesniffer-composer-installer": true } + }, + "scripts": { + "check": [ + "@cs-check", + "@test" + ], + "cs-check": "phpcs", + "cs-fix": "phpcbf", + "test": "phpunit --colors=always", + "test-coverage": "phpunit --colors=always --coverage-clover clover.xml", + "static-analysis": "psalm --shepherd --stats" } } diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..1efe663 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + src + test + + + + diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..76304d8 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,10 @@ + + + + + ./test + + + diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..df50202 --- /dev/null +++ b/psalm.xml @@ -0,0 +1,15 @@ + + + + + + + + + diff --git a/src/AuthorizationInterface.php b/src/AuthorizationInterface.php index fd233e2..09aa63e 100644 --- a/src/AuthorizationInterface.php +++ b/src/AuthorizationInterface.php @@ -1,25 +1,16 @@ mockAuthorizationInterface = $this->createMock(AuthorizationInterface::class); + $this->mockAuthorizationInterface->method('isGranted')->willReturn(false); + } + + public function testCreate(): void + { + $this->assertInstanceOf(AuthorizationInterface::class, $this->mockAuthorizationInterface); + } + + public function testFunction(): void + { + $this->assertFalse($this->mockAuthorizationInterface->isGranted('no', ['admin'], null)); + } +} diff --git a/test/Identity/IdentityInterfaceTest.php b/test/Identity/IdentityInterfaceTest.php new file mode 100644 index 0000000..c2cddd1 --- /dev/null +++ b/test/Identity/IdentityInterfaceTest.php @@ -0,0 +1,36 @@ +identityInterfaceMock = $this->createMock(IdentityInterface::class); + $this->identityInterfaceMock->method('getRoles')->willReturn(['admin']); + } + + public function testCreate(): void + { + $this->assertInstanceOf(IdentityInterface::class, $this->identityInterfaceMock); + } + + public function testFunctions(): void + { + $role = $this->identityInterfaceMock->getRoles(); + $this->assertIsArray($role); + $this->assertSame(['admin'], $role); + } +} diff --git a/test/Role/RoleInterfaceTest.php b/test/Role/RoleInterfaceTest.php new file mode 100644 index 0000000..7220f69 --- /dev/null +++ b/test/Role/RoleInterfaceTest.php @@ -0,0 +1,35 @@ +roleInterfaceMock = $this->createMock(RoleInterface::class); + $this->roleInterfaceMock->method('getName')->willReturn('Test'); + } + + public function testCreate(): void + { + $this->assertInstanceOf(RoleInterface::class, $this->roleInterfaceMock); + } + + public function testFunctions(): void + { + $name = $this->roleInterfaceMock->getName(); + $this->assertSame('Test', $name); + } +} From 1abd1322d0e65c60466d4ebb74fde7c0c6af98e2 Mon Sep 17 00:00:00 2001 From: poprazvan17 Date: Thu, 20 Jul 2023 17:48:09 +0300 Subject: [PATCH 2/4] implemented phpUnit test, psalm, phpcs and static analysis --- .gitignore | 2 +- phpunit.xml | 2 +- src/AuthorizationInterface.php | 3 --- src/Exception/ExceptionInterface.php | 3 --- src/Identity/IdentityInterface.php | 3 --- test/Exception/ForbiddenExceptionTest.php | 30 +++++++++++++++++++++++ test/Exception/RuntimeExceptionTest.php | 19 ++++++++++++++ 7 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 test/Exception/ForbiddenExceptionTest.php create mode 100644 test/Exception/RuntimeExceptionTest.php diff --git a/.gitignore b/.gitignore index 860882a..08f9dfa 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,4 @@ crashlytics-build.properties fabric.properties # Created by .ignore support plugin (hsz.mobi) .phpunit.result.cache -.phpcs-cache \ No newline at end of file +.phpcs-cache diff --git a/phpunit.xml b/phpunit.xml index 76304d8..00092cd 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,7 +3,7 @@ xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php"> - + ./test diff --git a/src/AuthorizationInterface.php b/src/AuthorizationInterface.php index 09aa63e..3fe85cc 100644 --- a/src/AuthorizationInterface.php +++ b/src/AuthorizationInterface.php @@ -4,9 +4,6 @@ namespace Dot\Authorization; -/** - * Interface AuthorizationInterface - */ interface AuthorizationInterface { /** diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index 79adb66..2bc468b 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -4,9 +4,6 @@ namespace Dot\Authorization\Exception; -/** - * Interface ExceptionInterface - */ interface ExceptionInterface { } diff --git a/src/Identity/IdentityInterface.php b/src/Identity/IdentityInterface.php index f9e5eb2..53b0333 100644 --- a/src/Identity/IdentityInterface.php +++ b/src/Identity/IdentityInterface.php @@ -6,9 +6,6 @@ use Dot\Authorization\Role\RoleInterface; -/** - * Interface IdentityInterface - */ interface IdentityInterface { /** diff --git a/test/Exception/ForbiddenExceptionTest.php b/test/Exception/ForbiddenExceptionTest.php new file mode 100644 index 0000000..40c8c16 --- /dev/null +++ b/test/Exception/ForbiddenExceptionTest.php @@ -0,0 +1,30 @@ +forbiddenExceptionMock = $this->createMock(ForbiddenException::class); + } + + public function testCreate(): void + { + $this->assertInstanceOf(ForbiddenException::class, $this->forbiddenExceptionMock); + $this->assertInstanceOf(ExceptionInterface::class, $this->forbiddenExceptionMock); + } +} diff --git a/test/Exception/RuntimeExceptionTest.php b/test/Exception/RuntimeExceptionTest.php new file mode 100644 index 0000000..8934c13 --- /dev/null +++ b/test/Exception/RuntimeExceptionTest.php @@ -0,0 +1,19 @@ +assertInstanceOf(RuntimeException::class, $exception); + $this->assertInstanceOf(ExceptionInterface::class, $exception); + } +} From 5ac09e019f18ddd03b7185f303df15953e118d6c Mon Sep 17 00:00:00 2001 From: poprazvan17 Date: Fri, 21 Jul 2023 12:27:19 +0300 Subject: [PATCH 3/4] implemented phpUnit test, psalm, phpcs and static analysis --- README.md | 2 +- src/Role/RoleInterface.php | 7 ------ test/AuthorizationInterfaceTest.php | 2 +- test/Exception/ExceptionInterfaceTest.php | 28 +++++++++++++++++++++++ test/Exception/ForbiddenExceptionTest.php | 2 +- test/Exception/RuntimeExceptionTest.php | 2 +- test/Identity/IdentityInterfaceTest.php | 2 +- test/Role/RoleInterfaceTest.php | 2 +- 8 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 test/Exception/ExceptionInterfaceTest.php diff --git a/README.md b/README.md index da6e645..baac88e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Authorization base package defining interfaces for authorization services to be used with DotKernel applications. ![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-authorization) -![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-authorization/3.2.0) +![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-authorization/3.4.0) [![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-authorization)](https://github.com/dotkernel/dot-authorization/issues) [![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-authorization)](https://github.com/dotkernel/dot-authorization/network) diff --git a/src/Role/RoleInterface.php b/src/Role/RoleInterface.php index 92ae9b6..40b7b8b 100644 --- a/src/Role/RoleInterface.php +++ b/src/Role/RoleInterface.php @@ -1,16 +1,9 @@ exceptionInterfaceMock = $this->createMock(ExceptionInterface::class); + } + + public function testCreate(): void + { + $this->assertInstanceOf(ExceptionInterface::class, $this->exceptionInterfaceMock); + } +} diff --git a/test/Exception/ForbiddenExceptionTest.php b/test/Exception/ForbiddenExceptionTest.php index 40c8c16..2ba3b38 100644 --- a/test/Exception/ForbiddenExceptionTest.php +++ b/test/Exception/ForbiddenExceptionTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace DotTest\Exception; +namespace DotTest\Authorization\Exception; use Dot\Authorization\Exception\ExceptionInterface; use Dot\Authorization\Exception\ForbiddenException; diff --git a/test/Exception/RuntimeExceptionTest.php b/test/Exception/RuntimeExceptionTest.php index 8934c13..067a200 100644 --- a/test/Exception/RuntimeExceptionTest.php +++ b/test/Exception/RuntimeExceptionTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace DotTest\Exception; +namespace DotTest\Authorization\Exception; use Dot\Authorization\Exception\ExceptionInterface; use Dot\Authorization\Exception\RuntimeException; diff --git a/test/Identity/IdentityInterfaceTest.php b/test/Identity/IdentityInterfaceTest.php index c2cddd1..e7066d9 100644 --- a/test/Identity/IdentityInterfaceTest.php +++ b/test/Identity/IdentityInterfaceTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace DotTest\Identity; +namespace DotTest\Authorization\Identity; use Dot\Authorization\Identity\IdentityInterface; use PHPUnit\Framework\MockObject\Exception; diff --git a/test/Role/RoleInterfaceTest.php b/test/Role/RoleInterfaceTest.php index 7220f69..9ed5823 100644 --- a/test/Role/RoleInterfaceTest.php +++ b/test/Role/RoleInterfaceTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace DotTest\Role; +namespace DotTest\Authorization\Role; use Dot\Authorization\Role\RoleInterface; use PHPUnit\Framework\MockObject\Exception; From 8b65398ee7de1071c483ccddb7f8d6ee37b162c7 Mon Sep 17 00:00:00 2001 From: poprazvan17 Date: Mon, 24 Jul 2023 11:49:42 +0300 Subject: [PATCH 4/4] implemented phpUnit test, psalm, phpcs and static analysis --- src/AuthorizationInterface.php | 3 --- test/AuthorizationInterfaceTest.php | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/AuthorizationInterface.php b/src/AuthorizationInterface.php index 3fe85cc..88cdba1 100644 --- a/src/AuthorizationInterface.php +++ b/src/AuthorizationInterface.php @@ -6,8 +6,5 @@ interface AuthorizationInterface { - /** - * @param array $roles - */ public function isGranted(string $permission, array $roles = [], mixed $context = null): bool; } diff --git a/test/AuthorizationInterfaceTest.php b/test/AuthorizationInterfaceTest.php index 77581ea..0a0b231 100644 --- a/test/AuthorizationInterfaceTest.php +++ b/test/AuthorizationInterfaceTest.php @@ -29,6 +29,6 @@ public function testCreate(): void public function testFunction(): void { - $this->assertFalse($this->mockAuthorizationInterface->isGranted('no', ['admin'], null)); + $this->assertFalse($this->mockAuthorizationInterface->isGranted('no', ['admin'])); } }