Completed
Push — master ( 2d2a4f...6d125c )
by Greg
01:53
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
        if ((version_compare(PHP_VERSION, '7.4.0') >= 0) && (getenv('TRAVIS'))) {
44
          $this->markTestSkipped('Zip libraries apparently not available on Travis CI with PHP 7.4 image.');
45
        }
46
47
        // Archive directory and then extract it again with Archive and Extract tasks
48
        $this->fixtures->createAndCdToSandbox();
49
50
        // Assert fixture was created correctly
51
        $this->assertDirectoryExists('some/deeply/nested');
52
        $this->assertFileExists('some/deeply/nested/structu.re');
53
        $this->assertFileExists('some/deeply/existing_file');
54
55
        // First, take everything from the folder 'some/deeply' and make
56
        // an archive for it located in 'deep'
57
        $result = $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...
58
            ->add(['deep' => 'some/deeply'])
59
            ->run();
60
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
61
        $this->assertFileExists("deeply.$archiveType");
62
        // We are next going to extract the archive we created, this time
63
        // putting it into a folder called "extracted-$archiveType" (different
64
        // for each archive type we test).  We rely on the default behavior
65
        // of our extractor to remove the top-level directory in the archive
66
        // ("deeply").
67
        $result = $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...
68
            ->to("extracted-$archiveType")
69
            ->preserveTopDirectory(false) // this is the default
70
            ->run();
71
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
72
        $this->assertDirectoryExists("extracted-$archiveType");
73
        $this->assertDirectoryExists("extracted-$archiveType/nested");
74
        $this->assertFileExists("extracted-$archiveType/nested/structu.re");
75
        // Next, we'll extract the same archive again, this time preserving
76
        // the top-level folder.
77
        $this->taskExtract("deeply.$archiveType")
78
            ->to("preserved-$archiveType")
79
            ->preserveTopDirectory()
80
            ->run();
81
        $this->assertDirectoryExists("preserved-$archiveType");
82
        $this->assertDirectoryExists("preserved-$archiveType/deep/nested");
83
        $this->assertFileExists("preserved-$archiveType/deep/nested/structu.re");
84
        // Make another archive, this time composed of fanciful locations
85
        $result = $this->taskPack("composed.$archiveType")
86
            ->add(['a/b/existing_file' => 'some/deeply/existing_file'])
87
            ->add(['x/y/z/structu.re' => 'some/deeply/nested/structu.re'])
88
            ->run();
89
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
90
        $this->assertFileExists("composed.$archiveType");
91
        // Extract our composed archive, and see if the resulting file
92
        // structure matches expectations.
93
        $result = $this->taskExtract("composed.$archiveType")
94
            ->to("decomposed-$archiveType")
95
            ->preserveTopDirectory()
96
            ->run();
97
        $this->assertTrue($result->wasSuccessful(), $result->getMessage());
98
        $this->assertDirectoryExists("decomposed-$archiveType");
99
        $this->assertDirectoryExists("decomposed-$archiveType/x/y/z");
100
        $this->assertFileExists("decomposed-$archiveType/x/y/z/structu.re");
101
        $this->assertDirectoryExists("decomposed-$archiveType/a/b");
102
        $this->assertFileExists("decomposed-$archiveType/a/b/existing_file");
103
104
    }
105
}
106