LocalTest::testFailedDeletePermissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
namespace Tests\AppBundle\Sync\Storage;
3
4
use AppBundle\Sync\Storage\Local;
5
use AppBundle\Sync\Entity\File;
6
use org\bovigo\vfs\vfsStream;
7
use org\bovigo\vfs\vfsStreamDirectory;
8
9
/**
10
 * Local Storage tests
11
 *
12
 * @author Sergey Sadovoi <[email protected]>
13
 */
14
class LocalTest extends \PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * Root directory of virtual FS
18
     *
19
     * @var vfsStreamDirectory
20
     */
21
    protected $root;
22
23
    public function setUp()
24
    {
25
        $tree = [
26
            'source' => [
27
                'ADUU' => [
28
                    'stream' => [
29
                        'ADUU00313.mov' => 'test stream content',
30
                    ],
31
                    'source' => [
32
                        'ADUU00213.mov' => 'test source content',
33
                    ],
34
                ],
35
                'BCUU' => [
36
                    'stream' => [
37
                        'BCUU00113.mov' => 'test stream content',
38
                    ],
39
                    'source' => [
40
                        'BCUU00313.mov' => 'test source content',
41
                    ],
42
                ]
43
            ],
44
            'dest' => [
45
                'ADUU' => [
46
                    'stream' => [
47
                        'ADUU00313.mov' => 'test stream content 2',
48
                    ],
49
                    'source' => [
50
                        'ADUU00213.mov' => 'test source content 2',
51
                    ],
52
                ],
53
            ]
54
        ];
55
        // Init virtual FS
56
        $this->root = vfsStream::setup('root', null, $tree);
57
    }
58
59
    public function testPut()
60
    {
61
        $source1 = 'root/source/ADUU/stream/ADUU00313.mov';
62
        $dest1   = 'root/dest/ADUU/stream/ADUU00313.mov';
63
        $source2 = 'root/source/BCUU/stream/BCUU00113.mov';
64
        $dest2   = 'root/dest/BCUU/stream/BCUU00113.mov';
65
66
        $storage = new Local();
67
68
        // Replace existing file
69
        $storage->put(vfsStream::url($source1), vfsStream::url($dest1));
70
        $this->assertTrue($this->root->hasChild($dest1));
71
        $this->assertEquals(
72
            filesize(vfsStream::url($source1)),
73
            filesize(vfsStream::url($dest1))
74
        );
75
76
        // Create new file and dir
77
        $storage->put(vfsStream::url($source2), vfsStream::url($dest2));
78
        $this->assertTrue($this->root->hasChild($dest2));
79
        $this->assertEquals(
80
            filesize(vfsStream::url($source2)),
81
            filesize(vfsStream::url($dest2))
82
        );
83
    }
84
85
    /**
86
     * @expectedException \AppBundle\Exception\LocalStorageException
87
     * @expectedExceptionCode \AppBundle\Exception\LocalStorageException::FILE_NOT_FOUND
88
     */
89
    public function testFailedPut()
90
    {
91
        $source = 'root/source/UUUU/stream/BCUU00113.mov';
92
        $dest   = 'root/dest/UUUU/stream/BCUU00113.mov';
93
94
        $storage = new Local();
95
        $storage->put(vfsStream::url($source), vfsStream::url($dest));
96
    }
97
98
    /**
99
     * @expectedException \AppBundle\Exception\LocalStorageException
100
     * @expectedExceptionCode \AppBundle\Exception\LocalStorageException::OPERATION_FAIL
101
     */
102
    public function testFailedPutPermissions()
103
    {
104
        $source = 'root/source/ADUU/stream/ADUU00313.mov';
105
        $dest   = 'root/dest/ADUU/stream/BCUU00113.mov';
106
107
        /**
108
         * @var \org\bovigo\vfs\vfsStreamFile $vfsLockFile
109
         */
110
        $vfsLockFile = $this->root->getChild($source);
111
        $vfsLockFile->chmod(0400)
112
                    ->chown(1);
113
114
        $storage = new Local();
115
        $storage->put(vfsStream::url($source), vfsStream::url($dest));
116
    }
117
118
    public function testDelete()
119
    {
120
        $storage = new Local();
121
122
        $dest = 'root/dest/ADUU/stream/ADUU00313.mov';
123
        $storage->delete(vfsStream::url($dest));
124
        $this->assertFalse($this->root->hasChild($dest));
125
    }
126
127
    /**
128
     * @expectedException \AppBundle\Exception\LocalStorageException
129
     * @expectedExceptionCode \AppBundle\Exception\LocalStorageException::FILE_NOT_FOUND
130
     */
131
    public function testFailedDelete()
132
    {
133
        $storage = new Local();
134
135
        $dest = 'root/dest/BCUU/stream/BCUU00113.mov';
136
        $storage->delete(vfsStream::url($dest));
137
    }
138
139
    /**
140
     * @expectedException \AppBundle\Exception\LocalStorageException
141
     * @expectedExceptionCode \AppBundle\Exception\LocalStorageException::OPERATION_FAIL
142
     */
143
    public function testFailedDeletePermissions()
144
    {
145
        $storage = new Local();
146
147
        $dest = 'root/dest/ADUU/stream/ADUU00313.mov';
148
        /**
149
         * @var \org\bovigo\vfs\vfsStreamFile $vfsDestDir
150
         */
151
        $vfsDestDir = $this->root->getChild(dirname($dest));
152
        $vfsDestDir->chmod(0400)
153
                    ->chown(1);
154
155
        $storage->delete(vfsStream::url($dest));
156
    }
157
158
    public function testList()
159
    {
160
        $sourceDir  = 'root/source';
161
        $sourceFile = 'root/source/ADUU/stream/ADUU00313.mov';
162
163
        $storage    = new Local();
164
165
        $collection = $storage->listContents(vfsStream::url($sourceDir));
166
        $this->assertCount(4, $collection);
167
168
        /**
169
         * @var File $file
170
         */
171
        $file = $collection->get(vfsStream::url($sourceFile));
172
        $this->assertEquals('ADUU00313.mov', $file->getUid());
173
    }
174
175
    /**
176
     * @expectedException \AppBundle\Exception\LocalStorageException
177
     * @expectedExceptionCode \AppBundle\Exception\LocalStorageException::FILE_NOT_FOUND
178
     */
179
    public function testListNotFound()
180
    {
181
        $sourceDir  = 'root/missing/dir';
182
183
        $storage    = new Local();
184
        $storage->listContents(vfsStream::url($sourceDir));
185
    }
186
187
    /**
188
     * @expectedException \AppBundle\Exception\LocalStorageException
189
     * @expectedExceptionCode \AppBundle\Exception\LocalStorageException::OPERATION_FAIL
190
     */
191
    public function testDirectoryFailed()
192
    {
193
        $source = 'root/source/ADUU/stream/ADUU00313.mov';
194
        $dest   = 'root/dest/XXXX/stream/ADUU00313.mov';
195
196
        /**
197
         * @var \org\bovigo\vfs\vfsStreamFile $vfsDestDir
198
         */
199
        $vfsDestDir = $this->root->getChild('root/dest');
200
        $vfsDestDir->chmod(0400)
201
                   ->chown(1);
202
203
        $storage = new Local();
204
        $storage->put(vfsStream::url($source), vfsStream::url($dest));
205
    }
206
}
207