~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/src/Util/PHP/Windows.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 * Windows utility for PHP sub-processes.
 
15
 *
 
16
 * @package    PHPUnit
 
17
 * @subpackage Util
 
18
 * @author     Sebastian Bergmann <sebastian@phpunit.de>
 
19
 * @copyright  Sebastian Bergmann <sebastian@phpunit.de>
 
20
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
21
 * @link       http://www.phpunit.de/
 
22
 * @since      Class available since Release 3.5.12
 
23
 */
 
24
class PHPUnit_Util_PHP_Windows extends PHPUnit_Util_PHP_Default
 
25
{
 
26
    /**
 
27
     * @var string
 
28
     */
 
29
    private $tempFile;
 
30
 
 
31
    /**
 
32
     * {@inheritdoc}
 
33
     *
 
34
     * Reading from STDOUT or STDERR hangs forever on Windows if the output is
 
35
     * too large.
 
36
     *
 
37
     * @see https://bugs.php.net/bug.php?id=51800
 
38
     */
 
39
    public function runJob($job, array $settings = array())
 
40
    {
 
41
        $runtime = new Runtime;
 
42
 
 
43
        if (false === $stdout_handle = tmpfile()) {
 
44
            throw new PHPUnit_Framework_Exception(
 
45
                'A temporary file could not be created; verify that your TEMP environment variable is writable'
 
46
            );
 
47
        }
 
48
 
 
49
        $process = proc_open(
 
50
            $runtime->getBinary() . $this->settingsToParameters($settings),
 
51
            array(
 
52
            0 => array('pipe', 'r'),
 
53
            1 => $stdout_handle,
 
54
            2 => array('pipe', 'w')
 
55
            ),
 
56
            $pipes
 
57
        );
 
58
 
 
59
        if (!is_resource($process)) {
 
60
            throw new PHPUnit_Framework_Exception(
 
61
                'Unable to spawn worker process'
 
62
            );
 
63
        }
 
64
 
 
65
        $this->process($pipes[0], $job);
 
66
        fclose($pipes[0]);
 
67
 
 
68
        $stderr = stream_get_contents($pipes[2]);
 
69
        fclose($pipes[2]);
 
70
 
 
71
        proc_close($process);
 
72
 
 
73
        rewind($stdout_handle);
 
74
        $stdout = stream_get_contents($stdout_handle);
 
75
        fclose($stdout_handle);
 
76
 
 
77
        $this->cleanup();
 
78
 
 
79
        return array('stdout' => $stdout, 'stderr' => $stderr);
 
80
    }
 
81
 
 
82
    /**
 
83
     * @param  resource                    $pipe
 
84
     * @param  string                      $job
 
85
     * @throws PHPUnit_Framework_Exception
 
86
     * @since  Method available since Release 3.5.12
 
87
     */
 
88
    protected function process($pipe, $job)
 
89
    {
 
90
        if (!($this->tempFile = tempnam(sys_get_temp_dir(), 'PHPUnit')) ||
 
91
            file_put_contents($this->tempFile, $job) === false) {
 
92
            throw new PHPUnit_Framework_Exception(
 
93
                'Unable to write temporary file'
 
94
            );
 
95
        }
 
96
 
 
97
        fwrite(
 
98
            $pipe,
 
99
            "<?php require_once " . var_export($this->tempFile, true) .  "; ?>"
 
100
        );
 
101
    }
 
102
 
 
103
    /**
 
104
     * @since Method available since Release 3.5.12
 
105
     */
 
106
    protected function cleanup()
 
107
    {
 
108
        unlink($this->tempFile);
 
109
    }
 
110
}