Skip to content

Commit 728a7ae

Browse files
authored
Merge pull request #92 from lmc-eu/feature/integration-tests
Chore: Add basic 'integration' tests to ensure code style is being properly checked
2 parents 29ade8c + cde08b1 commit 728a7ae

8 files changed

+173
-3
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"nette/utils": "^3.2",
1616
"slevomat/coding-standard": "^8.0",
1717
"squizlabs/php_codesniffer": "^3.9",
18-
"symplify/easy-coding-standard": "^12.1.4"
18+
"symplify/easy-coding-standard": "^12.1.5"
1919
},
2020
"require-dev": {
2121
"ergebnis/composer-normalize": "^2.13.2",

ecs-internal.php

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php declare(strict_types=1);
22

3+
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\ForbiddenFunctionsSniff;
34
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer;
45
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
56
use Symplify\EasyCodingStandard\Config\ECSConfig;
@@ -19,4 +20,9 @@
1920
->withConfiguredRule(
2021
LineLengthFixer::class,
2122
['line_length' => 120, 'break_long_lines' => true, 'inline_short_lines' => false],
23+
)
24+
->withSkip(
25+
[
26+
ForbiddenFunctionsSniff::class => ['tests/Integration/CodingStandardTest.php'],
27+
],
2228
);

phpstan.neon

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ parameters:
77
- vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/autoload.php
88
- vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/src/Util/Tokens.php
99
ignoreErrors:
10-
- message: '#Parameter \#1 \$code of static method PhpCsFixer\\Tokenizer\\Tokens::fromCode\(\) expects string, string\|false given#'
11-
path: %currentWorkingDirectory%/tests/Fixer/SpecifyArgSeparatorFixerTest.php
10+
- path: %currentWorkingDirectory%/tests/Fixer/SpecifyArgSeparatorFixerTest.php
11+
message: '#Parameter \#1 \$code of static method PhpCsFixer\\Tokenizer\\Tokens::fromCode\(\) expects string, string\|false given#'
12+
- path: %currentWorkingDirectory%/tests/Integration/CodingStandardTest.php
13+
message: '#Parameter \#2 \$actualString of method PHPUnit\\Framework\\Assert::assertStringEqualsFile\(\) expects string, string\|false given#'
1214
checkMissingIterableValueType: false
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lmc\CodingStandard\Integration;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* @coversNothing
9+
*/
10+
class CodingStandardTest extends TestCase
11+
{
12+
/**
13+
* @test
14+
* @dataProvider provideFilesToFix
15+
*/
16+
public function shouldFixFile(string $wrongFile, string $correctFile): void
17+
{
18+
// copy wrongFile to a new temporary file
19+
$fixtureFile = tempnam(sys_get_temp_dir(), 'ecs-test');
20+
if ($fixtureFile === false) {
21+
$this->fail('Could not create temporary file');
22+
}
23+
copy($wrongFile, $fixtureFile);
24+
25+
shell_exec(
26+
__DIR__ . '/../../vendor/bin/ecs check --no-progress-bar --no-ansi --no-interaction --fix ' . $fixtureFile,
27+
);
28+
29+
$this->assertStringEqualsFile($correctFile, file_get_contents($fixtureFile));
30+
unlink($fixtureFile);
31+
}
32+
33+
public function provideFilesToFix(): array
34+
{
35+
return [
36+
'Basic' => [__DIR__ . '/Fixtures/Basic.wrong.php.inc', __DIR__ . '/Fixtures/Basic.correct.php.inc'],
37+
'NewPhpFeatures' => [
38+
__DIR__ . '/Fixtures/NewPhpFeatures.wrong.php.inc',
39+
__DIR__ . '/Fixtures/NewPhpFeatures.correct.php.inc',
40+
],
41+
];
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
class Basic
4+
{
5+
public const FOO = 'foo'; // ClassAttributesSeparationFixer
6+
7+
public function isEqual($a, $b) // VisibilityRequiredFixer
8+
{
9+
// TrimArraySpacesFixer
10+
$fooBar = ['a', 'b'];
11+
// MbStrFunctionsFixer
12+
$bazLength = mb_strlen('baz');
13+
// LambdaNotUsedImportFixer
14+
$lambdaWithUnusedImport = function () { return 'foo'; };
15+
// NoUselessSprintfFixer
16+
$uselessSprintf = 'bar';
17+
// SingleSpaceAfterConstructFixer
18+
if ($a == $b) {
19+
return true;
20+
}
21+
22+
return false; // BlankLineBeforeStatementFixer
23+
}
24+
25+
public function fooBar(mixed $foo): mixed
26+
{
27+
// TernaryToElvisOperatorFixer
28+
return ($foo ?: 'not true');
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
class Basic
4+
{
5+
const FOO = 'foo'; // ClassAttributesSeparationFixer
6+
function isEqual($a, $b) // VisibilityRequiredFixer
7+
{
8+
// TrimArraySpacesFixer
9+
$fooBar = [ 'a', 'b'];
10+
// MbStrFunctionsFixer
11+
$bazLength = strlen('baz');
12+
// LambdaNotUsedImportFixer
13+
$lambdaWithUnusedImport = function () use ($fooBar) { return 'foo'; };
14+
// NoUselessSprintfFixer
15+
$uselessSprintf = sprintf('bar');
16+
// SingleSpaceAfterConstructFixer
17+
if ($a == $b) { return true; }
18+
return false; // BlankLineBeforeStatementFixer
19+
}
20+
21+
public function fooBar(mixed $foo): mixed
22+
{
23+
// TernaryToElvisOperatorFixer
24+
return ($foo ? $foo : 'not true');
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lmc\CodingStandard\Integration\Fixtures;
4+
5+
class NewPhpFeatures
6+
{
7+
public function __construct(private string $someString) // RequireConstructorPropertyPromotionSniff
8+
{
9+
}
10+
11+
public function php80features(
12+
string|bool $foo, // UnionTypeHintFormatSniff
13+
int $bar, // RequireTrailingCommaInDeclarationSniff
14+
): string|bool {
15+
$value = mt_rand(
16+
0,
17+
$bar, // RequireTrailingCommaInCallSniff
18+
);
19+
20+
$dateOrNull = $this->mayReturnDateTimeOrNull();
21+
$timestamp = $dateOrNull?->getTimestamp(); // RequireNullSafeObjectOperatorSniff
22+
23+
return $foo;
24+
}
25+
26+
public function mayReturnDateTimeOrNull(): ?\DateTime
27+
{
28+
return null;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Lmc\CodingStandard\Integration\Fixtures;
4+
5+
class NewPhpFeatures
6+
{
7+
private string $someString;
8+
9+
public function __construct(string $someString) // RequireConstructorPropertyPromotionSniff
10+
{
11+
$this->someString = $someString;
12+
}
13+
14+
public function php80features(
15+
string | bool $foo, // UnionTypeHintFormatSniff
16+
int $bar // RequireTrailingCommaInDeclarationSniff
17+
): string | bool {
18+
$value = mt_rand(
19+
0,
20+
$bar // RequireTrailingCommaInCallSniff
21+
);
22+
23+
$dateOrNull = $this->mayReturnDateTimeOrNull();
24+
$timestamp = $dateOrNull !== null ? $dateOrNull->getTimestamp() : null; // RequireNullSafeObjectOperatorSniff
25+
26+
return $foo;
27+
}
28+
29+
public function mayReturnDateTimeOrNull(): ?\DateTime
30+
{
31+
return null;
32+
}
33+
}

0 commit comments

Comments
 (0)