From 679010d4cda21c954fceb96b91297a6d8ece4c4a Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Thu, 7 Mar 2024 09:29:21 +0200 Subject: [PATCH 1/7] fix: InpsydeTemplates usage --- InpsydeTemplates/ruleset.xml | 4 +--- README.md | 10 ++-------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/InpsydeTemplates/ruleset.xml b/InpsydeTemplates/ruleset.xml index 098918e..56286fb 100644 --- a/InpsydeTemplates/ruleset.xml +++ b/InpsydeTemplates/ruleset.xml @@ -3,8 +3,6 @@ Coding standards for PHP templates. - - - + diff --git a/README.md b/README.md index 9938325..a75af07 100644 --- a/README.md +++ b/README.md @@ -184,8 +184,7 @@ For **notes and configuration** see [`/inpsyde-custom-sniffs.md`](/inpsyde-custo ## Template Rules -The `InpsydeTemplates` ruleset extends the standard `Inpsyde` ruleset with some template-specific -sniffs while disabling some rules that are not useful in templating context. +The `InpsydeTemplates` ruleset extends the standard `Inpsyde` ruleset with some template-specific sniffs. The recommended way to use the `InpsydeTemplates` ruleset is as follows: @@ -196,10 +195,7 @@ The recommended way to use the `InpsydeTemplates` ruleset is as follows: ./templates ./views - - */templates/* - */views/* - + */templates/* @@ -207,8 +203,6 @@ The recommended way to use the `InpsydeTemplates` ruleset is as follows: ``` -The following `Inpsyde` rules are disabled: -* `NoElse` The following templates-specific rules are available: From a6116b775ad5257aa48b657b060bff454219bdc4 Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Thu, 7 Mar 2024 09:37:39 +0200 Subject: [PATCH 2/7] tests: add E2E tests --- composer.json | 3 +- phpunit.xml.dist | 3 + tests/e2e/E2eTest.php | 97 +++++++++++++++++++ tests/e2e/test-package/index.php | 14 +++ tests/e2e/test-package/phpcs.xml | 20 ++++ tests/e2e/test-package/templates/template.php | 15 +++ 6 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 tests/e2e/E2eTest.php create mode 100644 tests/e2e/test-package/index.php create mode 100644 tests/e2e/test-package/phpcs.xml create mode 100644 tests/e2e/test-package/templates/template.php diff --git a/composer.json b/composer.json index fe89f1a..4bb7262 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,8 @@ "psr-4": { "Inpsyde\\CodingStandard\\Tests\\": [ "tests/src/", - "tests/cases/" + "tests/cases/", + "tests/e2e/" ] } }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 826c5fa..0be95d0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,5 +16,8 @@ tests/cases/Helpers + + tests/e2e/E2eTest.php + diff --git a/tests/e2e/E2eTest.php b/tests/e2e/E2eTest.php new file mode 100644 index 0000000..9c1d8b5 --- /dev/null +++ b/tests/e2e/E2eTest.php @@ -0,0 +1,97 @@ +testPackagePath = $libPath . '/tests/e2e/test-package'; + $this->phpCsBinary = $libPath . '/vendor/bin/phpcs'; + } + + public function testInpsydeAndTemplatesRulesets(): void + { + $output = []; + exec( + sprintf( + 'cd %s && %s', + $this->testPackagePath, + $this->phpCsBinary + ), + $output + ); + + /** @var array $output> */ + if ($output[0] !== 'EE 2 / 2 (100%)') { + throw new RuntimeException(implode("\n", $output)); + } + + $json = end($output); + + self::assertSame([ + 'index.php' => [ + [ + 'source' => 'Inpsyde.CodeQuality.NoElse.ElseFound', + 'line' => 12, + ], + [ + 'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped', + 'line' => 13, + ], + ], + 'template.php' => [ + + [ + 'source' => 'WordPress.Security.EscapeOutput.OutputNotEscaped', + 'line' => 12, + ], + [ + 'source' => 'InpsydeTemplates.Formatting.TrailingSemicolon.Found', + 'line' => 12, + ], + [ + 'source' => 'Inpsyde.CodeQuality.DisableCallUserFunc.call_user_func_call_user_func', + 'line' => 15, + ], + + ], + ], $this->phpCsNormalizedOutput($json)); + } + + /** + * @psalm-return array> + */ + private function phpCsNormalizedOutput(string $json): array + { + /** @var array{ + * files: array + * }> + * } $data */ + $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR); + + $result = []; + foreach ($data['files'] as $fileName => $fileData) { + $baseName = basename($fileName); + $result[$baseName] = []; + foreach ($fileData['messages'] as ['source' => $source, 'line' => $line]) { + $result[$baseName][] = ['source' => $source, 'line' => $line]; + } + } + + return $result; + } +} diff --git a/tests/e2e/test-package/index.php b/tests/e2e/test-package/index.php new file mode 100644 index 0000000..9193c39 --- /dev/null +++ b/tests/e2e/test-package/index.php @@ -0,0 +1,14 @@ + 0.5) { + echo esc_html($successMessage); +} else { + echo $errorMessage; +} diff --git a/tests/e2e/test-package/phpcs.xml b/tests/e2e/test-package/phpcs.xml new file mode 100644 index 0000000..70eccce --- /dev/null +++ b/tests/e2e/test-package/phpcs.xml @@ -0,0 +1,20 @@ + + + + ./index.php + ./templates + + + + + + + + */templates/* + + + + */templates/* + + + diff --git a/tests/e2e/test-package/templates/template.php b/tests/e2e/test-package/templates/template.php new file mode 100644 index 0000000..2ebddc0 --- /dev/null +++ b/tests/e2e/test-package/templates/template.php @@ -0,0 +1,15 @@ + + + 0.5) : ?> + + + + Date: Thu, 7 Mar 2024 09:44:46 +0200 Subject: [PATCH 3/7] style: format code --- phpcs.xml | 1 + tests/e2e/E2eTest.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index deef883..d86df83 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -7,6 +7,7 @@ ./Inpsyde/Sniffs ./tests/src ./tests/cases + ./tests/e2e/E2eTest.php