Completed
Push — master ( a95a96...37a9eb )
by Christian
02:10
created

BaseProjectTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 118
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUpBeforeClass() 0 11 1
B assertCommand() 0 26 4
A assertCommandSuccessful() 0 4 1
A getEbCmd() 0 11 2
B tearDownAfterClass() 0 25 4
1
<?php
2
3
namespace uuf6429\ElderBrother;
4
5
abstract class BaseProjectTest extends \PHPUnit_Framework_TestCase
6
{
7
    /**
8
     * @var string 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';
28
        mkdir(self::$projectPath);
29
30
        self::$oldWorkingDirectory = getcwd();
31
        chdir(self::$projectPath);
32
    }
33
34
    public static function tearDownAfterClass()
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();
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(
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