-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathcheck-before-after-same-fixtures.php
More file actions
67 lines (52 loc) · 2.05 KB
/
check-before-after-same-fixtures.php
File metadata and controls
67 lines (52 loc) · 2.05 KB
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
<?php
declare(strict_types=1);
use Nette\Utils\Strings;
use Rector\Scripts\Finder\FixtureFinder;
require __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\SplFileInfo;
final readonly class SameBeforeAfterFixtureDetector
{
private SymfonyStyle $symfonyStyle;
public function __construct()
{
$this->symfonyStyle = new SymfonyStyle(new ArgvInput(), new ConsoleOutput());
}
/**
* @param string[] $testDirectories
* @return Command::SUCCESS|Command::FAILURE
*/
public function run(array $testDirectories): int
{
$fixtureFiles = FixtureFinder::find($testDirectories);
$invalidFixturePaths = [];
foreach ($fixtureFiles as $fixtureFile) {
if (! $this->hasFileSameBeforeAndAfterPart($fixtureFile)) {
continue;
}
$invalidFixturePaths[] = substr($fixtureFile->getRealPath(), strlen(getcwd()) + 1);
}
if ($invalidFixturePaths === []) {
$this->symfonyStyle->success(sprintf('All %d fixtures are valid', count($fixtureFiles)));
return Command::SUCCESS;
}
$this->symfonyStyle->error(
'The following fixtures have the same before and after content. Remove the part after "-----" to fix them'
);
$this->symfonyStyle->listing($invalidFixturePaths);
return Command::FAILURE;
}
private function hasFileSameBeforeAndAfterPart(SplFileInfo $fixtureFile): bool
{
$parts = Strings::split($fixtureFile->getContents(), '#^\s*-----\s*$#m');
if (count($parts) !== 2) {
return false;
}
return trim((string) $parts[0]) === trim((string) $parts[1]);
}
}
$sameBeforeAfterFixtureDetector = new SameBeforeAfterFixtureDetector();
exit($sameBeforeAfterFixtureDetector->run([__DIR__ . '/../tests', __DIR__ . '/../rules-tests']));