-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMultiDirectionalFileSyncTest.php
75 lines (57 loc) · 2.01 KB
/
MultiDirectionalFileSyncTest.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
<?php
namespace DivineOmega\FileSync\Tests;
use DivineOmega\FileSync\FileSync;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use PHPUnit\Framework\TestCase;
final class MultiDirectionalFileSyncTest extends TestCase
{
public function setupDirectory($name)
{
$path = __DIR__.'/Data/'.$name.'/';
if (is_dir($path)) {
$files = glob($path.'*.txt');
foreach($files as $file) {
unlink($file);
}
} else {
mkdir($path, 0777, true);
}
$numFiles = random_int(1, 100);
$faker = \Faker\Factory::create();
$monthInSeconds = 2592000;
for ($i=0; $i < $numFiles; $i++) {
$filename = $faker->word().'.txt';
$content = $faker->text(random_int(5, 1000));
file_put_contents($path.$filename, $content);
touch($path.$filename, time() - random_int(0, $monthInSeconds));
}
$adapter = new Local($path);
return new Filesystem($adapter);
}
public function testMultiDirectionalFileSync()
{
$directoryA = $this->setupDirectory('a');
$directoryB = $this->setupDirectory('b');
$directoryC = $this->setupDirectory('c');
(new FileSync())
->multiDirectional()
->with($directoryA)
->with($directoryB)
->with($directoryC)
->begin();
$filesA = glob(__DIR__.'/Data/a/*.txt');
$filesB = glob(__DIR__.'/Data/b/*.txt');
$filesC = glob(__DIR__.'/Data/c/*.txt');
$this->assertSameSize($filesA, $filesB);
$this->assertSameSize($filesA, $filesC);
foreach($filesA as $fileA) {
$fileB = __DIR__.'/Data/b/'.basename($fileA);
$fileC = __DIR__.'/Data/c/'.basename($fileA);
$this->assertFileExists($fileB);
$this->assertFileExists($fileC);
$this->assertFileEquals($fileA, $fileB);
$this->assertFileEquals($fileA, $fileC);
}
}
}