From 334f042844a303c512ff85a650f6aa6b0920cbc0 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 --- composer.json | 2 +- ecs-internal.php | 6 +++ phpstan.neon | 6 ++- tests/Integration/CodingStandardTest.php | 43 +++++++++++++++++++ .../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 ++++++++++++++ 8 files changed, 173 insertions(+), 3 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/composer.json b/composer.json index 3909a6a..38354b5 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "nette/utils": "^3.2", "slevomat/coding-standard": "^8.0", "squizlabs/php_codesniffer": "^3.9", - "symplify/easy-coding-standard": "^12.1.4" + "symplify/easy-coding-standard": "^12.1.5" }, "require-dev": { "ergebnis/composer-normalize": "^2.13.2", 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..42cf1eb --- /dev/null +++ b/tests/Integration/CodingStandardTest.php @@ -0,0 +1,43 @@ +fail('Could not create temporary file'); + } + copy($wrongFile, $fixtureFile); + + shell_exec( + __DIR__ . '/../../vendor/bin/ecs check --no-progress-bar --no-ansi --no-interaction --fix ' . $fixtureFile, + ); + + $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; + } +}