9
9
*/
10
10
class CodingStandardTest extends TestCase
11
11
{
12
+ private string $ tempFixtureFile ;
13
+
14
+ /** @after */
15
+ protected function cleanUpTempFixtureFile (): void
16
+ {
17
+ unlink ($ this ->tempFixtureFile );
18
+ }
19
+
12
20
/**
13
21
* @test
14
22
* @dataProvider provideFilesToFix
15
23
*/
16
24
public function shouldFixFile (string $ wrongFile , string $ correctFile ): void
17
25
{
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 );
28
27
29
- $ this ->assertStringEqualsFile ($ correctFile , file_get_contents ($ fixtureFile ));
30
- unlink ($ fixtureFile );
28
+ $ this ->assertStringEqualsFile ($ correctFile , file_get_contents ($ fixedFile ));
31
29
}
32
30
33
31
public function provideFilesToFix (): array
@@ -40,4 +38,34 @@ public function provideFilesToFix(): array
40
38
],
41
39
];
42
40
}
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
+ }
43
71
}
0 commit comments