~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

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