Skip to content
This repository
Browse code

Refactor.

  • Loading branch information...
commit 3d3f631823e8e48ba8028bdf0703500db1703462 1 parent 353fe69
Sebastian Bergmann authored February 20, 2011
3  PHPUnit/Framework/TestCase.php
@@ -570,7 +570,8 @@ public function run(PHPUnit_Framework_TestResult $result = NULL)
570 570
 
571 571
             $this->prepareTemplate($template);
572 572
 
573  
-            PHPUnit_Util_PHP::runJob($template->render(), $this, $result);
  573
+            $php = PHPUnit_Util_PHP::factory();
  574
+            $php->runJob($template->render(), $this, $result);
574 575
         } else {
575 576
             $result->run($this);
576 577
         }
67  PHPUnit/Util/PHP.php
@@ -55,7 +55,7 @@
55 55
  * @link       http://www.phpunit.de/
56 56
  * @since      Class available since Release 3.4.0
57 57
  */
58  
-class PHPUnit_Util_PHP
  58
+abstract class PHPUnit_Util_PHP
59 59
 {
60 60
     /**
61 61
      * Path to the PHP interpreter that is to be used.
@@ -128,6 +128,19 @@ public static function getPhpBinary()
128 128
     }
129 129
 
130 130
     /**
  131
+     * @return PHPUnit_Util_PHP
  132
+     * @since  Method available since Release 3.5.12
  133
+     */
  134
+    public static function factory()
  135
+    {
  136
+        if (DIRECTORY_SEPARATOR == '\\') {
  137
+            return new PHPUnit_Util_PHP_Windows;
  138
+        }
  139
+
  140
+        return new PHPUnit_Util_PHP_Default;
  141
+    }
  142
+
  143
+    /**
131 144
      * Runs a single job (PHP code) using a separate PHP process.
132 145
      *
133 146
      * @param  string                       $job
@@ -136,17 +149,57 @@ public static function getPhpBinary()
136 149
      * @return array|null
137 150
      * @throws PHPUnit_Framework_Exception
138 151
      */
139  
-    public static function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit_Framework_TestResult $result = NULL)
  152
+    public function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit_Framework_TestResult $result = NULL)
140 153
     {
141  
-        if (DIRECTORY_SEPARATOR == '\\') {
142  
-            return PHPUnit_Util_PHP_Windows::runJob($job, $test, $result);
  154
+        $process = proc_open(
  155
+          self::getPhpBinary(), self::$descriptorSpec, $pipes
  156
+        );
  157
+
  158
+        if (!is_resource($process)) {
  159
+            throw new PHPUnit_Framework_Exception(
  160
+              'Unable to create process for process isolation.'
  161
+            );
  162
+        }
  163
+
  164
+        if ($result !== NULL) {
  165
+            $result->startTest($test);
  166
+        }
  167
+
  168
+        $this->process($pipes[0], $job);
  169
+        fclose($pipes[0]);
  170
+
  171
+        $stdout = stream_get_contents($pipes[1]);
  172
+        fclose($pipes[1]);
  173
+
  174
+        $stderr = stream_get_contents($pipes[2]);
  175
+        fclose($pipes[2]);
  176
+
  177
+        proc_close($process);
  178
+        $this->cleanup();
  179
+
  180
+        if ($result !== NULL) {
  181
+            $this->processChildResult($test, $result, $stdout, $stderr);
  182
+        } else {
  183
+            return array('stdout' => $stdout, 'stderr' => $stderr);
143 184
         }
  185
+    }
144 186
 
145  
-        return PHPUnit_Util_PHP_Default::runJob($job, $test, $result);
  187
+    /**
  188
+     * @param resource $pipe
  189
+     * @param string   $job
  190
+     * @since Method available since Release 3.5.12
  191
+     */
  192
+    abstract protected function process($pipe, $job);
  193
+
  194
+    /**
  195
+     * @since Method available since Release 3.5.12
  196
+     */
  197
+    protected function cleanup()
  198
+    {
146 199
     }
147 200
 
148 201
     /**
149  
-     * Runs a single job (PHP code) using a separate PHP process.
  202
+     * Processes the TestResult object from an isolated process.
150 203
      *
151 204
      * @param PHPUnit_Framework_TestCase   $test
152 205
      * @param PHPUnit_Framework_TestResult $result
@@ -154,7 +207,7 @@ public static function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit
154 207
      * @param string                       $stderr
155 208
      * @since Method available since Release 3.5.0
156 209
      */
157  
-    protected static function processChildResult(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result, $stdout, $stderr)
  210
+    protected function processChildResult(PHPUnit_Framework_Test $test, PHPUnit_Framework_TestResult $result, $stdout, $stderr)
158 211
     {
159 212
         if (!empty($stderr)) {
160 213
             $time = 0;
37  PHPUnit/Util/PHP/Default.php
@@ -58,40 +58,11 @@
58 58
 class PHPUnit_Util_PHP_Default extends PHPUnit_Util_PHP
59 59
 {
60 60
     /**
61  
-     * Runs a single job (PHP code) using a separate PHP process.
62  
-     *
63  
-     * @param  string                       $job
64  
-     * @param  PHPUnit_Framework_TestCase   $test
65  
-     * @param  PHPUnit_Framework_TestResult $result
66  
-     * @return array|null
  61
+     * @param resource $pipe
  62
+     * @since Method available since Release 3.5.12
67 63
      */
68  
-    public static function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit_Framework_TestResult $result = NULL)
  64
+    protected function process($pipe, $job)
69 65
     {
70  
-        $process = proc_open(
71  
-          self::getPhpBinary(), self::$descriptorSpec, $pipes
72  
-        );
73  
-
74  
-        if (is_resource($process)) {
75  
-            if ($result !== NULL) {
76  
-                $result->startTest($test);
77  
-            }
78  
-
79  
-            fwrite($pipes[0], $job);
80  
-            fclose($pipes[0]);
81  
-
82  
-            $stdout = stream_get_contents($pipes[1]);
83  
-            fclose($pipes[1]);
84  
-
85  
-            $stderr = stream_get_contents($pipes[2]);
86  
-            fclose($pipes[2]);
87  
-
88  
-            proc_close($process);
89  
-
90  
-            if ($result !== NULL) {
91  
-                self::processChildResult($test, $result, $stdout, $stderr);
92  
-            } else {
93  
-                return array('stdout' => $stdout, 'stderr' => $stderr);
94  
-            }
95  
-        }
  66
+        fwrite($pipe, $job);
96 67
     }
97 68
 }
56  PHPUnit/Util/PHP/Windows.php
@@ -58,50 +58,34 @@
58 58
 class PHPUnit_Util_PHP_Windows extends PHPUnit_Util_PHP
59 59
 {
60 60
     /**
61  
-     * Runs a single job (PHP code) using a separate PHP process.
62  
-     *
63  
-     * @param  string                       $job
64  
-     * @param  PHPUnit_Framework_TestCase   $test
65  
-     * @param  PHPUnit_Framework_TestResult $result
66  
-     * @return array|null
67  
-     * @throws PHPUnit_Framework_Exception
  61
+     * @var string
68 62
      */
69  
-    public static function runJob($job, PHPUnit_Framework_Test $test = NULL, PHPUnit_Framework_TestResult $result = NULL)
  63
+    protected $tempFile;
  64
+
  65
+    /**
  66
+     * @param resource $pipe
  67
+     * @since Method available since Release 3.5.12
  68
+     */
  69
+    protected function process($pipe, $job)
70 70
     {
71  
-        if(!($file = tempnam(sys_get_temp_dir(), 'PHPUnit')) || file_put_contents($file, $job) === FALSE) {
  71
+        if (!($this->tempFile = tempnam(sys_get_temp_dir(), 'PHPUnit')) ||
  72
+            file_put_contents($this->tempFile, $job) === FALSE) {
72 73
             throw new PHPUnit_Framework_Exception(
73 74
               'Unable to write temporary files for process isolation.'
74 75
             );
75 76
         }
76 77
 
77  
-        $process = proc_open(
78  
-          self::getPhpBinary(), self::$descriptorSpec, $pipes
  78
+        fwrite(
  79
+          $pipe,
  80
+          "<?php require_once '" . addcslashes($this->tempFile, "'") .  "'; ?>"
79 81
         );
  82
+    }
80 83
 
81  
-        if (is_resource($process)) {
82  
-            if ($result !== NULL) {
83  
-                $result->startTest($test);
84  
-            }
85  
-
86  
-            fwrite($pipes[0], "<?php require_once '" . addcslashes($file, "'") .  "'; ?>");
87  
-            fclose($pipes[0]);
88  
-
89  
-            $stdout = stream_get_contents($pipes[1]);
90  
-            fclose($pipes[1]);
91  
-
92  
-            $stderr = stream_get_contents($pipes[2]);
93  
-            fclose($pipes[2]);
94  
-
95  
-            proc_close($process);
96  
-            unlink($file);
97  
-
98  
-            if ($result !== NULL) {
99  
-                self::processChildResult($test, $result, $stdout, $stderr);
100  
-            } else {
101  
-                return array('stdout' => $stdout, 'stderr' => $stderr);
102  
-            }
103  
-        } else {
104  
-            unlink($file);
105  
-        }
  84
+    /**
  85
+     * @since Method available since Release 3.5.12
  86
+     */
  87
+    protected function cleanup()
  88
+    {
  89
+        unlink($this->tempFile);
106 90
     }
107 91
 }

0 notes on commit 3d3f631

Please sign in to comment.
Something went wrong with that request. Please try again.