Skip to content

Commit

Permalink
Merge pull request #61 from bizley/update-tests-workflow
Browse files Browse the repository at this point in the history
Update tests and GH workflow
  • Loading branch information
bizley authored Apr 7, 2024
2 parents 8af895b + ece994d commit e96172e
Show file tree
Hide file tree
Showing 17 changed files with 211 additions and 215 deletions.
38 changes: 15 additions & 23 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ on:
jobs:
PHPStan:
runs-on: ubuntu-latest
strategy:
fail-fast: false

steps:
- name: Checkout
Expand All @@ -19,26 +17,23 @@ jobs:
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.3'
extensions: mbstring, intl
coverage: none
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
update: true

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
- name: Cache vendor
uses: actions/cache@v4
id: cache-vendor
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
path: ./vendor
key: ${{ runner.os }}-8.3-vendor-${{ hashFiles('./composer.json') }}

- name: Install dependencies
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader
if: steps.cache-vendor.outputs.cache-hit != 'true'
run: composer i --prefer-dist --no-interaction --no-progress --optimize-autoloader

- name: PHPStan tests
run: vendor/bin/phpstan analyze -l 9 -a vendor/yiisoft/yii2/Yii.php --no-progress src
Expand All @@ -49,7 +44,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.1', '8.2']
php: ['8.1', '8.2', '8.3']

steps:
- name: Checkout
Expand All @@ -65,26 +60,23 @@ jobs:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
update: true

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
- name: Cache vendor
uses: actions/cache@v4
id: cache-vendor
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
path: ./vendor
key: ${{ runner.os }}-${{ matrix.php }}-vendor-${{ hashFiles('./composer.json') }}

- name: Install dependencies
if: steps.cache-vendor.outputs.cache-hit != 'true'
run: composer update --no-interaction --no-progress --optimize-autoloader

- name: Run PHPUnit
if: matrix.php != '8.1'
if: matrix.php != '8.3'
run: vendor/bin/phpunit

- name: Run Infection with PHPUnit
if: matrix.php == '8.1'
if: matrix.php == '8.3'
run: |
mkdir -p build/logs
vendor/bin/phpunit --coverage-xml=build/logs/coverage-xml --log-junit=build/logs/junit.xml
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"authors": [
{
"name": "Paweł Bizley Brzozowski",
"email": "pawel@positive.codes"
"email": "pawel[email protected]"
},
{
"name": "Dmitriy Demin",
Expand Down
2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"MethodCallRemoval": {
"ignore": [
"bizley\\jwt\\Jwt::init::124",
"bizley\\jwt\\JwtHttpBearerAuth::init::77"
"bizley\\jwt\\JwtHttpBearerAuth::init::72"
]
}
},
Expand Down
26 changes: 26 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="tests/bootstrap.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="Yii 2 JWT Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage includeUncoveredFiles="true"
pathCoverage="false"
ignoreDeprecatedCodeUnits="true"
disableCodeCoverageIgnore="false"/>
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
14 changes: 0 additions & 14 deletions phpunit.xml.dist

This file was deleted.

15 changes: 5 additions & 10 deletions src/JwtHttpBearerAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Lcobucci\JWT\Encoding\CannotDecodeContent;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation;
use Throwable;
use Yii;
use yii\base\InvalidConfigException;
use yii\di\Instance;
use yii\filters\auth\HttpBearerAuth;
Expand All @@ -18,9 +16,6 @@
use yii\web\UnauthorizedHttpException;
use yii\web\User;

use function call_user_func;
use function get_class;

/**
* JwtHttpBearerAuth is an action filter that supports the authentication method based on HTTP Bearer JSON Web Token.
*
Expand Down Expand Up @@ -126,19 +121,19 @@ public function authenticate($user, $request, $response): ?IdentityInterface //
if (!empty($data)) {
$token = $this->processToken($data);
}
} catch (Throwable $exception) {
Yii::warning($exception->getMessage(), 'JwtHttpBearerAuth');
} catch (\Throwable $exception) {
\Yii::warning($exception->getMessage(), 'JwtHttpBearerAuth');
if (!$this->throwException) {
return null;
}
throw $exception;
}

if ($token !== null) {
if (is_callable($this->auth, true)) {
$identity = call_user_func($this->auth, $token);
if (\is_callable($this->auth, true)) {
$identity = \call_user_func($this->auth, $token);
} else {
$identity = $user->loginByAccessToken($token->toString(), get_class($this));
$identity = $user->loginByAccessToken($token->toString(), \get_class($this));
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/JwtTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation;
use Lcobucci\JWT\Validator;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\di\Instance;
Expand Down Expand Up @@ -86,10 +85,10 @@ private function buildObjectFromArray(array $config): object
$keys = \array_keys($config);
if (\is_string(\reset($keys))) {
// most probably Yii-style config
return Yii::createObject($config);
return \Yii::createObject($config);
}

return Yii::createObject(...$config);
return \Yii::createObject(...$config);
}

/**
Expand Down Expand Up @@ -176,7 +175,7 @@ public function buildKey($key): Signer\Key
}
if (\str_starts_with($key, '@')) {
$keyConfig = [
Jwt::KEY => Yii::getAlias($key),
Jwt::KEY => \Yii::getAlias($key),
Jwt::METHOD => Jwt::METHOD_FILE,
];
} elseif (\str_starts_with($key, 'file://')) {
Expand Down Expand Up @@ -236,7 +235,7 @@ public function buildSigner($signer): Signer
}

if (\in_array($signer, [Jwt::ES256, Jwt::ES384, Jwt::ES512], true)) {
Yii::$container->set(Signer\Ecdsa\SignatureConverter::class, Signer\Ecdsa\MultibyteStringConverter::class);
\Yii::$container->set(Signer\Ecdsa\SignatureConverter::class, Signer\Ecdsa\MultibyteStringConverter::class);
}

/** @var Signer $signerInstance */
Expand Down
44 changes: 30 additions & 14 deletions tests/ConsecutiveCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,51 @@
namespace bizley\tests;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint;

class ConsecutiveCalls extends Assert
{
public const NEVER = 'never';
public const RETURN = 'return';
/**
* @var array<mixed[]>
*/
private array $data = [];
private int $internalCounter = -1;

/**
* @param array<mixed[]> $data
* @param mixed[] ...$args
*/
public function __construct(private array $data, private readonly string $mode = self::RETURN)
private function __construct(array ...$args)
{
foreach ($args as $arg) {
if (!\is_array($arg)) {
throw new \InvalidArgumentException('All arguments must be arrays');
}

$this->data[] = $arg;
}
}

/**
* @param mixed ...$args
* @return mixed|void|null
* @param mixed[] ...$arguments
*/
public function __invoke(...$args)
public static function withArgs(array ...$arguments): self
{
$testData = \array_shift($this->data);
return new self(...$arguments);
}

if ($this->mode === self::NEVER) {
self::assertSame($testData, $args);
} else {
$returned = $testData ? \array_pop($testData) : null;
self::assertSame($testData, $args);
public function __invoke(mixed ...$args): void
{
$testData = $this->data[++$this->internalCounter] ?? null;
if ($testData === null) {
$testData = $this->data[$this->internalCounter % \count($this->data)];
}

return $returned;
foreach ($testData as $key => $value) {
if ($value instanceof Constraint) {
$value->evaluate($args[$key]);
} else {
self::assertEquals($value, $args[$key]);
}
}
}
}
Loading

0 comments on commit e96172e

Please sign in to comment.