Skip to content

Commit

Permalink
Chore: Add basic 'integration' tests to ensure code style is being pr…
Browse files Browse the repository at this point in the history
…operly checked
  • Loading branch information
OndraM committed Apr 25, 2024
1 parent 48837fb commit 334f042
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions ecs-internal.php
@@ -1,5 +1,6 @@
<?php declare(strict_types=1);

use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer;
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;
Expand All @@ -19,4 +20,9 @@
->withConfiguredRule(
LineLengthFixer::class,
['line_length' => 120, 'break_long_lines' => true, 'inline_short_lines' => false],
)
->withSkip(
[
ForbiddenFunctionsSniff::class => ['tests/Integration/CodingStandardTest.php'],
],
);
6 changes: 4 additions & 2 deletions phpstan.neon
Expand Up @@ -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
43 changes: 43 additions & 0 deletions tests/Integration/CodingStandardTest.php
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Lmc\CodingStandard\Integration;

use PHPUnit\Framework\TestCase;

/**
* @coversNothing
*/
class CodingStandardTest extends TestCase
{
/**
* @test
* @dataProvider provideFilesToFix
*/
public function shouldFixFile(string $wrongFile, string $correctFile): void
{
// copy wrongFile to a new temporary file
$fixtureFile = tempnam(sys_get_temp_dir(), 'ecs-test');
if ($fixtureFile === false) {
$this->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',
],
];
}
}
30 changes: 30 additions & 0 deletions tests/Integration/Fixtures/Basic.correct.php.inc
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

class Basic
{
public const FOO = 'foo'; // ClassAttributesSeparationFixer

public function isEqual($a, $b) // VisibilityRequiredFixer
{
// TrimArraySpacesFixer
$fooBar = ['a', 'b'];
// MbStrFunctionsFixer
$bazLength = mb_strlen('baz');
// LambdaNotUsedImportFixer
$lambdaWithUnusedImport = function () { return 'foo'; };
// NoUselessSprintfFixer
$uselessSprintf = 'bar';
// SingleSpaceAfterConstructFixer
if ($a == $b) {
return true;
}

return false; // BlankLineBeforeStatementFixer
}

public function fooBar(mixed $foo): mixed
{
// TernaryToElvisOperatorFixer
return ($foo ?: 'not true');
}
}
26 changes: 26 additions & 0 deletions tests/Integration/Fixtures/Basic.wrong.php.inc
@@ -0,0 +1,26 @@
<?php

class Basic
{
const FOO = 'foo'; // ClassAttributesSeparationFixer
function isEqual($a, $b) // VisibilityRequiredFixer
{
// TrimArraySpacesFixer
$fooBar = [ 'a', 'b'];
// MbStrFunctionsFixer
$bazLength = strlen('baz');
// LambdaNotUsedImportFixer
$lambdaWithUnusedImport = function () use ($fooBar) { return 'foo'; };
// NoUselessSprintfFixer
$uselessSprintf = sprintf('bar');
// SingleSpaceAfterConstructFixer
if ($a == $b) { return true; }
return false; // BlankLineBeforeStatementFixer
}

public function fooBar(mixed $foo): mixed
{
// TernaryToElvisOperatorFixer
return ($foo ? $foo : 'not true');
}
}
30 changes: 30 additions & 0 deletions tests/Integration/Fixtures/NewPhpFeatures.correct.php.inc
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace Lmc\CodingStandard\Integration\Fixtures;

class NewPhpFeatures
{
public function __construct(private string $someString) // RequireConstructorPropertyPromotionSniff
{
}

public function php80features(
string|bool $foo, // UnionTypeHintFormatSniff
int $bar, // RequireTrailingCommaInDeclarationSniff
): string|bool {
$value = mt_rand(
0,
$bar, // RequireTrailingCommaInCallSniff
);

$dateOrNull = $this->mayReturnDateTimeOrNull();
$timestamp = $dateOrNull?->getTimestamp(); // RequireNullSafeObjectOperatorSniff

return $foo;
}

public function mayReturnDateTimeOrNull(): ?\DateTime
{
return null;
}
}
33 changes: 33 additions & 0 deletions tests/Integration/Fixtures/NewPhpFeatures.wrong.php.inc
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace Lmc\CodingStandard\Integration\Fixtures;

class NewPhpFeatures
{
private string $someString;

public function __construct(string $someString) // RequireConstructorPropertyPromotionSniff
{
$this->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;
}
}

0 comments on commit 334f042

Please sign in to comment.