CopyDirTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Robo;
3
4
use PHPUnit\Framework\TestCase;
5
use Robo\Traits\TestTasksTrait;
6
7
class CopyDirTest extends TestCase
8
{
9
    use TestTasksTrait;
10
    use Task\Filesystem\loadTasks;
11
12
    protected $fixtures;
13
14
    public function setUp()
15
    {
16
        $this->fixtures = new Fixtures();
17
        $this->initTestTasksTrait();
18
    }
19
20
    public function tearDown()
21
    {
22
        $this->fixtures->cleanup();
23
    }
24
25
    public function testCopyDir()
26
    {
27
        $this->fixtures->createAndCdToSandbox();
28
29
        $result = $this->taskCopyDir(['box' => 'bin'])
30
            ->run();
31
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
32
        $this->assertFileExists('bin');
33
        $this->assertFileExists('bin/robo.txt');
34
    }
35
36
    /**
37
     * Data provider for overwrite test
38
     */
39
    public function copyDirWithOverwriteData()
40
    {
41
        return [
42
            [
43
                true,
44
                'some existing file',
45
            ],
46
            [
47
                false,
48
                'newer data',
49
            ],
50
        ];
51
    }
52
53
    /**
54
     * @dataProvider copyDirWithOverwriteData
55
     */
56
    public function testCopyDirWithOverwrite($overwriteValue, $expected)
57
    {
58
        $this->fixtures->createAndCdToSandbox();
59
60
        @mkdir('some_destination');
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...
61
        @mkdir('some_destination/deeply');
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...
62
        file_put_contents('some_destination/deeply/existing_file', 'newer data');
63
64
        $this->assertFileExists('some');
65
        $this->assertFileExists('some/deeply/existing_file');
66
        $result = $this->taskCopyDir(['some' => 'some_destination'])
0 ignored issues
show
Bug introduced by
The method overwrite does only exist in Robo\Task\Filesystem\CopyDir, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
67
            ->overwrite($overwriteValue)
68
            ->run();
69
        $this->assertTrue($result->wasSuccessful());
70
71
        $this->assertFileExists('some_destination');
72
        $this->assertFileExists('some_destination/deeply/existing_file');
73
74
        $actual = trim(file_get_contents('some_destination/deeply/existing_file'));
75
        $this->assertEquals($expected, $actual);
76
    }
77
78 View Code Duplication
    public function testCopyRecursive()
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...
79
    {
80
        $this->fixtures->createAndCdToSandbox();
81
82
        $this->assertFileExists('some/deeply/nested');
83
        $this->assertFileExists('some/deeply/nested/structu.re');
84
        $result = $this->taskCopyDir(['some/deeply' => 'some_destination/deeply'])
85
            ->run();
86
        $this->assertTrue($result->wasSuccessful());
87
        $this->assertFileExists('some_destination/deeply/nested');
88
        $this->assertFileExists('some_destination/deeply/nested/structu.re');
89
90
    }
91
92
    public function testCopyRecursiveWithExcludedFile()
93
    {
94
        $this->fixtures->createAndCdToSandbox();
95
96
        $this->assertFileExists('some/deeply/nested');
97
        $this->assertFileExists('some/deeply/nested2');
98
        $this->assertFileExists('some/deeply/nested3');
99
        $this->assertFileExists('some/deeply/nested3/nested31');
100
        $this->assertFileExists('some/deeply/nested4');
101
        $this->assertFileExists('some/deeply/nested4/nested41');
102
        $this->assertFileExists('some/deeply/nested/structu.re');
103
        $this->assertFileExists('some/deeply/nested/structu1.re');
104
        $this->assertFileExists('some/deeply/nested/structu2.re');
105
        $this->assertFileExists('some/deeply/nested/structu3.re');
106
        $this->assertFileExists('some/deeply/nested2/structu21.re');
107
        $this->assertFileExists('some/deeply/nested3/structu31.re');
108
        $this->assertFileExists('some/deeply/nested3/structu32.re');
109
        $this->assertFileExists('some/deeply/nested3/nested31/structu311.re');
110
        $this->assertFileExists('some/deeply/nested4/nested41/structu411.re');
111
        $this->assertFileExists('some/deeply/nested4/nested41/structu412.re');
112
113
        $result = $this->taskCopyDir(['some/deeply' => 'some_destination/deeply'])
0 ignored issues
show
Bug introduced by
The method exclude does only exist in Robo\Task\Filesystem\CopyDir, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
114
            ->exclude([
115
                // Basename exclusion.
116
                'structu1.re',
117
                // File in subdir exclusion.
118
                'some/deeply/nested/structu3.re',
119
                // Dir exclusion.
120
                'nested2',
121
                // Subdir exclusion.
122
                'some/deeply/nested3/nested31',
123
                // Subpath within source exclusion.
124
                'nested3/structu31.re',
125
                // File in deeper subpath within source exclusion.
126
                'nested4/nested41/structu411.re',
127
            ])
128
            ->run();
129
        $this->assertTrue($result->wasSuccessful());
130
131
        $this->assertFileExists('some_destination/deeply/nested');
132
        $this->assertFileNotExists('some_destination/deeply/nested2');
133
        $this->assertFileExists('some_destination/deeply/nested3');
134
        $this->assertFileNotExists('some_destination/deeply/nested3/nested31');
135
        $this->assertFileExists('some_destination/deeply/nested4');
136
        $this->assertFileExists('some_destination/deeply/nested4/nested41');
137
        $this->assertFileExists('some_destination/deeply/nested/structu.re');
138
        $this->assertFileNotExists('some_destination/deeply/nested/structu1.re');
139
        $this->assertFileExists('some_destination/deeply/nested/structu2.re');
140
        $this->assertFileNotExists('some_destination/deeply/nested/structu3.re');
141
        $this->assertFileNotExists('some_destination/deeply/nested2/structu21.re');
142
        $this->assertFileNotExists('some_destination/deeply/nested3/structu31.re');
143
        $this->assertFileExists('some_destination/deeply/nested3/structu32.re');
144
        $this->assertFileNotExists('some_destination/deeply/nested3/nested31/structu311.re');
145
        $this->assertFileNotExists('some_destination/deeply/nested4/nested41/structu411.re');
146
        $this->assertFileExists('some_destination/deeply/nested4/nested41/structu412.re');
147
    }
148
}
149