diff --git a/.gitattributes b/.gitattributes index 93397b5..ca5eee6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,16 +2,16 @@ # https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html # Ignore all test and documentation with "export-ignore". -/.gitattributes export-ignore -/.gitignore export-ignore -/.travis.yml export-ignore -/phpunit.xml.dist export-ignore -/.scrutinizer.yml export-ignore +/.github export-ignore +/docker export-ignore /tests export-ignore -/.editorconfig export-ignore /.coveralls.yml export-ignore -/.styleci.yml export-ignore -/lighthouse-paperclip-demo.gif export-ignore -/docker export-ignore +/.editorconfig export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore /docker-compose.yml export-ignore +/lighthouse-paperclip-demo.gif export-ignore /Makefile export-ignore +/phpstan.neon export-ignore +/phpunit.xml.dist export-ignore +/rector.php export-ignore diff --git a/.github/workflows/run-composer-normalize.yml b/.github/workflows/run-composer-normalize.yml new file mode 100644 index 0000000..f4bfb59 --- /dev/null +++ b/.github/workflows/run-composer-normalize.yml @@ -0,0 +1,26 @@ +name: Normalize + +on: + push: + paths: + - composer.json + +jobs: + normalize: + runs-on: ubuntu-latest + steps: + - name: Git checkout + uses: actions/checkout@v2 + + - name: Validate Composer configuration + run: composer validate --strict + + - name: Normalize composer.json + run: | + composer global config --no-plugins allow-plugins.ergebnis/composer-normalize true + composer global require ergebnis/composer-normalize + composer normalize + + - uses: stefanzweifel/git-auto-commit-action@v4.0.0 + with: + commit_message: normalize composer.json diff --git a/.github/workflows/run-coverage.yml b/.github/workflows/run-coverage.yml new file mode 100644 index 0000000..e071c04 --- /dev/null +++ b/.github/workflows/run-coverage.yml @@ -0,0 +1,44 @@ +name: Coverage + +on: [push, pull_request] + +jobs: + coverage: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest] + php: [8.1] + dependency-version: [prefer-stable] + + name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.composer/cache/files + key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: pcov + + - name: Install dependencies + run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction + + - name: Execute tests with coverage + run: composer coverage + + - name: Upload coverage results to Coveralls + env: + COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + composer global require php-coveralls/php-coveralls + php-coveralls diff --git a/.github/workflows/run-larastan.yml b/.github/workflows/run-larastan.yml new file mode 100644 index 0000000..5151a9d --- /dev/null +++ b/.github/workflows/run-larastan.yml @@ -0,0 +1,36 @@ +name: Static Analysis + +on: [push, pull_request, workflow_dispatch] + +jobs: + static-analysis: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest] + php: [8.1] + dependency-version: [prefer-stable] + + name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.composer/cache/files + key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + + - name: Install dependencies + run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction + + - name: Execute static code analysis + run: composer phpstan diff --git a/.github/workflows/run-style.yml b/.github/workflows/run-style.yml new file mode 100644 index 0000000..35fc325 --- /dev/null +++ b/.github/workflows/run-style.yml @@ -0,0 +1,38 @@ +name: Code Style + +on: [push, pull_request] + +jobs: + fix-style: + name: Fix Code Style + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest] + php: [8.1] + dependency-version: [prefer-stable] + env: + COMPOSER_NO_INTERACTION: 1 + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + + - name: Install dependencies + run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction + + - run: composer rector + continue-on-error: true + + - run: composer pint + continue-on-error: true + + - uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: composer pint diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..5abe133 --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,43 @@ +name: Tests + +on: [push, pull_request, workflow_dispatch] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest] + php: [8.1] + dependency-version: [prefer-lowest, prefer-stable] + + name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.composer/cache/files + key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip + coverage: none + + - name: Setup Problem Matches + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Install dependencies + run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction + + - name: Execute tests + run: composer test diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index df16b68..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,19 +0,0 @@ -filter: - excluded_paths: [tests/*] - -checks: - php: - remove_extra_empty_lines: true - remove_php_closing_tag: true - remove_trailing_whitespace: true - fix_use_statements: - remove_unused: true - preserve_multiple: false - preserve_blanklines: true - order_alphabetically: true - fix_php_opening_tag: true - fix_linefeed: true - fix_line_ending: true - fix_identation_4spaces: true - fix_doc_comments: true - diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index f4d3cbc..0000000 --- a/.styleci.yml +++ /dev/null @@ -1,4 +0,0 @@ -preset: laravel - -disabled: - - single_class_element_per_statement diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a1d7dff..0000000 --- a/.travis.yml +++ /dev/null @@ -1,21 +0,0 @@ -language: php - -php: - - 7.4 - -env: - matrix: - - COMPOSER_FLAGS="--prefer-lowest" - - COMPOSER_FLAGS="" - -before_script: - - (phpenv config-rm xdebug.ini || exit 0) - - pecl install pcov - - travis_retry composer self-update - - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - -script: - - vendor/bin/phpunit --coverage-clover build/logs/clover.xml - -after_success: - - travis_retry php vendor/bin/php-coveralls diff --git a/Makefile b/Makefile index 81040c8..8fe8c11 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,8 @@ setup: @docker-compose run --rm app composer install destroy: - @docker-compose down --rmakemove-orphans --volumes + @docker-compose down --remove-orphans --volumes -.PHONY: app app: @docker-compose run --rm app sh diff --git a/README.md b/README.md index 90a73d6..3e59ed0 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) [![Build Status](https://img.shields.io/travis/daniel-de-wit/lighthouse-paperclip/master.svg?style=flat-square)](https://travis-ci.org/daniel-de-wit/lighthouse-paperclip) [![Coverage Status](https://coveralls.io/repos/github/daniel-de-wit/lighthouse-paperclip/badge.svg?branch=master)](https://coveralls.io/github/daniel-de-wit/lighthouse-paperclip?branch=master) -[![Quality Score](https://img.shields.io/scrutinizer/g/daniel-de-wit/lighthouse-paperclip.svg?style=flat-square)](https://scrutinizer-ci.com/g/daniel-de-wit/lighthouse-paperclip) [![Total Downloads](https://img.shields.io/packagist/dt/daniel-de-wit/lighthouse-paperclip.svg?style=flat-square)](https://packagist.org/packages/daniel-de-wit/lighthouse-paperclip) This package will add two directives that will support working with [czim/laravel-paperclip](https://github.com/czim/laravel-paperclip). diff --git a/composer.json b/composer.json index e58a07f..91ea19c 100644 --- a/composer.json +++ b/composer.json @@ -1,15 +1,14 @@ { "name": "daniel-de-wit/lighthouse-paperclip", "description": "Lighthouse GraphQL Directives for Paperclip attachment support", + "license": "MIT", + "type": "library", "keywords": [ "daniel-de-wit", "laravel-paperclip", "lighthouse", "lighthouse-paperclip" ], - "homepage": "https://github.com/daniel-de-wit/lighthouse-paperclip", - "license": "MIT", - "type": "library", "authors": [ { "name": "Daniel de Wit", @@ -17,15 +16,21 @@ "role": "Developer" } ], + "homepage": "https://github.com/daniel-de-wit/lighthouse-paperclip", "require": { - "php": "^7.4|^8.0", - "czim/laravel-paperclip": "^3|^4", + "php": "^8.1", + "czim/laravel-paperclip": "^5", "nuwave/lighthouse": "^5" }, "require-dev": { - "orchestra/testbench": "^6.0", + "laravel/pint": "^1.2", + "nunomaduro/larastan": "^2.2", + "orchestra/testbench": "^7.11", "php-coveralls/php-coveralls": "^2.4", - "phpunit/phpunit": "^9.0" + "phpstan/phpstan-mockery": "^1.1", + "phpstan/phpstan-phpunit": "^1.2", + "phpunit/phpunit": "^9.5", + "rector/rector": "^0.14.8" }, "autoload": { "psr-4": { @@ -37,10 +42,6 @@ "DanielDeWit\\LighthousePaperclip\\Tests\\": "tests" } }, - "scripts": { - "test": "vendor/bin/phpunit", - "test-coverage": "vendor/bin/phpunit --coverage-html coverage" - }, "config": { "sort-packages": true }, @@ -50,5 +51,15 @@ "DanielDeWit\\LighthousePaperclip\\Providers\\LighthousePaperclipServiceProvider" ] } + }, + "scripts": { + "post-autoload-dump": [ + "@php ./vendor/bin/testbench package:discover --ansi" + ], + "coverage": "vendor/bin/phpunit", + "phpstan": "vendor/bin/phpstan analyse", + "pint": "vendor/bin/pint", + "rector": "vendor/bin/rector", + "test": "vendor/bin/phpunit --no-coverage" } } diff --git a/docker/Dockerfile b/docker/Dockerfile index 56439f1..a49f481 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,5 +2,13 @@ FROM php:8.1-cli-alpine WORKDIR /var/www/html -COPY conf.d /usr/local/etc/php/conf.d COPY --from=composer /usr/bin/composer /usr/bin/composer + +RUN apk add --no-cache --virtual .build-deps \ + $PHPIZE_DEPS \ + && pecl install \ + pcov \ + && docker-php-ext-enable \ + pcov + +COPY conf.d /usr/local/etc/php/conf.d diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..81170e8 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,13 @@ +includes: + - vendor/nunomaduro/larastan/extension.neon + - vendor/phpstan/phpstan-mockery/extension.neon + - vendor/phpstan/phpstan-phpunit/extension.neon + - vendor/phpstan/phpstan-phpunit/rules.neon + +parameters: + level: max + paths: + - src + - tests + stubFiles: + - vendor/nuwave/lighthouse/_ide_helper.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 11d6f8b..16d4afb 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,19 @@ + verbose="true" +> - - tests + + ./tests/Unit @@ -22,13 +22,8 @@ src + + + - - - - - - - - diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..52e0395 --- /dev/null +++ b/rector.php @@ -0,0 +1,78 @@ +paths([ + __DIR__.'/src', + __DIR__.'/tests', + ]); + + // define sets of rules + $rectorConfig->sets([ + // PHP + LevelSetList::UP_TO_PHP_81, + + // PHPUnit + PHPUnitLevelSetList::UP_TO_PHPUNIT_100, + PHPUnitSetList::PHPUNIT_CODE_QUALITY, + PHPUnitSetList::PHPUNIT_EXCEPTION, + PHPUnitSetList::REMOVE_MOCKS, + PHPUnitSetList::PHPUNIT_SPECIFIC_METHOD, + PHPUnitSetList::PHPUNIT_YIELD_DATA_PROVIDER, + + SetList::DEAD_CODE, + SetList::CODING_STYLE, + SetList::EARLY_RETURN, + SetList::ACTION_INJECTION_TO_CONSTRUCTOR_INJECTION, + SetList::TYPE_DECLARATION, + SetList::TYPE_DECLARATION_STRICT, + ]); + + $rectorConfig->skip([ + ClosureToArrowFunctionRector::class, + AddDefaultValueForUndefinedVariableRector::class, + JsonThrowOnErrorRector::class, + NullableCompareToNullRector::class, + VarConstantCommentRector::class, + EncapsedStringsToSprintfRector::class, + UnSpreadOperatorRector::class, + AddArrayParamDocTypeRector::class, + AddArrayReturnDocTypeRector::class, + ReturnTypeDeclarationRector::class, + PostIncDecToPreIncDecRector::class, + AddLiteralSeparatorToNumberRector::class, + StaticArrowFunctionRector::class, + StaticClosureRector::class, + StaticCallOnNonStaticToInstanceCallRector::class, + FirstClassCallableRector::class, + ArrayShapeFromConstantArrayReturnRector::class, + ReturnNeverTypeRector::class, + AddSeeTestAnnotationRector::class, + ]); +}; diff --git a/src/Directives/VariantDirective.php b/src/Directives/VariantDirective.php index 0d0dc2d..ce66056 100644 --- a/src/Directives/VariantDirective.php +++ b/src/Directives/VariantDirective.php @@ -38,8 +38,8 @@ public function manipulateArgDefinition( InputValueDefinitionNode &$argDefinition, FieldDefinitionNode &$parentField, ObjectTypeDefinitionNode &$parentType - ): void - { + ): void { + /** @var string $modelName */ $modelName = ASTHelper::modelName($parentType); $modelFQN = $this->namespaceModelClass( @@ -53,37 +53,39 @@ public function manipulateArgDefinition( $availableVariants = $model->{$attribute}->variants(true); foreach ($availableVariants as $variant) { - if (preg_match('/^[0-9]/', $variant)) { - throw new \RuntimeException('Unable to create enum value for ' . $modelFQN . ' ' . $attribute . ' Attachment variant "' . $variant . '", due to starting with integer'); + if (preg_match('#^[0-9]#', (string) $variant)) { + throw new \RuntimeException('Unable to create enum value for '.$modelFQN.' '.$attribute.' Attachment variant "'.$variant.'", due to starting with integer'); } } + /** @var array $allowedVariants */ $allowedVariants = $this->directiveArgValue('variants'); + /** @var array $variants */ $variants = $availableVariants; if ($allowedVariants) { // Check if the pre-defined variants actually exist. $faulty = array_diff($allowedVariants, $availableVariants); - if (count($faulty)) { - // Todo: Throw dedicated Exception./vendor/bin/php-cs-fixer fix --allow-risky=yes --show-progress=run-in --dry-run - throw new Exception('Variant(s) "' . implode('", "', $faulty) . '" are not available for attachment "' . $attribute . '" on model "' . $modelFQN . '"'); + if ($faulty !== []) { + throw new Exception('Variant(s) "'.implode('", "', $faulty).'" are not available for attachment "'.$attribute.'" on model "'.$modelFQN.'"'); } $variants = $allowedVariants; } - $resizeEnumName = $parentType->name->value . Str::studly($parentField->name->value) . Str::studly($argDefinition->name->value); + $resizeEnumName = $parentType->name->value.Str::studly($parentField->name->value).Str::studly($argDefinition->name->value); $argDefinition->type = Parser::namedType($resizeEnumName); - $enumValues = collect($variants)->map(function (string $variant) { - return strtoupper($variant) . ' @enum(value: "' . $variant . '")'; + $enumValues = collect($variants)->map(function (string $variant): string { + return strtoupper($variant).' @enum(value: "'.$variant.'")'; }); - $enumDefinition = "\"Allowed resizes for the `{$argDefinition->name->value}` argument on the query `{$parentField->name->value}`.\"\n" . "enum $resizeEnumName {\n"; + $enumDefinition = "\"Allowed resizes for the `{$argDefinition->name->value}` argument on the query `{$parentField->name->value}`.\"\n"."enum {$resizeEnumName} {\n"; foreach ($enumValues as $enumValue) { - $enumDefinition .= "$enumValue\n"; + $enumDefinition .= "{$enumValue}\n"; } + $enumDefinition .= '}'; $documentAST->setTypeDefinition( diff --git a/tests/Directives/DirectiveTest.php b/tests/Directives/DirectiveTest.php deleted file mode 100644 index 2640f85..0000000 --- a/tests/Directives/DirectiveTest.php +++ /dev/null @@ -1,61 +0,0 @@ -setUpTestSchema(); - } - - protected function getPackageProviders($app): array - { - return [ - LighthouseServiceProvider::class, - LighthousePaperclipServiceProvider::class, - ]; - } - - /** - * Define environment setup. - * - * @param \Illuminate\Foundation\Application $app - */ - protected function getEnvironmentSetUp($app): void - { - /** @var \Illuminate\Contracts\Config\Repository $config */ - $config = $app['config']; - - $config->set('app.debug', true); - } - - /** - * Build an executable schema from an SDL string. - */ - protected function buildSchema(string $schema): Schema - { - $this->schema = $schema; - - return $this->app - ->make(GraphQL::class) - ->prepSchema(); - } -} diff --git a/tests/Directives/AttachmentDirectiveTest.php b/tests/Unit/GraphQL/Directives/AttachmentDirectiveTest.php similarity index 93% rename from tests/Directives/AttachmentDirectiveTest.php rename to tests/Unit/GraphQL/Directives/AttachmentDirectiveTest.php index 4066445..0a58e92 100644 --- a/tests/Directives/AttachmentDirectiveTest.php +++ b/tests/Unit/GraphQL/Directives/AttachmentDirectiveTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace DanielDeWit\LighthousePaperclip\Tests\Directives; +namespace DanielDeWit\LighthousePaperclip\Tests\Unit\GraphQL\Directives; use Czim\Paperclip\Attachment\Attachment; use Illuminate\Database\Eloquent\Model; diff --git a/tests/Unit/GraphQL/Directives/DirectiveTest.php b/tests/Unit/GraphQL/Directives/DirectiveTest.php new file mode 100644 index 0000000..5af7f8a --- /dev/null +++ b/tests/Unit/GraphQL/Directives/DirectiveTest.php @@ -0,0 +1,11 @@ +setUpTestSchema(); + } +} diff --git a/tests/Unit/UnitTest.php b/tests/Unit/UnitTest.php new file mode 100644 index 0000000..cd97918 --- /dev/null +++ b/tests/Unit/UnitTest.php @@ -0,0 +1,36 @@ +set('app.debug', true); + } +}