Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / phpunit / phpunit / src / Extensions / PhptTestCase.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 /**
12  * Runner for PHPT test cases.
13  *
14  * @since Class available since Release 3.1.4
15  */
16 class PHPUnit_Extensions_PhptTestCase implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
17 {
18     /**
19      * @var string
20      */
21     private $filename;
22
23     /**
24      * @var array
25      */
26     private $settings = array(
27         'allow_url_fopen=1',
28         'auto_append_file=',
29         'auto_prepend_file=',
30         'disable_functions=',
31         'display_errors=1',
32         'docref_root=',
33         'docref_ext=.html',
34         'error_append_string=',
35         'error_prepend_string=',
36         'error_reporting=-1',
37         'html_errors=0',
38         'log_errors=0',
39         'magic_quotes_runtime=0',
40         'output_handler=',
41         'open_basedir=',
42         'output_buffering=Off',
43         'report_memleaks=0',
44         'report_zend_debug=0',
45         'safe_mode=0',
46         'track_errors=1',
47         'xdebug.default_enable=0'
48     );
49
50     /**
51      * Constructs a test case with the given filename.
52      *
53      * @param string $filename
54      *
55      * @throws PHPUnit_Framework_Exception
56      */
57     public function __construct($filename)
58     {
59         if (!is_string($filename)) {
60             throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
61         }
62
63         if (!is_file($filename)) {
64             throw new PHPUnit_Framework_Exception(
65                 sprintf(
66                     'File "%s" does not exist.',
67                     $filename
68                 )
69             );
70         }
71
72         $this->filename = $filename;
73     }
74
75     /**
76      * Counts the number of test cases executed by run(TestResult result).
77      *
78      * @return int
79      */
80     public function count()
81     {
82         return 1;
83     }
84
85     /**
86      * Runs a test and collects its result in a TestResult instance.
87      *
88      * @param PHPUnit_Framework_TestResult $result
89      *
90      * @return PHPUnit_Framework_TestResult
91      */
92     public function run(PHPUnit_Framework_TestResult $result = null)
93     {
94         $sections = $this->parse();
95         $code     = $this->render($sections['FILE']);
96
97         if ($result === null) {
98             $result = new PHPUnit_Framework_TestResult;
99         }
100
101         $php      = PHPUnit_Util_PHP::factory();
102         $skip     = false;
103         $time     = 0;
104         $settings = $this->settings;
105
106         $result->startTest($this);
107
108         if (isset($sections['INI'])) {
109             $settings = array_merge($settings, $this->parseIniSection($sections['INI']));
110         }
111
112         if (isset($sections['SKIPIF'])) {
113             $jobResult = $php->runJob($sections['SKIPIF'], $settings);
114
115             if (!strncasecmp('skip', ltrim($jobResult['stdout']), 4)) {
116                 if (preg_match('/^\s*skip\s*(.+)\s*/i', $jobResult['stdout'], $message)) {
117                     $message = substr($message[1], 2);
118                 } else {
119                     $message = '';
120                 }
121
122                 $result->addFailure($this, new PHPUnit_Framework_SkippedTestError($message), 0);
123
124                 $skip = true;
125             }
126         }
127
128         if (!$skip) {
129             PHP_Timer::start();
130             $jobResult = $php->runJob($code, $settings);
131             $time      = PHP_Timer::stop();
132
133             if (isset($sections['EXPECT'])) {
134                 $assertion = 'assertEquals';
135                 $expected  = $sections['EXPECT'];
136             } else {
137                 $assertion = 'assertStringMatchesFormat';
138                 $expected  = $sections['EXPECTF'];
139             }
140
141             $output   = preg_replace('/\r\n/', "\n", trim($jobResult['stdout']));
142             $expected = preg_replace('/\r\n/', "\n", trim($expected));
143
144             try {
145                 PHPUnit_Framework_Assert::$assertion($expected, $output);
146             } catch (PHPUnit_Framework_AssertionFailedError $e) {
147                 $result->addFailure($this, $e, $time);
148             } catch (Throwable $t) {
149                 $result->addError($this, $t, $time);
150             } catch (Exception $e) {
151                 $result->addError($this, $e, $time);
152             }
153         }
154
155         $result->endTest($this, $time);
156
157         return $result;
158     }
159
160     /**
161      * Returns the name of the test case.
162      *
163      * @return string
164      */
165     public function getName()
166     {
167         return $this->toString();
168     }
169
170     /**
171      * Returns a string representation of the test case.
172      *
173      * @return string
174      */
175     public function toString()
176     {
177         return $this->filename;
178     }
179
180     /**
181      * @return array
182      *
183      * @throws PHPUnit_Framework_Exception
184      */
185     private function parse()
186     {
187         $sections = array();
188         $section  = '';
189
190         foreach (file($this->filename) as $line) {
191             if (preg_match('/^--([_A-Z]+)--/', $line, $result)) {
192                 $section            = $result[1];
193                 $sections[$section] = '';
194                 continue;
195             } elseif (empty($section)) {
196                 throw new PHPUnit_Framework_Exception('Invalid PHPT file');
197             }
198
199             $sections[$section] .= $line;
200         }
201
202         if (!isset($sections['FILE']) ||
203             (!isset($sections['EXPECT']) && !isset($sections['EXPECTF']))) {
204             throw new PHPUnit_Framework_Exception('Invalid PHPT file');
205         }
206
207         return $sections;
208     }
209
210     /**
211      * @param string $code
212      *
213      * @return string
214      */
215     private function render($code)
216     {
217         return str_replace(
218             array(
219             '__DIR__',
220             '__FILE__'
221             ),
222             array(
223             "'" . dirname($this->filename) . "'",
224             "'" . $this->filename . "'"
225             ),
226             $code
227         );
228     }
229
230     /**
231      * Parse --INI-- section key value pairs and return as array.
232      *
233      * @param string
234      *
235      * @return array
236      */
237     protected function parseIniSection($content)
238     {
239         return preg_split('/\n|\r/', $content, -1, PREG_SPLIT_NO_EMPTY);
240     }
241 }