Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PHPUnit
4
 *
5
 * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *
15
 *   * Redistributions in binary form must reproduce the above copyright
16
 *     notice, this list of conditions and the following disclaimer in
17
 *     the documentation and/or other materials provided with the
18
 *     distribution.
19
 *
20
 *   * Neither the name of Sebastian Bergmann nor the names of his
21
 *     contributors may be used to endorse or promote products derived
22
 *     from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @category   Testing
38
 * @package    PHPUnit
39
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link       http://www.phpunit.de/
43
 * @since      File available since Release 3.1.4
44
 */
45
 
46
if (PHPUnit_Util_Filesystem::fileExistsInIncludePath('PEAR/RunTest.php')) {
47
    $currentErrorReporting = error_reporting(E_ERROR | E_WARNING | E_PARSE);
48
    PHPUnit_Util_Filesystem::collectStart();
49
    require_once 'PEAR/RunTest.php';
50
    error_reporting($currentErrorReporting);
51
 
52
    PHPUnit_Util_Filesystem::collectEndAndAddToBlacklist();
53
}
54
 
55
require_once 'PHPUnit/Framework.php';
56
require_once 'PHPUnit/Extensions/PhptTestCase/Logger.php';
57
require_once 'PHPUnit/Util/Filter.php';
58
 
59
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
60
 
61
/**
62
 * Wrapper to run .phpt test cases.
63
 *
64
 * @category   Testing
65
 * @package    PHPUnit
66
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
67
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
68
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
69
 * @version    Release: 3.4.15
70
 * @link       http://www.phpunit.de/
71
 * @since      Class available since Release 3.1.4
72
 */
73
class PHPUnit_Extensions_PhptTestCase implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
74
{
75
    /**
76
     * The filename of the .phpt file.
77
     *
78
     * @var    string
79
     */
80
    protected $filename;
81
 
82
    /**
83
     * Options for PEAR_RunTest.
84
     *
85
     * @var    array
86
     */
87
    protected $options = array();
88
 
89
    /**
90
     * Constructs a test case with the given filename.
91
     *
92
     * @param  string $filename
93
     * @param  array  $options
94
     */
95
    public function __construct($filename, array $options = array())
96
    {
97
        if (!is_string($filename)) {
98
            throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
99
        }
100
 
101
        if (!is_file($filename)) {
102
            throw new PHPUnit_Framework_Exception(
103
              sprintf(
104
                'File "%s" does not exist.',
105
                $filename
106
              )
107
            );
108
        }
109
 
110
        $this->filename = $filename;
111
        $this->options  = $options;
112
    }
113
 
114
    /**
115
     * Counts the number of test cases executed by run(TestResult result).
116
     *
117
     * @return integer
118
     */
119
    public function count()
120
    {
121
        return 1;
122
    }
123
 
124
    /**
125
     * Runs a test and collects its result in a TestResult instance.
126
     *
127
     * @param  PHPUnit_Framework_TestResult $result
128
     * @param  array                        $options
129
     * @return PHPUnit_Framework_TestResult
130
     */
131
    public function run(PHPUnit_Framework_TestResult $result = NULL, array $options = array())
132
    {
133
        if (!class_exists('PEAR_RunTest', FALSE)) {
134
            throw new PHPUnit_Framework_Exception('Class PEAR_RunTest not found.');
135
        }
136
 
137
        if (isset($GLOBALS['_PEAR_destructor_object_list']) &&
138
            is_array($GLOBALS['_PEAR_destructor_object_list']) &&
139
            !empty($GLOBALS['_PEAR_destructor_object_list'])) {
140
            $pearDestructorObjectListCount = count($GLOBALS['_PEAR_destructor_object_list']);
141
        } else {
142
            $pearDestructorObjectListCount = 0;
143
        }
144
 
145
        if ($result === NULL) {
146
            $result = new PHPUnit_Framework_TestResult;
147
        }
148
 
149
        $coverage = $result->getCollectCodeCoverageInformation();
150
        $options  = array_merge($options, $this->options);
151
 
152
        if (!isset($options['include_path'])) {
153
            $options['include_path'] = get_include_path();
154
        }
155
 
156
        if ($coverage) {
157
            $options['coverage'] = TRUE;
158
        } else {
159
            $options['coverage'] = FALSE;
160
        }
161
 
162
        $currentErrorReporting = error_reporting(E_ERROR | E_WARNING | E_PARSE);
163
        $runner                = new PEAR_RunTest(new PHPUnit_Extensions_PhptTestCase_Logger, $options);
164
 
165
        if ($coverage) {
166
            $runner->xdebug_loaded = TRUE;
167
        } else {
168
            $runner->xdebug_loaded = FALSE;
169
        }
170
 
171
        $result->startTest($this);
172
 
173
        PHPUnit_Util_Timer::start();
174
        $buffer       = $runner->run($this->filename, $options);
175
        $time         = PHPUnit_Util_Timer::stop();
176
        error_reporting($currentErrorReporting);
177
        $base         = basename($this->filename);
178
        $path         = dirname($this->filename);
179
        $coverageFile = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.xdebug', $base);
180
        $diffFile     = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.diff', $base);
181
        $expFile      = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.exp', $base);
182
        $logFile      = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.log', $base);
183
        $outFile      = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.out', $base);
184
        $phpFile      = $path . DIRECTORY_SEPARATOR . str_replace('.phpt', '.php', $base);
185
 
186
        if (file_exists($phpFile)) {
187
            PHPUnit_Util_Filter::addFileToFilter($phpFile, 'TESTS');
188
        }
189
 
190
        if (is_object($buffer) && $buffer instanceof PEAR_Error) {
191
            $result->addError(
192
              $this,
193
              new RuntimeException($buffer->getMessage()),
194
              $time
195
            );
196
        }
197
 
198
        else if ($buffer == 'SKIPPED') {
199
            $result->addFailure($this, new PHPUnit_Framework_SkippedTestError, 0);
200
        }
201
 
202
        else if ($buffer != 'PASSED') {
203
            $result->addFailure(
204
              $this,
205
              PHPUnit_Framework_ComparisonFailure::diffEqual(
206
                file_get_contents($expFile),
207
                file_get_contents($outFile)
208
              ),
209
              $time
210
            );
211
        }
212
 
213
        foreach (array($diffFile, $expFile, $logFile, $phpFile, $outFile) as $file) {
214
            if (file_exists($file)) {
215
                unlink($file);
216
            }
217
        }
218
 
219
        if ($coverage && file_exists($coverageFile)) {
220
            eval('$coverageData = ' . file_get_contents($coverageFile) . ';');
221
            unset($coverageData[$phpFile]);
222
 
223
            $result->appendCodeCoverageInformation($this, $coverageData);
224
            unlink($coverageFile);
225
        }
226
 
227
        $result->endTest($this, $time);
228
 
229
        // Do not invoke PEAR's destructor mechanism for PHP 4
230
        // as it raises an E_STRICT.
231
        if ($pearDestructorObjectListCount == 0) {
232
            unset($GLOBALS['_PEAR_destructor_object_list']);
233
        } else {
234
            $count = count($GLOBALS['_PEAR_destructor_object_list']) - $pearDestructorObjectListCount;
235
 
236
            for ($i = 0; $i < $count; $i++) {
237
                array_pop($GLOBALS['_PEAR_destructor_object_list']);
238
            }
239
        }
240
 
241
        return $result;
242
    }
243
 
244
    /**
245
     * Returns the name of the test case.
246
     *
247
     * @return string
248
     */
249
    public function getName()
250
    {
251
        return $this->toString();
252
    }
253
 
254
    /**
255
     * Returns a string representation of the test case.
256
     *
257
     * @return string
258
     */
259
    public function toString()
260
    {
261
        return $this->filename;
262
    }
263
}
264
?>