Skip to content

Commit

Permalink
Test: Refactor integration tests to make them more versatile
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraM committed May 10, 2024
1 parent f77ef49 commit 2a3f239
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions tests/Integration/CodingStandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}

0 comments on commit 2a3f239

Please sign in to comment.