-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCodingStandardTest.php
101 lines (84 loc) · 2.82 KB
/
CodingStandardTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php declare(strict_types=1);
namespace Lmc\CodingStandard\Integration;
use PHPUnit\Framework\TestCase;
/**
* @coversNothing
*/
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
{
$fixedFile = $this->runEcsCheckOnFile($wrongFile);
$this->assertStringEqualsFile($correctFile, file_get_contents($fixedFile));
}
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',
],
'PhpDoc' => [__DIR__ . '/Fixtures/PhpDoc.wrong.php.inc', __DIR__ . '/Fixtures/PhpDoc.correct.php.inc'],
'PhpUnit' => [__DIR__ . '/Fixtures/PhpUnit.wrong.php.inc', __DIR__ . '/Fixtures/PhpUnit.correct.php.inc'],
];
}
/**
* @test
* @requires PHP >= 8.1
*/
public function shouldFixPhp81(): void
{
$fixedFile = $this->runEcsCheckOnFile(__DIR__ . '/Fixtures/Php81.wrong.php.inc');
$this->assertStringEqualsFile(
__DIR__ . '/Fixtures/Php81.correct.php.inc',
file_get_contents($fixedFile),
);
}
/**
* @test
* @requires PHP >= 8.2
*/
public function shouldFixPhp82(): void
{
$fixedFile = $this->runEcsCheckOnFile(__DIR__ . '/Fixtures/Php82.wrong.php.inc');
$this->assertStringEqualsFile(
__DIR__ . '/Fixtures/Php82.correct.php.inc',
file_get_contents($fixedFile),
);
}
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;
}
}