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 Hope\Locker\FileLocker; |
8
|
|
|
use org\bovigo\vfs\vfsStream; |
9
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
10
|
|
|
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Sync tests |
14
|
|
|
* |
15
|
|
|
* @author Sergey Sadovoi <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class SyncTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Root directory of virtual FS |
21
|
|
|
* |
22
|
|
|
* @var vfsStreamDirectory |
23
|
|
|
*/ |
24
|
|
|
protected $root; |
25
|
|
|
|
26
|
|
|
public function setUp() |
27
|
|
|
{ |
28
|
|
|
$tree = [ |
29
|
|
|
'source' => [ |
30
|
|
|
'TEST' => [ |
31
|
|
|
'stream' => [ |
32
|
|
|
'TEST00213.mov' => 'test stream content', |
33
|
|
|
'TEST00313.mov' => 'test stream content', |
34
|
|
|
], |
35
|
|
|
'source' => [ |
36
|
|
|
'TEST00213.mov' => 'test source content', |
37
|
|
|
], |
38
|
|
|
], |
39
|
|
|
'ZZZZ' => [ |
40
|
|
|
'stream' => [ |
41
|
|
|
'ZZZZ00113.mov' => 'test stream content', |
42
|
|
|
], |
43
|
|
|
'source' => [ |
44
|
|
|
'ZZZZ00313.mov' => 'test source content', |
45
|
|
|
], |
46
|
|
|
] |
47
|
|
|
], |
48
|
|
|
'dest' => [ |
49
|
|
|
'TEST' => [ |
50
|
|
|
'TEST00213.mov' => 'test stream content', |
51
|
|
|
'TEST00313.mov' => 'test stream content 2', |
52
|
|
|
'TEST00413.mov' => 'test stream content 2', |
53
|
|
|
], |
54
|
|
|
], |
55
|
|
|
'lock' => [ |
56
|
|
|
'lock.tmp' => '', |
57
|
|
|
], |
58
|
|
|
]; |
59
|
|
|
// Init virtual FS |
60
|
|
|
$this->root = vfsStream::setup('root', null, $tree); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testDirSync() |
64
|
|
|
{ |
65
|
|
|
$sync = new Sync(); |
66
|
|
|
|
67
|
|
|
// Set up Master |
68
|
|
|
$pathFilter = new Path(); |
69
|
|
|
$pathFilter->setPattern('~[A-Z]{4}/stream/.*\.mov~'); |
70
|
|
|
|
71
|
|
|
$masterStorage = new Local(); |
72
|
|
|
$masterPath = 'root/source'; |
73
|
|
|
$masterFilters = [$pathFilter]; |
74
|
|
|
|
75
|
|
|
$sync->setMasterStorage($masterStorage); |
76
|
|
|
$sync->setMasterPath(vfsStream::url($masterPath)); |
77
|
|
|
$sync->setMasterFilters($masterFilters); |
78
|
|
|
|
79
|
|
|
// Set up Slave |
80
|
|
|
$slaveStorage = new Local(); |
81
|
|
|
$slavePath = 'root/dest'; |
82
|
|
|
$slavePathTpl = $slavePath . '/__program__/__uid__'; |
83
|
|
|
|
84
|
|
|
$sync->setSlaveStorage($slaveStorage); |
85
|
|
|
$sync->setSlavePath(vfsStream::url($slavePath)); |
86
|
|
|
$sync->setSlavePathTpl(vfsStream::url($slavePathTpl)); |
87
|
|
|
|
88
|
|
|
$locker = new FileLocker('test-sync', vfsStream::url('root/lock')); |
89
|
|
|
$sync->setLocker($locker); |
90
|
|
|
|
91
|
|
|
// Run |
92
|
|
|
$sync->run(); |
93
|
|
|
|
94
|
|
|
// Test |
95
|
|
|
$files = [ |
96
|
|
|
'root/dest/TEST/TEST00213.mov' => 'test stream content', |
97
|
|
|
'root/dest/TEST/TEST00313.mov' => 'test stream content', |
98
|
|
|
'root/dest/ZZZZ/ZZZZ00113.mov' => 'test stream content', |
99
|
|
|
]; |
100
|
|
|
$removed = 'root/dest/TEST/TEST00413.mov'; |
101
|
|
|
|
102
|
|
|
foreach ($files as $path => $content) { |
103
|
|
|
$this->assertTrue($this->root->hasChild($path)); |
104
|
|
|
$this->assertEquals($content, $this->root->getChild($path)->getContent()); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->assertFalse($this->root->hasChild($removed)); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: