From 07c4bc353211883b207360d9d8812f34ceaa665a Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Thu, 22 Feb 2024 08:46:14 +0700 Subject: [PATCH] [#89] Updated to scaffold 0.12.1 (#90) --- .github/PULL_REQUEST_TEMPLATE.md | 16 +++++ .github/release-drafter.yml | 8 ++- ...assign-pr-author.yml => assign-author.yml} | 4 +- .github/workflows/draft-release-notes.yml | 25 +++++++ .github/workflows/release.yml | 39 ----------- README.md | 6 ++ composer.json | 8 ++- phpmd.xml | 15 +++++ rector.php | 66 +++++++++++++++++++ .../ScreenshotContextInitializer.php | 51 ++------------ .../Context/ScreenshotContext.php | 23 +++---- .../BehatScreenshotExtension.php | 19 +++--- 12 files changed, 164 insertions(+), 116 deletions(-) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md rename .github/workflows/{auto-assign-pr-author.yml => assign-author.yml} (71%) create mode 100644 .github/workflows/draft-release-notes.yml delete mode 100644 .github/workflows/release.yml create mode 100644 phpmd.xml create mode 100644 rector.php diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..95cbae2 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,16 @@ +## Checklist before requesting a review + +- [ ] I have formatted the subject to include ticket number as `[#123] Verb in past tense with dot at the end.` +- [ ] I have added a link to the issue tracker +- [ ] I have provided information in `Changed` section about WHY something was done if this was not a normal implementation +- [ ] I have performed a self-review of my code +- [ ] I have commented my code, particularly in hard-to-understand areas +- [ ] I have added tests that prove my fix is effective or that my feature works +- [ ] I have run new and existing relevant tests locally with my changes, and they passed +- [ ] I have provided screenshots, where applicable + +## Changed + +1. + +## Screenshots diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml index 699ff47..8b07383 100644 --- a/.github/release-drafter.yml +++ b/.github/release-drafter.yml @@ -1,12 +1,14 @@ -name-template: '$NEXT_MINOR_VERSION' -tag-template: '$NEXT_MINOR_VERSION' +name-template: '$RESOLVED_VERSION' +tag-template: '$RESOLVED_VERSION' change-template: '- $TITLE @$AUTHOR (#$NUMBER)' change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks. +version-resolver: + default: minor template: | ## What's new since $PREVIOUS_TAG $CHANGES - **Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...$NEXT_MINOR_VERSION + **Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...$RESOLVED_VERSION $CONTRIBUTORS diff --git a/.github/workflows/auto-assign-pr-author.yml b/.github/workflows/assign-author.yml similarity index 71% rename from .github/workflows/auto-assign-pr-author.yml rename to .github/workflows/assign-author.yml index df73ac4..466655f 100644 --- a/.github/workflows/auto-assign-pr-author.yml +++ b/.github/workflows/assign-author.yml @@ -12,5 +12,7 @@ permissions: jobs: assign-author: runs-on: ubuntu-latest + steps: - - uses: toshimaru/auto-author-assign@v2.1.0 + - name: Assign author + uses: toshimaru/auto-author-assign@v2.1.0 diff --git a/.github/workflows/draft-release-notes.yml b/.github/workflows/draft-release-notes.yml new file mode 100644 index 0000000..c9e0353 --- /dev/null +++ b/.github/workflows/draft-release-notes.yml @@ -0,0 +1,25 @@ +name: Draft release notes + +on: + push: + tags: + - '*' + branches: + - main + +permissions: + contents: write + +jobs: + release-drafter: + permissions: + contents: write + pull-requests: write + + runs-on: ubuntu-latest + + steps: + - name: Draft release notes + uses: release-drafter/release-drafter@v6 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index e44cd9e..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Release - -on: - push: - tags: - - '*' - branches: - - main - -permissions: - contents: write - -jobs: - release-drafter: - permissions: - contents: write - pull-requests: write - runs-on: ubuntu-latest - steps: - - uses: release-drafter/release-drafter@v6 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - release-php: - if: startsWith(github.ref, 'refs/tags/') - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/cache@v4 - with: - path: /tmp/composer-cache - key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} - - uses: php-actions/composer@v6 - - name: Get Tag Name - id: get-version - run: echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT - - name: Create Release - uses: softprops/action-gh-release@v1 - with: - files: | diff --git a/README.md b/README.md index a089d37..7d7c628 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,12 @@ docker compose exec phpserver composer install --ansi docker compose exec phpserver composer lint ``` +### Lint fix + +```bash +docker compose exec phpserver composer lint-fix +``` + ### Run tests ```bash diff --git a/composer.json b/composer.json index 97cffb7..2799eb3 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "phpmd/phpmd": "^2.13", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^11.0", + "rector/rector": "^1.0.0", "squizlabs/php_codesniffer": "^3", "symfony/process": "^6.4 || ^7.0" }, @@ -43,9 +44,12 @@ "scripts": { "lint": [ "phpcs", - "phpstan" + "phpmd --exclude vendor,tests . text phpmd.xml", + "phpstan", + "rector --clear-cache --dry-run" ], - "lint:fix": [ + "lint-fix": [ + "rector --clear-cache", "phpcbf" ], "test": [ diff --git a/phpmd.xml b/phpmd.xml new file mode 100644 index 0000000..7d3cedb --- /dev/null +++ b/phpmd.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..c088341 --- /dev/null +++ b/rector.php @@ -0,0 +1,66 @@ +paths([ + __DIR__ . '/**', + ]); + + $rectorConfig->sets([ + SetList::PHP_80, + SetList::PHP_81, + SetList::CODE_QUALITY, + SetList::CODING_STYLE, + SetList::DEAD_CODE, + SetList::INSTANCEOF, + SetList::TYPE_DECLARATION, + ]); + + $rectorConfig->skip([ + // Rules added by Rector's rule sets. + ArraySpreadInsteadOfArrayMergeRector::class, + CountArrayToEmptyArrayComparisonRector::class, + DisallowedEmptyRuleFixerRector::class, + InlineArrayReturnAssignRector::class, + NewlineAfterStatementRector::class, + NewlineBeforeNewAssignSetRector::class, + PostIncDecToPreIncDecRector::class, + RemoveAlwaysTrueIfConditionRector::class, + SimplifyEmptyCheckOnEmptyArrayRector::class, + // Dependencies. + '*/vendor/*', + '*/node_modules/*', + '*/tests/*', + ]); + + $rectorConfig->fileExtensions([ + 'php', + 'inc', + ]); + + $rectorConfig->importNames(TRUE, FALSE); + $rectorConfig->importShortClasses(FALSE); +}; diff --git a/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php b/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php index 7d84ca4..14f4616 100644 --- a/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php +++ b/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php @@ -18,56 +18,17 @@ class ScreenshotContextInitializer implements ContextInitializer { - /** - * Screenshot directory name. - * - * @var string - */ - protected $dir; - - /** - * Makes screenshot when fail. - * - * @var bool - */ - protected $fail; - - /** - * Prefix for failed screenshot files. - * - * @var string - */ - private $failPrefix; - - /** - * Purge dir before start test. - * - * @var bool - */ - protected $purge; - - /** - * Check if need to actually purge. - * - * @var bool - */ - protected $needsPurging; - /** * ScreenshotContextInitializer constructor. * - * @param string $dir Screenshot dir. - * @param bool $fail Screenshot when fail. - * @param string $failPrefix File name prefix for a failed test. - * @param bool $purge Purge dir before start script. + * @param string $dir Screenshot dir. + * @param bool $fail Screenshot when fail. + * @param string $failPrefix File name prefix for a failed test. + * @param bool $purge Purge dir before start script. + * @param bool $needsPurging Check if need to actually purge. */ - public function __construct(string $dir, bool $fail, string $failPrefix, bool $purge) + public function __construct(protected string $dir, protected bool $fail, private readonly string $failPrefix, protected bool $purge, protected bool $needsPurging = true) { - $this->needsPurging = true; - $this->dir = $dir; - $this->purge = $purge; - $this->fail = $fail; - $this->failPrefix = $failPrefix; } /** diff --git a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php index d0ac487..1d11a4e 100644 --- a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php +++ b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php @@ -38,25 +38,19 @@ class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContex protected $stepLine; /** - * Screenshot directory name. - * - * @var string + * Makes screenshot when fail. */ - private $dir; + private bool $fail = false; /** - * Makes screenshot when fail. - * - * @var bool + * Screenshot directory name. */ - private $fail; + private string $dir = ''; /** * Prefix for failed screenshot files. - * - * @var string */ - private $failPrefix; + private string $failPrefix = ''; /** * {@inheritdoc} @@ -94,7 +88,6 @@ public function beforeScenarioInit(BeforeScenarioScope $scope): void /** * Init values required for snapshot. * - * @param BeforeStepScope $scope * * @BeforeStep */ @@ -142,7 +135,7 @@ public function iSaveScreenshot($fail = false, $filename = null): void try { $data = $driver->getContent(); - } catch (DriverException $exception) { + } catch (DriverException) { // Do not do anything if the driver does not have any content - most // likely the page has not been loaded yet. return; @@ -159,7 +152,7 @@ public function iSaveScreenshot($fail = false, $filename = null): void // content and screenshot files together by name. $fileName = substr($fileName, 0, -1 * strlen('html')).'png'; $this->saveScreenshotData($fileName, $data); - } catch (UnsupportedDriverActionException $exception) { + } catch (UnsupportedDriverActionException) { // Nothing to do here - drivers without support for screenshots // simply do not have them created. } @@ -190,7 +183,7 @@ public function iSaveSizedScreenshot(string|int $width = 1440, string|int $heigh { try { $this->getSession()->resizeWindow((int) $width, (int) $height, 'current'); - } catch (UnsupportedDriverActionException $exception) { + } catch (UnsupportedDriverActionException) { // Nothing to do here - drivers without resize support may proceed. } $this->iSaveScreenshot(); diff --git a/src/DrevOps/BehatScreenshotExtension/ServiceContainer/BehatScreenshotExtension.php b/src/DrevOps/BehatScreenshotExtension/ServiceContainer/BehatScreenshotExtension.php index d8d9430..e895851 100644 --- a/src/DrevOps/BehatScreenshotExtension/ServiceContainer/BehatScreenshotExtension.php +++ b/src/DrevOps/BehatScreenshotExtension/ServiceContainer/BehatScreenshotExtension.php @@ -7,12 +7,11 @@ namespace DrevOps\BehatScreenshotExtension\ServiceContainer; +use DrevOps\BehatScreenshotExtension\Context\Initializer\ScreenshotContextInitializer; use Behat\Behat\Context\ServiceContainer\ContextExtension; use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface; use Behat\Testwork\ServiceContainer\ExtensionManager; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; -use Symfony\Component\Config\Definition\Builder\NodeBuilder; -use Symfony\Component\Config\Definition\Builder\NodeParentInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -55,14 +54,12 @@ public function initialize(ExtensionManager $extensionManager): void public function configure(ArrayNodeDefinition $builder): void { $definitionChildren = $builder->children(); - if ($definitionChildren instanceof NodeBuilder) { - // @phpstan-ignore-next-line - $definitionChildren - ->scalarNode('dir')->cannotBeEmpty()->defaultValue('%paths.base%/screenshots')->end() - ->scalarNode('fail')->cannotBeEmpty()->defaultValue(true)->end() - ->scalarNode('fail_prefix')->cannotBeEmpty()->defaultValue('failed_')->end() - ->scalarNode('purge')->cannotBeEmpty()->defaultValue(false)->end(); - } + // @phpstan-ignore-next-line + $definitionChildren + ->scalarNode('dir')->cannotBeEmpty()->defaultValue('%paths.base%/screenshots')->end() + ->scalarNode('fail')->cannotBeEmpty()->defaultValue(true)->end() + ->scalarNode('fail_prefix')->cannotBeEmpty()->defaultValue('failed_')->end() + ->scalarNode('purge')->cannotBeEmpty()->defaultValue(false)->end(); } /** @@ -70,7 +67,7 @@ public function configure(ArrayNodeDefinition $builder): void */ public function load(ContainerBuilder $container, array $config): void { - $definition = new Definition('DrevOps\BehatScreenshotExtension\Context\Initializer\ScreenshotContextInitializer', [ + $definition = new Definition(ScreenshotContextInitializer::class, [ $config['dir'], $config['fail'], $config['fail_prefix'],