From b8f6fefd5b37fef40d9cc86684732ebd669e2768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Machulda?= Date: Thu, 25 Apr 2024 15:10:17 +0200 Subject: [PATCH] Chore: Add basic 'integration' tests to ensure code style is being properly checked --- ecs-internal.php | 6 +++ phpstan.neon | 6 ++- tests/Integration/CodingStandardTest.php | 45 +++++++++++++++++++ .../Fixtures/Basic.correct.php.inc | 30 +++++++++++++ .../Integration/Fixtures/Basic.wrong.php.inc | 26 +++++++++++ .../Fixtures/NewPhpFeatures.correct.php.inc | 30 +++++++++++++ .../Fixtures/NewPhpFeatures.wrong.php.inc | 33 ++++++++++++++ 7 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 tests/Integration/CodingStandardTest.php create mode 100644 tests/Integration/Fixtures/Basic.correct.php.inc create mode 100644 tests/Integration/Fixtures/Basic.wrong.php.inc create mode 100644 tests/Integration/Fixtures/NewPhpFeatures.correct.php.inc create mode 100644 tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc diff --git a/ecs-internal.php b/ecs-internal.php index 4a67d8c..ac83458 100644 --- a/ecs-internal.php +++ b/ecs-internal.php @@ -1,5 +1,6 @@ withConfiguredRule( LineLengthFixer::class, ['line_length' => 120, 'break_long_lines' => true, 'inline_short_lines' => false], + ) + ->withSkip( + [ + ForbiddenFunctionsSniff::class => ['tests/Integration/CodingStandardTest.php'], + ], ); diff --git a/phpstan.neon b/phpstan.neon index 42705fa..817d6d8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,6 +7,8 @@ parameters: - vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/autoload.php - vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/src/Util/Tokens.php ignoreErrors: - - message: '#Parameter \#1 \$code of static method PhpCsFixer\\Tokenizer\\Tokens::fromCode\(\) expects string, string\|false given#' - path: %currentWorkingDirectory%/tests/Fixer/SpecifyArgSeparatorFixerTest.php + - path: %currentWorkingDirectory%/tests/Fixer/SpecifyArgSeparatorFixerTest.php + message: '#Parameter \#1 \$code of static method PhpCsFixer\\Tokenizer\\Tokens::fromCode\(\) expects string, string\|false given#' + - path: %currentWorkingDirectory%/tests/Integration/CodingStandardTest.php + message: '#Parameter \#2 \$actualString of method PHPUnit\\Framework\\Assert::assertStringEqualsFile\(\) expects string, string\|false given#' checkMissingIterableValueType: false diff --git a/tests/Integration/CodingStandardTest.php b/tests/Integration/CodingStandardTest.php new file mode 100644 index 0000000..3cbc877 --- /dev/null +++ b/tests/Integration/CodingStandardTest.php @@ -0,0 +1,45 @@ +fail('Could not create temporary file'); + } + copy($wrongFile, $fixtureFile); + + // @codingStandardsIgnoreStart + shell_exec( + __DIR__ . '/../../vendor/bin/ecs check --no-progress-bar --no-ansi --no-interaction --fix ' . $fixtureFile, + ); + // @codingStandardsIgnoreEnd + + $this->assertStringEqualsFile($correctFile, file_get_contents($fixtureFile)); + unlink($fixtureFile); + } + + public function provideFilesToFix(): array + { + return [ + 'Basic' => [__DIR__ . '/Fixtures/Basic.wrong.php.inc', __DIR__ . '/Fixtures/Basic.correct.php.inc'], + 'NewPhpFeatures' => [ + __DIR__ . '/Fixtures/NewPhpFeatures.wrong.php.inc', + __DIR__ . '/Fixtures/NewPhpFeatures.correct.php.inc', + ], + ]; + } +} diff --git a/tests/Integration/Fixtures/Basic.correct.php.inc b/tests/Integration/Fixtures/Basic.correct.php.inc new file mode 100644 index 0000000..922a41a --- /dev/null +++ b/tests/Integration/Fixtures/Basic.correct.php.inc @@ -0,0 +1,30 @@ +mayReturnDateTimeOrNull(); + $timestamp = $dateOrNull?->getTimestamp(); // RequireNullSafeObjectOperatorSniff + + return $foo; + } + + public function mayReturnDateTimeOrNull(): ?\DateTime + { + return null; + } +} diff --git a/tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc b/tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc new file mode 100644 index 0000000..49d1d08 --- /dev/null +++ b/tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc @@ -0,0 +1,33 @@ +someString = $someString; + } + + public function php80features( + string | bool $foo, // UnionTypeHintFormatSniff + int $bar // RequireTrailingCommaInDeclarationSniff + ): string | bool { + $value = mt_rand( + 0, + $bar // RequireTrailingCommaInCallSniff + ); + + $dateOrNull = $this->mayReturnDateTimeOrNull(); + $timestamp = $dateOrNull !== null ? $dateOrNull->getTimestamp() : null; // RequireNullSafeObjectOperatorSniff + + return $foo; + } + + public function mayReturnDateTimeOrNull(): ?\DateTime + { + return null; + } +}