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 ca12fe7..c1ae9d9 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,5 @@ com_crashlytics_export_strings.xml crashlytics.properties crashlytics-build.properties fabric.properties - +.phpunit.result.cache +.phpcs-cache diff --git a/README.md b/README.md index 9d50394..07540d1 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Authentication base package defining interfaces for authentication services to b [![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-authentication)](https://github.com/dotkernel/dot-authentication/stargazers) [![GitHub license](https://img.shields.io/github/license/dotkernel/dot-authentication)](https://github.com/dotkernel/dot-authentication/blob/2.1/LICENSE.md) +[![SymfonyInsight](https://insight.symfony.com/projects/8a0dfa12-fdda-43d7-bdbe-1c6996b30c12/big.svg)](https://insight.symfony.com/projects/8a0dfa12-fdda-43d7-bdbe-1c6996b30c12) ## Installation diff --git a/composer.json b/composer.json index 8bca6f1..e432a9d 100644 --- a/composer.json +++ b/composer.json @@ -10,12 +10,13 @@ } ], "require": { - "php": "^7.4 || ~8.0.0 || ~8.1.0", + "php": "~8.1.0 || ~8.2.0", "psr/http-message": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^4.8", - "squizlabs/php_codesniffer": "^2.3" + "phpunit/phpunit": "^10.2", + "laminas/laminas-coding-standard": "^2.5", + "vimeo/psalm": "^5.13" }, "autoload": { "psr-4": { @@ -27,15 +28,22 @@ "DotTest\\Authentication\\": "test/" } }, - "extra": { - "branch-alias": { - "dev-master": "2.0-dev", - "dev-develop": "2.1-dev" - } - }, "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/AuthenticationInterface.php b/src/AuthenticationInterface.php index 5620c9a..ff20176 100644 --- a/src/AuthenticationInterface.php +++ b/src/AuthenticationInterface.php @@ -1,11 +1,6 @@ code = (int)$code; + protected const FAILURE = 0; + protected const SUCCESS = 1; + protected const FAILURE_INVALID_CREDENTIALS = -1; + protected const FAILURE_IDENTITY_AMBIGUOUS = -2; + protected const FAILURE_IDENTITY_NOT_FOUND = -3; + protected const FAILURE_UNCATEGORIZED = -4; + protected const FAILURE_MISSING_CREDENTIALS = -5; + + protected int $code; + + protected IdentityInterface $identity; + + protected string $message = ''; + + public function __construct(int $code, string $message = '', ?IdentityInterface $identity = null) + { + $this->code = $code; $this->identity = $identity; - $this->message = $message; + $this->message = $message; } - /** - * @return bool - */ public function isValid(): bool { - return ($this->code > 0); + return $this->code > 0; } - /** - * @return bool - */ public function hasIdentity(): bool { return $this->identity instanceof IdentityInterface; } - /** - * @return int - */ public function getCode(): int { return $this->code; } - /** - * @param int $code - */ - public function setCode(int $code) + public function setCode(int $code): void { $this->code = $code; } - /** - * @return IdentityInterface - */ public function getIdentity(): ?IdentityInterface { return $this->identity; } - /** - * @param IdentityInterface $identity - */ - public function setIdentity(IdentityInterface $identity) + public function setIdentity(IdentityInterface $identity): void { $this->identity = $identity; } - /** - * @return string - */ public function getMessage(): string { - return $this->message ?? ''; + return $this->message; } - /** - * @param string $message - */ - public function setMessage(string $message) + public function setMessage(string $message): void { $this->message = $message; } diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index 9de5798..76add1f 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -1,19 +1,12 @@ indentyInterfaceMock = $this->createMock(IdentityInterface::class); + $this->indentyInterfaceMock->method('getId')->will($this->returnValue(10)); + $this->indentyInterfaceMock->method('getName')->willReturn('username'); + $this->subject = new AuthenticationResult(2, 'valid', $this->indentyInterfaceMock); + } + + public function testAuth() + { + $code = $this->subject->getCode(); + $message = $this->subject->getMessage(); + $name = $this->subject->getIdentity(); + $interface = $this->subject->hasIdentity(); + $isValid = $this->subject->isValid(); + + $this->assertInstanceOf(IdentityInterface::class, $name); + $this->assertSame(2, $code); + $this->assertSame('valid', $message); + $this->assertSame('username', $name->getName()); + $this->assertSame(10, $name->getId()); + $this->assertTrue($interface); + $this->assertEquals('valid', $message); + $this->assertTrue($isValid); } }