forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilesFinderTest.php
92 lines (73 loc) · 2.7 KB
/
FilesFinderTest.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
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\FileSystem\FilesFinder;
use Iterator;
use Rector\Core\FileSystem\FilesFinder;
use Rector\Core\HttpKernel\RectorKernel;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;
final class FilesFinderTest extends AbstractKernelTestCase
{
/**
* @var FilesFinder
*/
private $filesFinder;
/**
* @var SmartFileSystem
*/
private $smartFileSystem;
protected function setUp(): void
{
$this->bootKernel(RectorKernel::class);
$this->filesFinder = self::$container->get(FilesFinder::class);
$this->smartFileSystem = self::$container->get(SmartFileSystem::class);
}
/**
* @dataProvider provideData()
*/
public function testSingleSuffix(string $suffix, int $count, string $expectedFileName): void
{
$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([__DIR__ . '/Source'], [$suffix]);
$this->assertCount($count, $foundFiles);
/** @var SmartFileInfo $foundFile */
$foundFile = array_pop($foundFiles);
$this->assertSame($expectedFileName, $foundFile->getBasename());
}
public function provideData(): Iterator
{
yield ['php', 1, 'SomeFile.php'];
yield ['yml', 1, 'some_config.yml'];
yield ['yaml', 1, 'other_config.yaml'];
yield ['php', 1, 'SomeFile.php'];
}
public function testMultipleSuffixes(): void
{
$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([__DIR__ . '/Source'], ['yaml', 'yml']);
$this->assertCount(2, $foundFiles);
$foundFileNames = [];
foreach ($foundFiles as $foundFile) {
$foundFileNames[] = $foundFile->getFilename();
}
$expectedFoundFileNames = ['some_config.yml', 'other_config.yaml'];
sort($foundFileNames);
sort($expectedFoundFileNames);
$this->assertSame($expectedFoundFileNames, $foundFileNames);
}
public function testMatchGitDiff(): void
{
$dir = sys_get_temp_dir() . '/' . mt_rand();
mkdir($dir);
chdir($dir);
shell_exec('git init');
$filename = $dir . '/tmp.php';
touch($filename);
touch($dir . '/tmp.yml');
shell_exec('git add --all && git commit -m "first commit"');
$this->smartFileSystem->dumpFile($filename, '<?php echo ' . mt_rand() . ';');
$this->smartFileSystem->dumpFile($dir . '/tmp.yml', '');
$foundFiles = $this->filesFinder->findInDirectoriesAndFiles([$dir], ['php'], true);
$this->assertCount(1, $foundFiles);
$this->smartFileSystem->remove($filename);
}
}