ResourceTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 144
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testOpenClose() 0 10 1
A testWrite() 0 10 1
A testRead() 0 10 1
A testRewind() 0 15 1
A testIsActive() 0 11 1
A testCleanup() 0 14 1
A testCleanupNotEmpty() 0 15 1
A testIsEmptyDir() 0 19 1
1
<?php
2
3
namespace Maketok\DataMigration\Storage\Filesystem;
4
5
use org\bovigo\vfs\vfsStream;
6
use org\bovigo\vfs\vfsStreamDirectory;
7
8
class ResourceTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @var vfsStreamDirectory
12
     */
13
    private $root;
14
15
    public function setUp()
16
    {
17
        $this->root = vfsStream::setup('root');
18
    }
19
20
    /**
21
     * @test
22
     * @expectedException \RuntimeException
23
     * @expectedExceptionMessage failed to open stream
24
     */
25
    public function testOpenClose()
26
    {
27
        $filesystem = new Resource();
28
        $filesystem->open(vfsStream::url('root/file1'), 'w');
29
        $filesystem->close();
30
        $this->assertFileExists(vfsStream::url('root/file1'));
31
32
        // should fail
33
        $filesystem->open(vfsStream::url('root/file2'), 'r');
34
    }
35
36
    /**
37
     * @depends testOpenClose
38
     */
39
    public function testWrite()
40
    {
41
        $filesystem = new Resource();
42
        $filesystem->open(vfsStream::url('root/file1'), 'w');
43
        $filesystem->writeRow(['name', 'age', 'address']);
44
        $filesystem->close();
45
46
        $fileObject = new \SplFileObject(vfsStream::url('root/file1'), 'r');
47
        $this->assertSame(['name', 'age', 'address'], $fileObject->fgetcsv());
48
    }
49
50
    /**
51
     * @depends testOpenClose
52
     */
53
    public function testRead()
54
    {
55
        $fileObject = new \SplFileObject(vfsStream::url('root/file1'), 'w');
56
        $fileObject->fputcsv(['name', 'age', 'address']);
57
58
        $filesystem = new Resource();
59
        $filesystem->open(vfsStream::url('root/file1'), 'r');
60
        $this->assertSame(['name', 'age', 'address'], $filesystem->readRow());
61
        $filesystem->close();
62
    }
63
64
    /**
65
     * @depends testRead
66
     */
67
    public function testRewind()
68
    {
69
        $fileObject = new \SplFileObject(vfsStream::url('root/file1'), 'w');
70
        $fileObject->fputcsv(['name', 'age', 'address']);
71
        $fileObject->fputcsv(['Oleg', '3', 'Universe']);
72
73
        $filesystem = new Resource();
74
        $filesystem->open(vfsStream::url('root/file1'), 'r');
75
        $this->assertSame(['name', 'age', 'address'], $filesystem->readRow());
76
        $this->assertSame(['Oleg', '3', 'Universe'], $filesystem->readRow());
77
        $this->assertEmpty($filesystem->readRow());
78
        $filesystem->rewind();
79
        $this->assertSame(['name', 'age', 'address'], $filesystem->readRow());
80
        $filesystem->close();
81
    }
82
83
    /**
84
     * @depends testOpenClose
85
     */
86
    public function testIsActive()
87
    {
88
        $filesystem = new Resource();
89
        $filesystem->open(vfsStream::url('root/file1'), 'w');
90
        $this->assertTrue($filesystem->isActive());
91
        $filesystem->close();
92
        $this->assertFalse($filesystem->isActive());
93
        $filesystem->open(vfsStream::url('root/file1'), 'w');
94
        $this->assertTrue($filesystem->isActive());
95
        $filesystem->close();
96
    }
97
98
    public function testCleanup()
99
    {
100
        $dir = vfsStream::newDirectory('tmp');
101
        $dir->at($this->root);
102
        vfsStream::newFile('file1')->at($dir);
103
104
        $this->assertTrue(file_exists(vfsStream::url('root/tmp/file1')));
105
106
        $filesystem = new Resource();
107
        $filesystem->cleanUp(vfsStream::url('root/tmp/file1'));
108
109
        $this->assertFalse(file_exists(vfsStream::url('root/tmp/file1')));
110
        $this->assertFalse(file_exists(vfsStream::url('root/tmp')));
111
    }
112
113
    public function testCleanupNotEmpty()
114
    {
115
        $dir = vfsStream::newDirectory('tmp');
116
        $dir->at($this->root);
117
        vfsStream::newFile('file1')->at($dir);
118
        vfsStream::newFile('file2')->at($dir);
119
120
        $this->assertTrue(file_exists(vfsStream::url('root/tmp/file1')));
121
122
        $filesystem = new Resource();
123
        $filesystem->cleanUp(vfsStream::url('root/tmp/file1'));
124
125
        $this->assertFalse(file_exists(vfsStream::url('root/tmp/file1')));
126
        $this->assertTrue(file_exists(vfsStream::url('root/tmp')));
127
    }
128
129
    /**
130
     * @test
131
     */
132
    public function testIsEmptyDir()
133
    {
134
        $dir1 = vfsStream::newDirectory('tmp');
135
        $dir1->at($this->root);
136
        $dir2 = vfsStream::newDirectory('tmp_locked', 0111);
137
        $dir2->at($this->root);
138
        $file1 = vfsStream::newFile('tmpfile');
139
        $file1->at($this->root);
140
        $dir3 = vfsStream::newDirectory('tmp2');
141
        $dir3->at($this->root);
142
        $file2 = vfsStream::newFile('tmpfile2');
143
        $file2->at($dir3);
144
145
        $filesystem = new Resource();
146
        $this->assertSame(true, $filesystem->isEmptyDir(vfsStream::url('root/tmp')));
147
        $this->assertSame(null, $filesystem->isEmptyDir(vfsStream::url('root/tmp_locked')));
148
        $this->assertSame(null, $filesystem->isEmptyDir(vfsStream::url('root/tmpfile')));
149
        $this->assertSame(false, $filesystem->isEmptyDir(vfsStream::url('root/tmp2')));
150
    }
151
}
152