Test Failed
Push — master ( e042c7...883f5c )
by Hannes
02:15
created

ProcessRunner   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 20 3
A __construct() 0 3 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace hanneskod\readmetester\Runner;
6
7
use hanneskod\readmetester\Parser\CodeBlock;
8
use Symfony\Component\Process\PhpProcess;
9
10
/**
11
 * Execute code in isolation using symfony php-process
12
 */
13
class ProcessRunner implements RunnerInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $bootstrapCode;
19
20
    public function __construct(string $bootstrap = '')
21
    {
22
        $this->bootstrapCode = $bootstrap ? "require '$bootstrap';" : '';
23
    }
24
25
    public function run(CodeBlock $codeBlock): OutcomeInterface
26
    {
27
        $filename = tempnam(sys_get_temp_dir(), 'doctestphp');
28
29
        file_put_contents($filename, "<?php $codeBlock");
30
31
        $process = new PhpProcess("<?php {$this->bootstrapCode} require '$filename';");
32
        $process->run();
33
34
        unlink($filename);
35
36
        if ($errorOutput = $process->getErrorOutput()) {
37
            return new ErrorOutcome(trim($errorOutput));
38
        }
39
40
        if ($output = $process->getOutput()) {
41
            return new OutputOutcome($output);
42
        }
43
44
        return new VoidOutcome;
45
    }
46
}
47