Completed
Pull Request — master (#37)
by Christian
02:07
created

BaseProjectTest::assertCommand()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 14
nc 4
nop 4
1
<?php
2
3
namespace uuf6429\ElderBrother;
4
5
use Symfony\Component\Process\Process;
6
7
abstract class BaseProjectTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @var string Temporary directory for project under test
11
     */
12
    protected static $projectPath;
13
14
    /**
15
     * @var string File that ensure other tests won't influence this one
16
     */
17
    private static $projectLockFile;
18
19
    /**
20
     * @var string
21
     */
22
    private static $oldWorkingDirectory;
23
24
    public static function setUpBeforeClass()
25
    {
26
        parent::setUpBeforeClass();
27
28
        self::$projectLockFile = tempnam(sys_get_temp_dir(), 'test');
29
30
        self::assertNotEmpty(self::$projectLockFile);
31
32
        self::$projectPath = self::$projectLockFile . '_dir';
33
        mkdir(self::$projectPath);
34
35
        self::$oldWorkingDirectory = getcwd();
36
        chdir(self::$projectPath);
37
    }
38
39
    public static function tearDownAfterClass()
40
    {
41
        $files = new \RecursiveIteratorIterator(
42
            new \RecursiveDirectoryIterator(
43
                self::$projectPath,
44
                \RecursiveDirectoryIterator::SKIP_DOTS
45
            ),
46
            \RecursiveIteratorIterator::CHILD_FIRST
47
        );
48
        foreach ($files as $file) {
49
            if ($file->isDir()) {
50
                @rmdir($file->getRealPath());
51
            } else {
52
                @unlink($file->getRealPath());
53
            }
54
        }
55
56
        chdir(self::$oldWorkingDirectory);
57
58
        if (@rmdir(self::$projectPath)) {
59
            @unlink(self::$projectLockFile);
60
        }
61
62
        parent::tearDownAfterClass();
63
    }
64
65
    /**
66
     * Runs a command and compares result against expection.
67
     *
68
     * @param string        $command        The command to execute
69
     * @param int           $expectedResult Expected exit code
70
     * @param string[]|null $expectedOutput Expected stdout as array of lines (null to skip check)
71
     * @param string        $message        Description of the assertion
72
     */
73
    protected static function assertCommand($command, $expectedResult, $expectedOutput = null, $message = '')
74
    {
75
        $process = new Process($command);
76
        $process->run();
77
78
        $actualResult = $process->getExitCode();
79
        $actualOutput = explode("\n", str_replace(PHP_EOL, "\n", $process->getOutput() ?: $process->getErrorOutput()));
80
81
        if (!$message) {
82
            $message = sprintf(
83
                "Command:\n$ %s\nResult (exit: %d):\n> %s",
84
                $command,
85
                $process->getExitCode(),
86
                implode("\n> ", $actualOutput)
87
            );
88
        }
89
90
        static::assertEquals($expectedResult, $actualResult, $message);
91
92
        if (!is_null($expectedOutput)) {
93
            static::assertEquals($expectedOutput, $actualOutput, $message);
94
        }
95
    }
96
97
    /**
98
     * Runs a command and asserts that it was successful.
99
     *
100
     * @param string        $command        The command to execute
101
     * @param string[]|null $expectedOutput Expected stdout as array of lines (null to skip check)
102
     * @param string        $message        Description of the assertion
103
     */
104
    protected static function assertCommandSuccessful($command, $expectedOutput = null, $message = '')
105
    {
106
        static::assertCommand($command, 0, $expectedOutput, $message);
107
    }
108
109
    /**
110
     * Returns command line to run ElderBrother.
111
     *
112
     * @return string
113
     */
114
    protected static function getEbCmd()
115
    {
116
        static $cache = null;
117
118
        if (!$cache) {
119
            $bin = __DIR__ . '/../../../elder-brother';
120
            $cache = 'php -f ' . escapeshellarg(realpath($bin)) . ' -- ';
121
        }
122
123
        return $cache;
124
    }
125
}
126