diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f0194b3 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,49 @@ +name: CI + +on: + push: + branches: [ main, develop ] + pull_request: + branches: [ main, develop ] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + php-versions: ['8.1', '8.3'] + + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + + - name: Print PHP version + run: php -v + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v4 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Validate composer.json + run: composer validate + + - name: Install dependencies + run: composer install --optimize-autoloader --no-progress --no-interaction --prefer-dist + + - name: Run tests + run: vendor/bin/phpunit --testdox + + - name: Run PHPStan + run: vendor/bin/phpstan analyse --level 8 --no-interaction --no-progress src + + - name: Run PHPCS + run: vendor/bin/phpcs src tests --standard=psr2 -s diff --git a/composer.json b/composer.json index 0b0c0e8..7922282 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "require-dev": { "phpunit/phpunit": "^10.5", "squizlabs/php_codesniffer": "^3.8", - "mockery/mockery": "^1.6" + "mockery/mockery": "^1.6", + "phpstan/phpstan": "^1.10" } } diff --git a/src/Provider/Galette.php b/src/Provider/Galette.php index fffcdd4..c0ef197 100644 --- a/src/Provider/Galette.php +++ b/src/Provider/Galette.php @@ -29,10 +29,14 @@ class Galette extends AbstractProvider protected string $pluginDirectory = 'plugin-oauth2'; /** - * @var array + * @var string[] */ protected array $scope; + /** + * @param array $options + * @param array $collaborators + */ public function __construct(array $options = [], array $collaborators = []) { if (!isset($options['instance'])) { @@ -62,38 +66,56 @@ public function getBaseURL(): string ); } - public function getBaseAuthorizationUrl() + public function getBaseAuthorizationUrl(): string { return $this->getBaseURL() . '/authorize'; } - public function getBaseAccessTokenUrl(array $params) + /** + * @param array $params + * @return string + */ + public function getBaseAccessTokenUrl(array $params): string { return $this->getBaseURL() . '/access_token'; } - public function getResourceOwnerDetailsUrl(AccessToken $token) + public function getResourceOwnerDetailsUrl(AccessToken $token): string { return $this->getBaseURL() . '/user'; } - protected function getDefaultScopes() + /** + * @return string[] + */ + protected function getDefaultScopes(): array { return []; } + /** + * @param ResponseInterface $response + * @param string|array $data + * @return void + * @throws IdentityProviderException + */ protected function checkResponse(ResponseInterface $response, $data) { $statusCode = $response->getStatusCode(); if ($statusCode > 400) { throw new IdentityProviderException( - $data['message'] ?: $response->getReasonPhrase(), + (is_array($data) && $data['message']) ? $data['message'] : $response->getReasonPhrase(), $statusCode, $response ); } } + /** + * @param array $response + * @param AccessToken $token + * @return GaletteResourceOwner + */ protected function createResourceOwner(array $response, AccessToken $token) { return new GaletteResourceOwner($response); diff --git a/src/Provider/GaletteResourceOwner.php b/src/Provider/GaletteResourceOwner.php index dcbef0c..35703b9 100644 --- a/src/Provider/GaletteResourceOwner.php +++ b/src/Provider/GaletteResourceOwner.php @@ -7,13 +7,16 @@ class GaletteResourceOwner implements ResourceOwnerInterface { - use ArrayAccessorTrait; + use ArrayAccessorTrait; /** * @var array */ protected array $response; + /** + * @param array $response + */ public function __construct(array $response = []) { $this->response = $response; @@ -73,10 +76,10 @@ public function getStatus(): ?int /** * Return all owner details available as an array. * - * @return array + * @return array */ public function toArray(): array { return $this->response; } -} \ No newline at end of file +} diff --git a/tests/src/Provider/GaletteTest.php b/tests/src/Provider/GaletteTest.php index 39e5c15..d22d78e 100644 --- a/tests/src/Provider/GaletteTest.php +++ b/tests/src/Provider/GaletteTest.php @@ -60,7 +60,6 @@ public function testGetAuthorizationUrl() public function testGetAccessToken() { $response = m::mock('Psr\Http\Message\ResponseInterface'); - //$response->shouldReceive('getBody')->andReturn('{"access_token": "mock_access_token", "token_type": "bearer", "account_id": "12345", "uid": "deprecated_id"}'); $response ->shouldReceive('getBody') ->andReturn( @@ -117,7 +116,8 @@ public function testUserData() ->andReturn( new Stream( fopen( - 'data://text/plain,{"id": '.$userId.', "username": "'.$username.'", "email": "'.$email.'", "language": "'.$lang.'", "status": '.$status.'}', + 'data://text/plain,{"id": '.$userId.', "username": "'.$username.'", "email": "'. + $email.'", "language": "'.$lang.'", "status": '.$status.'}', 'r' ) ) @@ -156,7 +156,7 @@ public function testUserData() public function testUserDataFails() { $this->expectException(\League\OAuth2\Client\Provider\Exception\IdentityProviderException::class); - $status = rand(400,600); + $status = rand(400, 600); $postResponse = m::mock('Psr\Http\Message\ResponseInterface'); $postResponse