Completed
Push — master ( 68f30c...a95a96 )
by Christian
02:25
created

BaseProjectTest::tearDownAfterClass2222222222()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
nc 6
cc 4
eloc 15
nop 0
1
<?php
2
3
namespace uuf6429\ElderBrother;
4
5
abstract class BaseProjectTest extends \PHPUnit_Framework_TestCase
6
{
7
    /**
8
     * @var sting Temporary directory for project under test
9
     */
10
    protected static $projectPath;
11
12
    /**
13
     * @var string File that ensure other tests won't influence this one
14
     */
15
    private static $projectLockFile;
16
17
    /**
18
     * @var string
19
     */
20
    private static $oldWorkingDirectory;
21
22
    public static function setUpBeforeClass()
23
    {
24
        parent::setUpBeforeClass();
25
26
        self::$projectLockFile = tempnam(sys_get_temp_dir(), 'test');
27
        self::$projectPath = self::$projectLockFile . '_dir';
0 ignored issues
show
Documentation Bug introduced by
It seems like self::$projectLockFile . '_dir' of type string is incompatible with the declared type object<uuf6429\ElderBrother\sting> of property $projectPath.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
        mkdir(self::$projectPath);
29
30
        self::$oldWorkingDirectory = getcwd();
31
        chdir(self::$projectPath);
32
    }
33
34
    public static function tearDownAfterClass2222222222()
35
    {
36
        $files = new \RecursiveIteratorIterator(
37
            new \RecursiveDirectoryIterator(
38
                self::$projectPath,
39
                \RecursiveDirectoryIterator::SKIP_DOTS
40
            ),
41
            \RecursiveIteratorIterator::CHILD_FIRST
42
        );
43
        foreach ($files as $file) {
44
            if ($file->isDir()) {
45
                rmdir($file->getRealPath());
46
            } else {
47
                unlink($file->getRealPath());
48
            }
49
        }
50
51
        chdir(self::$oldWorkingDirectory);
52
53
        if (rmdir(self::$projectPath)) {
54
            unlink(self::$projectLockFile);
55
        }
56
57
        parent::tearDownAfterClass();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (tearDownAfterClass() instead of tearDownAfterClass2222222222()). Are you sure this is correct? If so, you might want to change this to $this->tearDownAfterClass().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
58
    }
59
60
    /**
61
     * Runs a command and compares result against expection.
62
     *
63
     * @param string        $command        The command to execute
64
     * @param mixed         $expectedResult Expected exit code
65
     * @param string[]|null $expectedOutput Expected stdout as array of lines (null to skip check)
66
     * @param string        $message        Description of the assertion
67
     */
68
    protected static function assertCommand($command, $expectedResult, $expectedOutput = null, $message = '')
69
    {
70
        $actualResult = null;
71
        $actualOutput = null;
72
        $command .= ' 2>&1';
73
74
        exec($command, $actualOutput, $actualResult);
75
76
        if (!$message) {
77
            $sep = PHP_EOL . '- ';
78
            $message = sprintf(
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $message. This often makes code more readable.
Loading history...
79
                'Command:%sResult (exit: %s):%s',
80
                $sep . $command . PHP_EOL,
81
                $actualResult,
82
                $sep . implode($sep, $actualOutput)
83
            );
84
        }
85
86
        if (!is_null($expectedResult)) {
87
            static::assertEquals($expectedResult, $actualResult, $message);
88
        }
89
90
        if (!is_null($expectedOutput)) {
91
            static::assertEquals($expectedOutput, $actualOutput, $message);
92
        }
93
    }
94
95
    /**
96
     * Runs a command and asserts that it was successful.
97
     *
98
     * @param string $command The command to execute
99
     * @param string $message Description of the assertion
100
     */
101
    protected static function assertCommandSuccessful($command, $message = '')
102
    {
103
        static::assertCommand($command, 0, null, $message);
104
    }
105
106
    /**
107
     * Returns command line to run ElderBrother.
108
     *
109
     * @return string
110
     */
111
    protected static function getEbCmd()
112
    {
113
        static $cache = null;
114
115
        if (!$cache) {
116
            $bin = __DIR__ . '/../../../elder-brother';
117
            $cache = 'php -f ' . escapeshellarg(realpath($bin)) . ' -- ';
118
        }
119
120
        return $cache;
121
    }
122
}
123