From f9a89eb28d62458e64e103aa9ced861351850f77 Mon Sep 17 00:00:00 2001 From: "tan.nguyen" Date: Mon, 19 Feb 2024 11:35:13 +0700 Subject: [PATCH] Update to scaffold 0.12.1. --- .gitattributes | 23 ++-- .github/PULL_REQUEST_TEMPLATE.md | 9 +- .github/release-drafter.yml | 8 +- ...assign-pr-author.yml => assign-author.yml} | 4 +- .../{release.yml => draft-release-notes.yml} | 9 +- .github/workflows/test-php.yml | 42 +++++++ .github/workflows/test.yml | 29 ----- .gitignore | 3 + README.md | 4 +- composer.json | 9 +- rector.php | 65 ++++++++++ renovate.json | 4 +- .../FormatExtension.php | 33 +++-- .../Printer/PrinterProgressFail.php | 54 +++----- .../features/bootstrap/BehatCliContext.php | 115 ++++++++---------- .../features/bootstrap/BehatCliTrait.php | 24 ++-- 16 files changed, 248 insertions(+), 187 deletions(-) rename .github/workflows/{auto-assign-pr-author.yml => assign-author.yml} (71%) rename .github/workflows/{release.yml => draft-release-notes.yml} (68%) create mode 100644 .github/workflows/test-php.yml delete mode 100644 .github/workflows/test.yml create mode 100644 rector.php diff --git a/.gitattributes b/.gitattributes index f7c7a3b..0938577 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,10 +1,13 @@ -.gitattributes export-ignore -.gitignore export-ignore -.editorconfig export-ignore -.github export-ignore -tests export-ignore -behat.yml export-ignore -phpcs.xml export-ignore -phpmd.xml export-ignore -phpstan.neon export-ignore -renovate.json export-ignore +# Ignore files for distribution archives. + +/.editorconfig export-ignore +/.gitattributes export-ignore +/.github export-ignore +/.gitignore export-ignore +/tests export-ignore +/phpcs.xml export-ignore +/phpmd.xml export-ignore +/phpstan.neon export-ignore +/phpunit.xml export-ignore +/renovate.json export-ignore +/behat.yml export-ignore diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 813aa2e..95cbae2 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,15 +1,12 @@ ## Checklist before requesting a review -- [ ] I have formatted the subject to include ticket number - as `[PRJ-123] Verb in past tense with dot at the end.` +- [ ] 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 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 run new and existing relevant tests locally with my changes, and they passed - [ ] I have provided screenshots, where applicable ## Changed 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/release.yml b/.github/workflows/draft-release-notes.yml similarity index 68% rename from .github/workflows/release.yml rename to .github/workflows/draft-release-notes.yml index c6587e7..5f3bd03 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/draft-release-notes.yml @@ -1,11 +1,11 @@ -name: Release +name: Draft release notes on: push: tags: - '*' branches: - - main + - master permissions: contents: write @@ -15,8 +15,11 @@ jobs: permissions: contents: write pull-requests: write + runs-on: ubuntu-latest + steps: - - uses: release-drafter/release-drafter@v6 + - name: Draft release notes + uses: release-drafter/release-drafter@v6 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/test-php.yml b/.github/workflows/test-php.yml new file mode 100644 index 0000000..fd2fdbd --- /dev/null +++ b/.github/workflows/test-php.yml @@ -0,0 +1,42 @@ +name: Test PHP + +on: + push: + branches: + - master + pull_request: + branches: + - master + - 'feature/**' + +jobs: + test-php: + runs-on: ubuntu-latest + + strategy: + matrix: + php-versions: ['8.1', '8.2', '8.3'] + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Cache Composer dependencies + uses: actions/cache@v4 + with: + path: /tmp/composer-cache + key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + + - name: Install dependencies + run: composer install + + - name: Check coding standards + run: composer lint + + - name: Run tests + run: XDEBUG_MODE=coverage composer test diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index 60329a9..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Test - -on: - pull_request: - branches: - - main - - 'feature/**' - push: - branches: - - main - -jobs: - test-php: - runs-on: ubuntu-latest - strategy: - matrix: - php-versions: ['8.1', '8.2', '8.3'] - steps: - - uses: actions/checkout@v4 - - uses: actions/cache@v4 - with: - path: /tmp/composer-cache - key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} - - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - - run: composer install - - run: composer lint - - run: composer test diff --git a/.gitignore b/.gitignore index 7579f74..87b01ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ +# To ignore OS temporary files use global .gitignore +# https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-computer + vendor composer.lock diff --git a/README.md b/README.md index 7599927..ec5d6b5 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ [![GitHub Issues](https://img.shields.io/github/issues/drevops/behat-format-progress-fail.svg)](https://github.com/drevops/behat-format-progress-fail/issues) [![GitHub Pull Requests](https://img.shields.io/github/issues-pr/drevops/behat-format-progress-fail.svg)](https://github.com/drevops/behat-format-progress-fail/pulls) -[![Test](https://github.com/drevops/behat-format-progress-fail/actions/workflows/test.yml/badge.svg)](https://github.com/drevops/behat-format-progress-fail/actions/workflows/test.yml) +[![Test](https://github.com/drevops/behat-format-progress-fail/actions/workflows/test-php.yml/badge.svg)](https://github.com/drevops/behat-format-progress-fail/actions/workflows/test-php.yml) ![GitHub release (latest by date)](https://img.shields.io/github/v/release/drevops/behat-format-progress-fail) ![LICENSE](https://img.shields.io/github/license/drevops/behat-format-progress-fail) ![Renovate](https://img.shields.io/badge/renovate-enabled-green?logo=renovatebot) @@ -63,7 +63,7 @@ default: ```bash composer lint -composer lint:fix +composer lint-fix ``` ### Run tests diff --git a/composer.json b/composer.json index 63b0d55..b1a18bc 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,7 @@ "phpmd/phpmd": "^2.14", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^10.0", + "rector/rector": "^1.0.0", "squizlabs/php_codesniffer": "^3", "symfony/process": "^6 || ^7.0" }, @@ -43,9 +44,13 @@ "lint": [ "phpcs", "phpmd --exclude vendor . text phpmd.xml", - "phpstan" + "phpstan", + "rector --clear-cache --dry-run" + ], + "lint-fix": [ + "rector --clear-cache", + "phpcbf" ], - "lint:fix": "phpcbf", "test": "vendor/bin/behat" } } diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..a158b55 --- /dev/null +++ b/rector.php @@ -0,0 +1,65 @@ +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/*', + ]); + + $rectorConfig->fileExtensions([ + 'php', + 'inc', + ]); + + $rectorConfig->importNames(TRUE, FALSE); + $rectorConfig->importShortClasses(FALSE); +}; diff --git a/renovate.json b/renovate.json index 3e19ab4..27da12f 100644 --- a/renovate.json +++ b/renovate.json @@ -1,8 +1,8 @@ { "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "automerge": true, "dependencyDashboard": true, "extends": [ "config:base" - ], - "automerge": true + ] } diff --git a/src/DrevOps/BehatFormatProgressFail/FormatExtension.php b/src/DrevOps/BehatFormatProgressFail/FormatExtension.php index 6dc5065..69e83e9 100644 --- a/src/DrevOps/BehatFormatProgressFail/FormatExtension.php +++ b/src/DrevOps/BehatFormatProgressFail/FormatExtension.php @@ -7,6 +7,16 @@ namespace DrevOps\BehatFormatProgressFail; +use Behat\Behat\Output\Node\EventListener\AST\StepListener; +use DrevOps\BehatFormatProgressFail\Printer\PrinterProgressFail; +use Behat\Testwork\Output\NodeEventListeningFormatter; +use Behat\Testwork\Output\Node\EventListener\ChainEventListener; +use Behat\Behat\Output\Node\EventListener\Statistics\StatisticsListener; +use Behat\Behat\Output\Node\EventListener\Statistics\ScenarioStatsListener; +use Behat\Behat\Output\Node\EventListener\Statistics\StepStatsListener; +use Behat\Behat\Output\Node\EventListener\Statistics\HookStatsListener; +use Behat\Testwork\Output\Printer\StreamOutputPrinter; +use Behat\Behat\Output\Printer\ConsoleOutputFactory; use Behat\Testwork\Exception\ServiceContainer\ExceptionExtension; use Behat\Testwork\Output\ServiceContainer\OutputExtension; use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface; @@ -76,14 +86,14 @@ public function configure(ArrayNodeDefinition $builder): void public function load(ContainerBuilder $container, array $config): void { $definition = new Definition( - 'Behat\Behat\Output\Node\EventListener\AST\StepListener', [ + StepListener::class, [ new Reference('output.printer.'.$config['name']), ] ); $container->setDefinition(self::ROOT_LISTENER_ID, $definition); $definition = new Definition( - 'DrevOps\BehatFormatProgressFail\Printer\PrinterProgressFail', [ + PrinterProgressFail::class, [ new Reference(self::RESULT_TO_STRING_CONVERTER_ID), $config['base_path'], ] @@ -93,17 +103,17 @@ public function load(ContainerBuilder $container, array $config): void ); $definition = new Definition( - 'Behat\Testwork\Output\NodeEventListeningFormatter', [ + NodeEventListeningFormatter::class, [ $config['name'], 'Prints one character per step and fail view pretty.', ['timer' => true], $this->createOutputPrinterDefinition(), new Definition( - 'Behat\Testwork\Output\Node\EventListener\ChainEventListener', [ + ChainEventListener::class, [ [ new Reference(self::ROOT_LISTENER_ID), new Definition( - 'Behat\Behat\Output\Node\EventListener\Statistics\StatisticsListener', [ + StatisticsListener::class, [ new Reference('output.progress.statistics'), new Reference( 'output.node.printer.progress.statistics' @@ -111,12 +121,12 @@ public function load(ContainerBuilder $container, array $config): void ] ), new Definition( - 'Behat\Behat\Output\Node\EventListener\Statistics\ScenarioStatsListener', [ + ScenarioStatsListener::class, [ new Reference('output.progress.statistics'), ] ), new Definition( - 'Behat\Behat\Output\Node\EventListener\Statistics\StepStatsListener', [ + StepStatsListener::class, [ new Reference('output.progress.statistics'), new Reference( ExceptionExtension::PRESENTER_ID @@ -124,7 +134,7 @@ public function load(ContainerBuilder $container, array $config): void ] ), new Definition( - 'Behat\Behat\Output\Node\EventListener\Statistics\HookStatsListener', [ + HookStatsListener::class, [ new Reference('output.progress.statistics'), new Reference( ExceptionExtension::PRESENTER_ID @@ -148,13 +158,14 @@ public function load(ContainerBuilder $container, array $config): void * Creates output printer definition. * * @return Definition + * The output printer definition. */ - protected function createOutputPrinterDefinition() + protected function createOutputPrinterDefinition(): Definition { return new Definition( - 'Behat\Testwork\Output\Printer\StreamOutputPrinter', [ + StreamOutputPrinter::class, [ new Definition( - 'Behat\Behat\Output\Printer\ConsoleOutputFactory' + ConsoleOutputFactory::class ), ] ); diff --git a/src/DrevOps/BehatFormatProgressFail/Printer/PrinterProgressFail.php b/src/DrevOps/BehatFormatProgressFail/Printer/PrinterProgressFail.php index ca29751..acfc484 100644 --- a/src/DrevOps/BehatFormatProgressFail/Printer/PrinterProgressFail.php +++ b/src/DrevOps/BehatFormatProgressFail/Printer/PrinterProgressFail.php @@ -23,31 +23,13 @@ class PrinterProgressFail implements StepPrinter { - /** - * @var ResultToStringConverter $resultConverter - */ - private ResultToStringConverter $resultConverter; - - /** - * @var int $stepsPrinted - */ private int $stepsPrinted = 0; - /** - * @var string $basePath - */ - private string $basePath; - /** * Initializes printer. - * - * @param ResultToStringConverter $resultConverter - * @param string $basePath */ - public function __construct(ResultToStringConverter $resultConverter, string $basePath = '') + public function __construct(private readonly ResultToStringConverter $resultConverter, private readonly string $basePath = '') { - $this->resultConverter = $resultConverter; - $this->basePath = $basePath; } /** @@ -61,16 +43,16 @@ public function printStep(Formatter $formatter, Scenario $scenario, StepNode $st switch ($result->getResultCode()) { case TestResult::PASSED: - $printer->write("{+$style}.{-$style}"); + $printer->write(sprintf('{+%s}.{-%s}', $style, $style)); break; case TestResult::SKIPPED: - $printer->write("{+$style}-{-$style}"); + $printer->write(sprintf('{+%s}-{-%s}', $style, $style)); break; case TestResult::PENDING: - $printer->write("{+$style}P{-$style}"); + $printer->write(sprintf('{+%s}P{-%s}', $style, $style)); break; case StepResult::UNDEFINED: - $printer->write("{+$style}U{-$style}"); + $printer->write(sprintf('{+%s}U{-%s}', $style, $style)); break; case TestResult::FAILED: $printer->write($this->printFailure($result, $step)); @@ -85,10 +67,8 @@ public function printStep(Formatter $formatter, Scenario $scenario, StepNode $st /** * Creates information about fail step. * - * @param StepResult $result - * @param StepNode $step - * * @return string + * Information about fail step. */ protected function printFailure(StepResult $result, StepNode $step): string { @@ -96,7 +76,7 @@ protected function printFailure(StepResult $result, StepNode $step): string // Return default format for any non-executed step results. if (!$result instanceof ExecutedStepResult) { - return "{+$style}F{-$style}"; + return sprintf('{+%s}F{-%s}', $style, $style); } $output = ''; @@ -111,14 +91,14 @@ protected function printFailure(StepResult $result, StepNode $step): string $fileLine = $step->getLine(); $output .= PHP_EOL; - $output .= "{+$style}--- FAIL ---{-$style}"; + $output .= sprintf('{+%s}--- FAIL ---{-%s}', $style, $style); $output .= PHP_EOL; - $output .= sprintf(" {+$style}%s %s{-$style} {+comment}# (%s):%s{-comment}", $step->getKeyword(), $step->getText(), $fileName, $fileLine); + $output .= sprintf(sprintf(' {+%s}%%s %%s{-%s} {+comment}# (%%s):%%s{-comment}', $style, $style), $step->getKeyword(), $step->getText(), $fileName, $fileLine); $output .= PHP_EOL; $stepArguments = $step->getArguments(); - $stepArguments = array_map(function ($item) { + $stepArguments = array_map(static function ($item) { if (method_exists($item, '__toString')) { return $item->__toString(); } @@ -127,32 +107,30 @@ protected function printFailure(StepResult $result, StepNode $step): string }, $stepArguments); $stepArguments = array_filter($stepArguments); if (count($stepArguments) > 0) { - $output .= sprintf(" {+$style}%s{-$style}", implode(PHP_EOL, array_filter($stepArguments))); + $output .= sprintf(sprintf(' {+%s}%%s{-%s}', $style, $style), implode(PHP_EOL, array_filter($stepArguments))); $output .= PHP_EOL; } $exception = $result->getException(); if ($exception) { - $output .= sprintf(" {+$style}%s{-$style}", $exception->getMessage()); + $output .= sprintf(sprintf(' {+%s}%%s{-%s}', $style, $style), $exception->getMessage()); $output .= PHP_EOL; } - $output .= "{+$style}------------{-$style}"; - $output .= PHP_EOL; + $output .= sprintf('{+%s}------------{-%s}', $style, $style); - return sprintf("%s", $output); + return $output.PHP_EOL; } /** * Transforms path to relative. * - * @param string $path - * * @return string + * Relative path. */ protected function relativizePaths(string $path): string { - return !$this->basePath ? $path : str_replace( + return $this->basePath === '' || $this->basePath === '0' ? $path : str_replace( $this->basePath.DIRECTORY_SEPARATOR, '', $path ); } diff --git a/tests/behat/features/bootstrap/BehatCliContext.php b/tests/behat/features/bootstrap/BehatCliContext.php index 91c38f3..94be59b 100644 --- a/tests/behat/features/bootstrap/BehatCliContext.php +++ b/tests/behat/features/bootstrap/BehatCliContext.php @@ -39,22 +39,10 @@ class BehatCliContext implements Context { use BehatCliTrait; - /** - * @var string - */ - private $phpBin; - /** - * @var Process - */ - private $process; - /** - * @var string - */ - private $workingDir; - /** - * @var string - */ - private $options = '--format-settings=\'{"timer": false}\' --no-interaction'; + private ?string $phpBin = null; + private ?Process $process = null; + private ?string $workingDir = null; + private string $options = '--format-settings=\'{"timer": false}\' --no-interaction'; /** * @var array */ @@ -70,7 +58,7 @@ class BehatCliContext implements Context * @BeforeSuite * @AfterSuite */ - public static function cleanTestFolders() + public static function cleanTestFolders(): void { if (is_dir($dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'behat')) { self::clearDirectory($dir); @@ -82,7 +70,7 @@ public static function cleanTestFolders() * * @BeforeScenario */ - public function prepareTestFolders() + public function prepareTestFolders(): void { $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'behat' . DIRECTORY_SEPARATOR . md5(microtime() . rand(0, 10000)); @@ -106,7 +94,7 @@ public function prepareTestFolders() * @param string $filename name of the file (relative path) * @param PyStringNode $content PyString string instance */ - public function aFileNamedWith($filename, PyStringNode $content) + public function aFileNamedWith(string $filename, PyStringNode $content): void { $content = strtr((string) $content, array("'''" => '"""')); $this->createFile($this->workingDir . '/' . $filename, $content); @@ -119,7 +107,7 @@ public function aFileNamedWith($filename, PyStringNode $content) * * @param string $filename name of the file (relative path) */ - public function aFileNamed($filename) + public function aFileNamed(string $filename): void { $this->createFile($this->workingDir . '/' . $filename, ''); } @@ -129,7 +117,7 @@ public function aFileNamed($filename) * * @Given /^(?:there is )?a some feature context$/ */ - public function aNoopFeatureContext() + public function aNoopFeatureContext(): void { $filename = 'features/bootstrap/FeatureContext.php'; $content = <<<'EOL' @@ -149,7 +137,7 @@ class FeatureContext implements Context * * @Given /^(?:there is )?a some feature scenarios/ */ - public function aNoopFeature() + public function aNoopFeature(): void { $filename = 'features/bootstrap/FeatureContext.php'; $content = <<<'EOL' @@ -164,10 +152,8 @@ public function aNoopFeature() * Moves user to the specified path. * * @Given /^I am in the "([^"]*)" path$/ - * - * @param string $path */ - public function iAmInThePath($path) + public function iAmInThePath(string $path): void { $this->moveToNewPath($path); } @@ -176,10 +162,8 @@ public function iAmInThePath($path) * Checks whether a file at provided path exists. * * @Given /^file "([^"]*)" should exist$/ - * - * @param string $path */ - public function fileShouldExist($path) + public function fileShouldExist(string $path): void { Assert::assertFileExists($this->workingDir . DIRECTORY_SEPARATOR . $path); } @@ -188,10 +172,8 @@ public function fileShouldExist($path) * Sets specified ENV variable * * @When /^"BEHAT_PARAMS" environment variable is set to:$/ - * - * @param PyStringNode $value */ - public function iSetEnvironmentVariable(PyStringNode $value) + public function iSetEnvironmentVariable(PyStringNode $value): void { $this->env = array('BEHAT_PARAMS' => (string) $value); } @@ -203,19 +185,19 @@ public function iSetEnvironmentVariable(PyStringNode $value) * * @param string $argumentsString */ - public function iRunBehat($argumentsString = '') + public function iRunBehat($argumentsString = ''): void { - $argumentsString = strtr($argumentsString, array('\'' => '"')); + $argumentsString = strtr($argumentsString, array("'" => '"')); $cmd = sprintf( '%s %s %s %s', $this->phpBin, - escapeshellarg(BEHAT_BIN_PATH), + escapeshellarg((string) BEHAT_BIN_PATH), $argumentsString, - strtr($this->options, array('\'' => '"', '"' => '\"')) + strtr($this->options, array("'" => '"', '"' => '\"')) ); - if (method_exists('\\Symfony\\Component\\Process\\Process', 'fromShellCommandline')) { + if (method_exists(Process::class, 'fromShellCommandline')) { $this->process = Process::fromShellCommandline($cmd); } else { // BC layer for symfony/process 4.1 and older @@ -250,7 +232,7 @@ public function iRunBehat($argumentsString = '') * @param string $answerString * @param string $argumentsString */ - public function iRunBehatInteractively($answerString, $argumentsString) + public function iRunBehatInteractively($answerString, $argumentsString): void { $this->env['SHELL_INTERACTIVE'] = true; @@ -265,7 +247,7 @@ public function iRunBehatInteractively($answerString, $argumentsString) * * @When /^I run behat in debug mode$/ */ - public function iRunBehatInDebugMode() + public function iRunBehatInDebugMode(): void { $this->options = ''; $this->iRunBehat('--debug'); @@ -279,7 +261,7 @@ public function iRunBehatInDebugMode() * @param string $success "fail" or "pass" * @param PyStringNode $text PyString text instance */ - public function itShouldPassWith($success, PyStringNode $text) + public function itShouldPassWith($success, PyStringNode $text): void { $this->itShouldFail($success); $this->theOutputShouldContain($text); @@ -292,7 +274,7 @@ public function itShouldPassWith($success, PyStringNode $text) * * @param string $success "fail" or "pass" */ - public function itShouldPassWithNoOutput($success) + public function itShouldPassWithNoOutput($success): void { $this->itShouldFail($success); Assert::assertEmpty($this->getOutput()); @@ -306,7 +288,7 @@ public function itShouldPassWithNoOutput($success) * @param string $path file path * @param PyStringNode $text file content */ - public function fileShouldContain($path, PyStringNode $text) + public function fileShouldContain($path, PyStringNode $text): void { $path = $this->workingDir . '/' . $path; Assert::assertFileExists($path); @@ -328,7 +310,7 @@ public function fileShouldContain($path, PyStringNode $text) * @param string $path file path * @param PyStringNode $text file content */ - public function fileXmlShouldBeLike($path, PyStringNode $text) + public function fileXmlShouldBeLike($path, PyStringNode $text): void { $path = $this->workingDir . '/' . $path; Assert::assertFileExists($path); @@ -352,15 +334,15 @@ public function fileXmlShouldBeLike($path, PyStringNode $text) * * @param PyStringNode $text PyString text instance */ - public function theOutputShouldContain(PyStringNode $text) + public function theOutputShouldContain(PyStringNode $text): void { Assert::assertStringContainsString($this->getExpectedOutput($text), $this->getOutput()); } - private function getExpectedOutput(PyStringNode $expectedText) + private function getExpectedOutput(PyStringNode $expectedText): string|array|null { $text = strtr($expectedText, array( - '\'\'\'' => '"""', + "'''" => '"""', '%%TMP_DIR%%' => sys_get_temp_dir() . DIRECTORY_SEPARATOR, '%%WORKING_DIR%%' => realpath($this->workingDir . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR, '%%DS%%' => DIRECTORY_SEPARATOR, @@ -369,26 +351,26 @@ private function getExpectedOutput(PyStringNode $expectedText) // windows path fix if ('/' !== DIRECTORY_SEPARATOR) { $text = preg_replace_callback( - '/[ "]features\/[^\n "]+/', function ($matches) { - return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); - }, $text + '/[ "]features\/[^\n "]+/', static function (array $matches) : string|array { + return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); + }, $text ); $text = preg_replace_callback( - '/\features\/[^\<]+/', function ($matches) { - return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); - }, $text + '/\features\/[^\<]+/', static function (array $matches) : string|array { + return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); + }, (string) $text ); $text = preg_replace_callback( - '/\+[fd] [^ ]+/', function ($matches) { - return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); - }, $text + '/\+[fd] [^ ]+/', static function (array $matches) : string|array { + return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); + }, (string) $text ); // error stacktrace $text = preg_replace_callback( - '/#\d+ [^:]+:/', function ($matches) { - return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); - }, $text + '/#\d+ [^:]+:/', static function (array $matches) : string|array { + return str_replace('/', DIRECTORY_SEPARATOR, $matches[0]); + }, (string) $text ); } @@ -402,7 +384,7 @@ private function getExpectedOutput(PyStringNode $expectedText) * * @param string $success "fail" or "pass" */ - public function itShouldFail($success) + public function itShouldFail($success): void { if ('fail' === $success) { if (0 === $this->getExitCode()) { @@ -424,10 +406,9 @@ public function itShouldFail($success) * * @Then /^the file "([^"]+)" should be a valid document according to "([^"]+)"$/ * - * @param string $xmlFile * @param string $schemaPath relative to features/bootstrap/schema */ - public function xmlShouldBeValid($xmlFile, $schemaPath) + public function xmlShouldBeValid(string $xmlFile, string $schemaPath): void { $dom = new DomDocument(); $dom->load($this->workingDir . '/' . $xmlFile); @@ -435,12 +416,12 @@ public function xmlShouldBeValid($xmlFile, $schemaPath) $dom->schemaValidate(__DIR__ . '/schema/' . $schemaPath); } - private function getExitCode() + private function getExitCode(): ?int { return $this->process->getExitCode(); } - private function getOutput() + private function getOutput(): string { $output = $this->process->getErrorOutput() . $this->process->getOutput(); @@ -459,10 +440,10 @@ private function getOutput() $output = str_replace('Warning: Undefined array key','Notice: Undefined offset:', $output); $output = preg_replace('/Class "([^"]+)" not found/', 'Class \'$1\' not found', $output); - return trim(preg_replace("/ +$/m", '', $output)); + return trim((string) preg_replace("/ +$/m", '', (string) $output)); } - private function createFile($filename, $content) + private function createFile(string $filename, string $content): void { $path = dirname($filename); $this->createDirectory($path); @@ -470,14 +451,14 @@ private function createFile($filename, $content) file_put_contents($filename, $content); } - private function createDirectory($path) + private function createDirectory(string $path): void { if (!is_dir($path)) { mkdir($path, 0777, true); } } - private function moveToNewPath($path) + private function moveToNewPath(string $path): void { $newWorkingDir = $this->workingDir .'/' . $path; if (!file_exists($newWorkingDir)) { @@ -487,7 +468,7 @@ private function moveToNewPath($path) $this->workingDir = $newWorkingDir; } - private static function clearDirectory($path) + private static function clearDirectory(string $path): void { $files = scandir($path); array_shift($files); diff --git a/tests/behat/features/bootstrap/BehatCliTrait.php b/tests/behat/features/bootstrap/BehatCliTrait.php index 69c6154..432ac25 100644 --- a/tests/behat/features/bootstrap/BehatCliTrait.php +++ b/tests/behat/features/bootstrap/BehatCliTrait.php @@ -14,23 +14,21 @@ trait BehatCliTrait /** * @AfterScenario */ - public function behatCliAfterScenarioPrintOutput(AfterScenarioScope $scope) + public function behatCliAfterScenarioPrintOutput(AfterScenarioScope $scope): void { - if ($scope->getFeature()->hasTag('behatcli')) { - if (static::behatCliIsDebug()) { - print "-------------------- OUTPUT START --------------------".PHP_EOL; - print PHP_EOL; - print $this->getOutput(); - print PHP_EOL; - print "-------------------- OUTPUT FINISH -------------------".PHP_EOL; - } + if ($scope->getFeature()->hasTag('behatcli') && static::behatCliIsDebug()) { + print "-------------------- OUTPUT START --------------------".PHP_EOL; + print PHP_EOL; + print $this->getOutput(); + print PHP_EOL; + print "-------------------- OUTPUT FINISH -------------------".PHP_EOL; } } /** * Helper to print file comments. */ - protected static function behatCliPrintFileContents($filename, $title = '') + protected static function behatCliPrintFileContents(string $filename, $title = '') { if (!is_readable($filename)) { throw new \RuntimeException(sprintf('Unable to access file "%s"', $filename)); @@ -38,11 +36,11 @@ protected static function behatCliPrintFileContents($filename, $title = '') $content = file_get_contents($filename); - print "-------------------- $title START --------------------".PHP_EOL; + print sprintf('-------------------- %s START --------------------', $title).PHP_EOL; print $filename.PHP_EOL; print_r($content); print PHP_EOL; - print "-------------------- $title FINISH --------------------".PHP_EOL; + print sprintf('-------------------- %s FINISH --------------------', $title).PHP_EOL; } /** @@ -51,7 +49,7 @@ protected static function behatCliPrintFileContents($filename, $title = '') * @return bool * TRUE to see debug messages for this trait. */ - protected static function behatCliIsDebug() + protected static function behatCliIsDebug(): string|false { return getenv('BEHAT_CLI_DEBUG'); }