Completed
Pull Request — master (#433)
by Albin
10:58
created

LocalTest::shouldWorkWithSyslink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Gaufrette\Functional\Adapter;
4
5
use Gaufrette\Filesystem;
6
use Gaufrette\Adapter\Local\Local;
7
8
class LocalTest extends FunctionalTestCase
9
{
10
    private $directory;
11
12
    public function setUp()
13
    {
14
        $this->directory = sprintf('%s/filesystem', str_replace('\\', '/', __DIR__));
15
16
        if (!file_exists($this->directory)) {
17
            mkdir($this->directory);
18
        }
19
20
        // var_dump($this->directory);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
        $this->filesystem = new Filesystem(new Local($this->directory));
22
    }
23
24 View Code Duplication
    public function tearDown()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
25
    {
26
        $this->filesystem = null;
27
28
        if (file_exists($this->directory)) {
29
            $iterator = new \RecursiveIteratorIterator(
30
                new \RecursiveDirectoryIterator(
31
                    $this->directory,
32
                    \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS
33
                ),
34
                \RecursiveIteratorIterator::CHILD_FIRST
35
            );
36
37
            foreach ($iterator as $item) {
38
                if ($item->isDir()) {
39
                    rmdir(strval($item));
40
                } else {
41
                    unlink(strval($item));
42
                }
43
            }
44
        }
45
    }
46
47
    /**
48
     * @test
49
     * @group functional
50
     */
51
    public function shouldWorkWithSyslink()
52
    {
53
        $dirname = sprintf(
54
            '%s/adapters/aaa',
55
            __DIR__
56
        );
57
        $linkname = sprintf(
58
            '%s/adapters/../../../../link',
59
            __DIR__
60
        );
61
62
        @mkdir($dirname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
        @unlink($linkname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
64
        symlink($dirname, $linkname);
65
66
        $this->filesystem = new Filesystem(new Local($linkname));
67
        $this->filesystem->write('test.txt', 'abc 123');
68
69
        $this->assertSame('abc 123', $this->filesystem->read('test.txt'));
70
        $this->filesystem->delete('test.txt');
71
        @unlink($linkname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
72
        @rmdir($dirname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
73
    }
74
75
    /**
76
     * @test
77
     * @covers Gaufrette\Adapter\Local
78
     * @group functional
79
     */
80
    public function shouldListingOnlyGivenDirectory()
81
    {
82
        $dirname = sprintf(
83
            '%s/localDir',
84
            $this->directory
85
        );
86
        @mkdir($dirname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
87
88
        $this->filesystem = new Filesystem(new Local($this->directory));
89
        $this->filesystem->write('aaa.txt', 'some content');
90
        $this->filesystem->write('localDir/test.txt', 'some content');
91
92
        $dirs = $this->filesystem->listKeys('localDir/test');
93
        $this->assertEmpty($dirs['dirs']);
94
        $this->assertCount(1, $dirs['keys']);
95
        $this->assertEquals('localDir/test.txt', $dirs['keys'][0]);
96
97
        $dirs = $this->filesystem->listKeys();
98
99
        $this->assertCount(1, $dirs['dirs']);
100
        $this->assertEquals('localDir', $dirs['dirs'][0]);
101
        $this->assertCount(2, $dirs['keys']);
102
        $this->assertEquals('aaa.txt', $dirs['keys'][0]);
103
        $this->assertEquals('localDir/test.txt', $dirs['keys'][1]);
104
105
        @unlink($dirname.DIRECTORY_SEPARATOR.'test.txt');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
106
        @unlink($this->directory.DIRECTORY_SEPARATOR.'aaa.txt');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
107
        @rmdir($dirname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
108
    }
109
110
    /**
111
     * @test
112
     * @covers Gaufrette\Adapter\Local
113
     * @group functional
114
     */
115
    public function shouldListingAllKeys()
116
    {
117
        $dirname = sprintf(
118
            '%s/localDir',
119
            $this->directory
120
        );
121
        @mkdir($dirname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
122
123
        $this->filesystem = new Filesystem(new Local($this->directory));
124
        $this->filesystem->write('aaa.txt', 'some content');
125
        $this->filesystem->write('localDir/dir1/dir2/dir3/test.txt', 'some content');
126
127
        $keys = $this->filesystem->keys();
128
        $dirs = $this->filesystem->listKeys();
129
        $this->assertCount(6, $keys);
130
        $this->assertCount(4, $dirs['dirs']);
131
        $this->assertEquals('localDir/dir1/dir2/dir3/test.txt', $dirs['keys'][1]);
132
133
        foreach ($dirs['keys'] as $item) {
134
            @unlink($item);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
135
        }
136
        $reversed = array_reverse($dirs['dirs']);
137
        foreach ($reversed as $item) {
138
            @rmdir($item);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
139
        }
140
    }
141
142
    /**
143
     * @test
144
     * @group functional
145
     */
146
    public function shouldBeAbleToClearCache()
147
    {
148
        $dirname = sprintf(
149
            '%s/adapters/bbb',
150
            __DIR__
151
        );
152
153
        @mkdir($dirname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
154
155
        $this->filesystem = new Filesystem(new Local($dirname));
156
157
        $this->filesystem->get('test.txt', true);
158
        $this->filesystem->write('test.txt', '123', true);
159
160
        $this->filesystem->get('test2.txt', true);
161
        $this->filesystem->write('test2.txt', '123', true);
162
163
        $fsReflection = new \ReflectionClass($this->filesystem);
164
165
        $fsIsFileInRegister = $fsReflection->getMethod('isFileInRegister');
166
        $fsIsFileInRegister->setAccessible(true);
167
168
        $this->assertTrue($fsIsFileInRegister->invoke($this->filesystem, 'test.txt'));
169
        $this->filesystem->removeFromRegister('test.txt');
170
        $this->assertFalse($fsIsFileInRegister->invoke($this->filesystem, 'test.txt'));
171
172
        $this->filesystem->clearFileRegister();
173
        $fsRegister = $fsReflection->getProperty('fileRegister');
174
        $fsRegister->setAccessible(true);
175
        $this->assertEquals(0, count($fsRegister->getValue($this->filesystem)));
176
177
        $this->filesystem->delete('test.txt');
178
        $this->filesystem->delete('test2.txt');
179
        @rmdir($dirname);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
180
    }
181
}
182