658cf5dd1b6c3dd54aea66fe07eaeab374f94b39
[yaffs-website] / vendor / phpunit / phpunit / src / Util / PHP / Default.php
1 <?php
2 /*
3  * This file is part of PHPUnit.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 use SebastianBergmann\Environment\Runtime;
12
13 /**
14  * Default utility for PHP sub-processes.
15  *
16  * @since Class available since Release 3.5.12
17  */
18 class PHPUnit_Util_PHP_Default extends PHPUnit_Util_PHP
19 {
20     /**
21      * Runs a single job (PHP code) using a separate PHP process.
22      *
23      * @param string $job
24      * @param array  $settings
25      *
26      * @return array
27      *
28      * @throws PHPUnit_Framework_Exception
29      */
30     public function runJob($job, array $settings = array())
31     {
32         $runtime = new Runtime;
33         $runtime = $runtime->getBinary() . $this->settingsToParameters($settings);
34
35         if ('phpdbg' === PHP_SAPI) {
36             $runtime .= ' -qrr ' . escapeshellarg(__DIR__ . '/eval-stdin.php');
37         }
38
39         $process = proc_open(
40             $runtime,
41             array(
42             0 => array('pipe', 'r'),
43             1 => array('pipe', 'w'),
44             2 => array('pipe', 'w')
45             ),
46             $pipes
47         );
48
49         if (!is_resource($process)) {
50             throw new PHPUnit_Framework_Exception(
51                 'Unable to spawn worker process'
52             );
53         }
54
55         $this->process($pipes[0], $job);
56         fclose($pipes[0]);
57
58         $stdout = stream_get_contents($pipes[1]);
59         fclose($pipes[1]);
60
61         $stderr = stream_get_contents($pipes[2]);
62         fclose($pipes[2]);
63
64         proc_close($process);
65         $this->cleanup();
66
67         return array('stdout' => $stdout, 'stderr' => $stderr);
68     }
69
70     /**
71      * @param resource $pipe
72      * @param string   $job
73      *
74      * @throws PHPUnit_Framework_Exception
75      *
76      * @since Method available since Release 3.5.12
77      */
78     protected function process($pipe, $job)
79     {
80         fwrite($pipe, $job);
81     }
82
83     /**
84      * @since Method available since Release 3.5.12
85      */
86     protected function cleanup()
87     {
88     }
89 }