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

SourceCollectionTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
c 1
b 0
f 1
lcom 0
cbo 5
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B sourceCollectionCombinesResultsFromSeveralSources() 0 26 1
A ifOneSourceFailsWholeCollectionFails() 0 19 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
use Psr\Log\NullLogger;
16
use RunOpenCode\Backup\Contract\FileInterface;
17
use RunOpenCode\Backup\Source\Glob;
18
use RunOpenCode\Backup\Source\SourceCollection;
19
20
class SourceCollectionTest extends \PHPUnit_Framework_TestCase
21
{
22
    /**
23
     * @test
24
     */
25
    public function sourceCollectionCombinesResultsFromSeveralSources()
26
    {
27
        $source = new SourceCollection();
28
29
        $source
30
            ->add($src1 = new Glob(realpath(__DIR__ . '/../Fixtures/glob/globSet1') . '/*'))
31
            ->add($src2 = new Glob(realpath(__DIR__ . '/../Fixtures/glob/globSet2') . '/*'));
32
33
        $logger = new NullLogger();
34
35
        $src1->setLogger($logger);
36
        $src2->setLogger($logger);
37
38
        $files = $source->fetch();
39
40
        $this->assertSame(6, count($files), 'Collection returns all files from all sources.');
41
42
        $this->assertArraySubset(
43
            array('file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', 'file5.txt', 'file6.txt'),
44
            array_map(function(FileInterface $file) {
45
                return $file->getName();
46
            }, $files),
47
            false,
48
            'Has to have 6 specific files.'
49
        );
50
    }
51
52
    /**
53
     * @test
54
     *
55
     * @expectedException \RunOpenCode\Backup\Exception\SourceException
56
     */
57
    public function ifOneSourceFailsWholeCollectionFails()
58
    {
59
        $source = new SourceCollection();
60
61
        $directory = realpath(__DIR__ . '/../Fixtures/glob/globCanNotReadThis');
62
63
        @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...
64
65
        $source
66
            ->add($src1 = new Glob(realpath(__DIR__ . '/../Fixtures/glob/globSet1') . '/*'))
67
            ->add($src2 = new Glob($directory. '/*'));
68
69
        $logger = new NullLogger();
70
71
        $src1->setLogger($logger);
72
        $src2->setLogger($logger);
73
74
        $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...
75
    }
76
77
}