99 */
1010class CodingStandardTest extends TestCase
1111{
12+ private string $ tempFixtureFile ;
13+
14+ /** @after */
15+ protected function cleanUpTempFixtureFile (): void
16+ {
17+ unlink ($ this ->tempFixtureFile );
18+ }
19+
1220 /**
1321 * @test
1422 * @dataProvider provideFilesToFix
1523 */
1624 public function shouldFixFile (string $ wrongFile , string $ correctFile ): void
1725 {
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- );
26+ $ fixedFile = $ this ->runEcsCheckOnFile ($ wrongFile );
2827
29- $ this ->assertStringEqualsFile ($ correctFile , file_get_contents ($ fixtureFile ));
30- unlink ($ fixtureFile );
28+ $ this ->assertStringEqualsFile ($ correctFile , file_get_contents ($ fixedFile ));
3129 }
3230
3331 public function provideFilesToFix (): array
@@ -40,4 +38,34 @@ public function provideFilesToFix(): array
4038 ],
4139 ];
4240 }
41+
42+ private function runEcsCheckOnFile (string $ file ): string
43+ {
44+ $ fixtureFile = $ this ->initTempFixtureFile ();
45+
46+ // copy source of wrongFile to a temporary file which will be modified by ECS
47+ copy ($ file , $ fixtureFile );
48+
49+ shell_exec (
50+ sprintf (
51+ '%s/../../vendor/bin/ecs check --no-progress-bar --no-ansi --no-interaction --fix %s ' ,
52+ __DIR__ ,
53+ $ fixtureFile ,
54+ ),
55+ );
56+
57+ return $ fixtureFile ;
58+ }
59+
60+ private function initTempFixtureFile (): string
61+ {
62+ // Create new file in temporary directory
63+ $ fixtureFile = tempnam (sys_get_temp_dir (), 'ecs-test ' );
64+ if ($ fixtureFile === false ) {
65+ $ this ->fail ('Could not create temporary file ' );
66+ }
67+ $ this ->tempFixtureFile = $ fixtureFile ; // store to be able to remove it later
68+
69+ return $ fixtureFile ;
70+ }
4371}
0 commit comments