Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Add basic 'integration' tests to ensure code style is being properly checked #92

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
}