1
|
|
|
<?php |
2
|
|
|
namespace Tests\AppBundle\Sync; |
3
|
|
|
|
4
|
|
|
use AppBundle\Sync\Entity\Filter\Path; |
5
|
|
|
use AppBundle\Sync\Storage\Local; |
6
|
|
|
use AppBundle\Sync\Sync; |
7
|
|
|
use org\bovigo\vfs\vfsStream; |
8
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
9
|
|
|
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Sync tests |
13
|
|
|
* |
14
|
|
|
* @author Sergey Sadovoi <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class SyncTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Root directory of virtual FS |
20
|
|
|
* |
21
|
|
|
* @var vfsStreamDirectory |
22
|
|
|
*/ |
23
|
|
|
protected $root; |
24
|
|
|
|
25
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$tree = [ |
28
|
|
|
'source' => [ |
29
|
|
|
'TEST' => [ |
30
|
|
|
'stream' => [ |
31
|
|
|
'TEST00213.mov' => 'test stream content', |
32
|
|
|
'TEST00313.mov' => 'test stream content', |
33
|
|
|
], |
34
|
|
|
'source' => [ |
35
|
|
|
'TEST00213.mov' => 'test source content', |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
'ZZZZ' => [ |
39
|
|
|
'stream' => [ |
40
|
|
|
'ZZZZ00113.mov' => 'test stream content', |
41
|
|
|
], |
42
|
|
|
'source' => [ |
43
|
|
|
'ZZZZ00313.mov' => 'test source content', |
44
|
|
|
], |
45
|
|
|
] |
46
|
|
|
], |
47
|
|
|
'dest' => [ |
48
|
|
|
'TEST' => [ |
49
|
|
|
'TEST00213.mov' => 'test stream content', |
50
|
|
|
'TEST00313.mov' => 'test stream content 2', |
51
|
|
|
'TEST00413.mov' => 'test stream content 2', |
52
|
|
|
], |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
// Init virtual FS |
56
|
|
|
$this->root = vfsStream::setup('root', null, $tree); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testDirSync() |
60
|
|
|
{ |
61
|
|
|
$sync = new Sync(); |
62
|
|
|
|
63
|
|
|
// Set up Master |
64
|
|
|
$pathFilter = new Path(); |
65
|
|
|
$pathFilter->setPattern('~[A-Z]{4}/stream/.*\.mov~'); |
66
|
|
|
|
67
|
|
|
$masterStorage = new Local(); |
68
|
|
|
$masterPath = 'root/source'; |
69
|
|
|
$masterFilters = [$pathFilter]; |
70
|
|
|
|
71
|
|
|
$sync->setMasterStorage($masterStorage); |
72
|
|
|
$sync->setMasterPath(vfsStream::url($masterPath)); |
73
|
|
|
$sync->setMasterFilters($masterFilters); |
74
|
|
|
|
75
|
|
|
// Set up Slave |
76
|
|
|
$slaveStorage = new Local(); |
77
|
|
|
$slavePath = 'root/dest'; |
78
|
|
|
$slavePathTpl = $slavePath . '/__program__/__uid__'; |
79
|
|
|
|
80
|
|
|
$sync->setSlaveStorage($slaveStorage); |
81
|
|
|
$sync->setSlavePath(vfsStream::url($slavePath)); |
82
|
|
|
$sync->setSlavePathTpl(vfsStream::url($slavePathTpl)); |
83
|
|
|
|
84
|
|
|
// Run |
85
|
|
|
$sync->run(); |
86
|
|
|
|
87
|
|
|
// Test |
88
|
|
|
$files = [ |
89
|
|
|
'root/dest/TEST/TEST00213.mov' => 'test stream content', |
90
|
|
|
'root/dest/TEST/TEST00313.mov' => 'test stream content', |
91
|
|
|
'root/dest/ZZZZ/ZZZZ00113.mov' => 'test stream content', |
92
|
|
|
]; |
93
|
|
|
$removed = 'root/dest/TEST/TEST00413.mov'; |
94
|
|
|
|
95
|
|
|
foreach ($files as $path => $content) { |
96
|
|
|
$this->assertTrue($this->root->hasChild($path)); |
97
|
|
|
$this->assertEquals($content, $this->root->getChild($path)->getContent()); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->assertFalse($this->root->hasChild($removed)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.