-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Add basic 'integration' tests to ensure code style is being pr…
…operly checked
- Loading branch information
Showing
7 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?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); | ||
|
||
// @codingStandardsIgnoreStart | ||
shell_exec( | ||
__DIR__ . '/../../vendor/bin/ecs check --no-progress-bar --no-ansi --no-interaction --fix ' . $fixtureFile, | ||
); | ||
// @codingStandardsIgnoreEnd | ||
|
||
$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', | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |