From 2a3f2391d2807f3652f9761122981a901e06ce6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Machulda?= Date: Fri, 10 May 2024 12:31:51 +0200 Subject: [PATCH] Test: Refactor integration tests to make them more versatile --- tests/Integration/CodingStandardTest.php | 52 ++++++++++++++++++------ 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/tests/Integration/CodingStandardTest.php b/tests/Integration/CodingStandardTest.php index 42cf1eb..316f58b 100644 --- a/tests/Integration/CodingStandardTest.php +++ b/tests/Integration/CodingStandardTest.php @@ -9,25 +9,23 @@ */ class CodingStandardTest extends TestCase { + private string $tempFixtureFile; + + /** @after */ + protected function cleanUpTempFixtureFile(): void + { + unlink($this->tempFixtureFile); + } + /** * @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, - ); + $fixedFile = $this->runEcsCheckOnFile($wrongFile); - $this->assertStringEqualsFile($correctFile, file_get_contents($fixtureFile)); - unlink($fixtureFile); + $this->assertStringEqualsFile($correctFile, file_get_contents($fixedFile)); } public function provideFilesToFix(): array @@ -40,4 +38,34 @@ public function provideFilesToFix(): array ], ]; } + + private function runEcsCheckOnFile(string $file): string + { + $fixtureFile = $this->initTempFixtureFile(); + + // copy source of wrongFile to a temporary file which will be modified by ECS + copy($file, $fixtureFile); + + shell_exec( + sprintf( + '%s/../../vendor/bin/ecs check --no-progress-bar --no-ansi --no-interaction --fix %s', + __DIR__, + $fixtureFile, + ), + ); + + return $fixtureFile; + } + + private function initTempFixtureFile(): string + { + // Create new file in temporary directory + $fixtureFile = tempnam(sys_get_temp_dir(), 'ecs-test'); + if ($fixtureFile === false) { + $this->fail('Could not create temporary file'); + } + $this->tempFixtureFile = $fixtureFile; // store to be able to remove it later + + return $fixtureFile; + } }