Completed
Push — master ( d7c148...f76de2 )
by Nikola
02:27
created

GlobSourceTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 0
cbo 4
dl 0
loc 77
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A singleGlob() 0 23 1
B multipleGlobs() 0 27 1
A invalidGlob() 0 10 1
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
namespace RunOpenCode\Backup\Tests\Source;
14
15
16
use Psr\Log\NullLogger;
17
use RunOpenCode\Backup\Contract\FileInterface;
18
use RunOpenCode\Backup\Source\Glob;
19
20
class GlobSourceTest extends \PHPUnit_Framework_TestCase
21
{
22
23
    /**
24
     * @test
25
     */
26
    public function singleGlob()
27
    {
28
        $source = new Glob(realpath(__DIR__ . '/../Fixtures/glob/globSet1') . '/*');
29
        $files = $source->fetch();
30
31
        $this->assertArraySubset(
32
            array('file1.txt', 'file2.txt', 'file3.txt'),
33
            array_map(function(FileInterface $file) {
34
                return $file->getName();
35
            }, $files),
36
            false,
37
            'Has to have 3 specific files.'
38
        );
39
40
        $this->assertArraySubset(
41
            array('file1.txt', 'file2.txt', 'file3.txt'),
42
            array_map(function(FileInterface $file) {
43
                return $file->getRelativePath();
44
            }, $files),
45
            false,
46
            'Relative path must be filename since glob is root path.'
47
        );
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function multipleGlobs()
54
    {
55
        $source = new Glob(array(
56
            realpath(__DIR__ . '/../Fixtures/glob/globSet1') . '/*' => realpath(__DIR__ . '/../Fixtures/glob'),
57
            realpath(__DIR__ . '/../Fixtures/glob/globSet2') . '/*' => realpath(__DIR__ . '/../Fixtures/glob'),
58
        ));
59
60
        $files = $source->fetch();
61
62
        $this->assertArraySubset(
63
            array('file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt', 'file6.txt'),
64
            array_map(function(FileInterface $file) {
65
                return $file->getName();
66
            }, $files),
67
            false,
68
            'Has to have 6 specific files.'
69
        );
70
71
        $this->assertArraySubset(
72
            array('globSet1/file1.txt', 'globSet1/file2.txt', 'globSet1/file3.txt', 'globSet2/file4.txt', 'globSet2/file5.txt', 'globSet2/file6.txt'),
73
            array_map(function(FileInterface $file) {
74
                return $file->getRelativePath();
75
            }, $files),
76
            false,
77
            'Relative path must be as define since glob root path is given.'
78
        );
79
    }
80
81
    /**
82
     * @test
83
     *
84
     * @expectedException \RunOpenCode\Backup\Exception\SourceException
85
     */
86
    public function invalidGlob()
87
    {
88
        $directory = realpath(__DIR__ . '/../Fixtures/glob/globCanNotReadThis');
89
90
        @chmod($directory, 0200);
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...
91
92
        $source = new Glob($directory . '/*');
93
        $source->setLogger(new NullLogger());
94
        $files = $source->fetch();
0 ignored issues
show
Unused Code introduced by
$files is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
    }
96
}