Completed
Pull Request — master (#924)
by Greg
02:28
created

PackExtractTest::setUp()   A

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 PackExtractTest extends TestCase
8
{
9
    use TestTasksTrait;
10
    use Task\Archive\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
    /**
26
     * Data provider for testPackExtract.
27
     */
28
    public function archiveTypeProvider()
29
    {
30
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
31
            return [['zip']];
32
        }
33
        return [['zip'], ['tar'], ['tar.gz'], ['tar.bz2'], ['tgz']];
34
    }
35
36
    /**
37
     * Test all of the different sorts of archivers
38
     *
39
     * @dataProvider archiveTypeProvider
40
     */
41
    public function testPackExtract($archiveType)
42
    {
43
        // Archive directory and then extract it again with Archive and Extract tasks
44
        $this->fixtures->createAndCdToSandbox();
45
46
        // Assert fixture was created correctly
47
        $this->assertDirectoryExists('some/deeply/nested');
48
        $this->assertFileExists('some/deeply/nested/structu.re');
49
        $this->assertFileExists('some/deeply/existing_file');
50
51
        // First, take everything from the folder 'some/deeply' and make
52
        // an archive for it located in 'deep'
53
        $this->taskPack("deeply.$archiveType")
0 ignored issues
show
Bug introduced by
The method add does only exist in Robo\Task\Archive\Pack, 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...
54
            ->add(['deep' => 'some/deeply'])
55
            ->run();
56
        $this->assertFileExists("deeply.$archiveType");
57
        // We are next going to extract the archive we created, this time
58
        // putting it into a folder called "extracted-$archiveType" (different
59
        // for each archive type we test).  We rely on the default behavior
60
        // of our extractor to remove the top-level directory in the archive
61
        // ("deeply").
62
        $this->taskExtract("deeply.$archiveType")
0 ignored issues
show
Bug introduced by
The method to does only exist in Robo\Task\Archive\Extract, 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...
63
            ->to("extracted-$archiveType")
64
            ->preserveTopDirectory(false) // this is the default
65
            ->run();
66
        $this->assertDirectoryExists("extracted-$archiveType");
67
        $this->assertDirectoryExists("extracted-$archiveType/nested");
68
        $this->assertFileExists("extracted-$archiveType/nested/structu.re");
69
        // Next, we'll extract the same archive again, this time preserving
70
        // the top-level folder.
71
        $this->taskExtract("deeply.$archiveType")
72
            ->to("preserved-$archiveType")
73
            ->preserveTopDirectory()
74
            ->run();
75
        $this->assertDirectoryExists("preserved-$archiveType");
76
        $this->assertDirectoryExists("preserved-$archiveType/deep/nested");
77
        $this->assertFileExists("preserved-$archiveType/deep/nested/structu.re");
78
        // Make another archive, this time composed of fanciful locations
79
        $this->taskPack("composed.$archiveType")
80
            ->add(['a/b/existing_file' => 'some/deeply/existing_file'])
81
            ->add(['x/y/z/structu.re' => 'some/deeply/nested/structu.re'])
82
            ->run();
83
        $this->assertFileExists("composed.$archiveType");
84
        // Extract our composed archive, and see if the resulting file
85
        // structure matches expectations.
86
        $this->taskExtract("composed.$archiveType")
87
            ->to("decomposed-$archiveType")
88
            ->preserveTopDirectory()
89
            ->run();
90
        $this->assertDirectoryExists("decomposed-$archiveType");
91
        $this->assertDirectoryExists("decomposed-$archiveType/x/y/z");
92
        $this->assertFileExists("decomposed-$archiveType/x/y/z/structu.re");
93
        $this->assertDirectoryExists("decomposed-$archiveType/a/b");
94
        $this->assertFileExists("decomposed-$archiveType/a/b/existing_file");
95
96
    }
97
}
98