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 2.3.0
44
 */
45
 
46
require_once 'PHPUnit/Framework.php';
47
require_once 'PHPUnit/Util/Class.php';
48
require_once 'PHPUnit/Util/Filter.php';
49
require_once 'PHPUnit/Util/Printer.php';
50
require_once 'PHPUnit/Util/XML.php';
51
 
52
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
53
 
54
/**
55
 * A TestListener that generates a logfile of the test execution in XML markup.
56
 *
57
 * The XML markup used is the same as the one that is used by the JUnit Ant task.
58
 *
59
 * @category   Testing
60
 * @package    PHPUnit
61
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
62
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
63
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
64
 * @version    Release: 3.4.15
65
 * @link       http://www.phpunit.de/
66
 * @since      Class available since Release 2.1.0
67
 */
68
class PHPUnit_Util_Log_JUnit extends PHPUnit_Util_Printer implements PHPUnit_Framework_TestListener
69
{
70
    /**
71
     * @var    DOMDocument
72
     */
73
    protected $document;
74
 
75
    /**
76
     * @var    DOMElement
77
     */
78
    protected $root;
79
 
80
    /**
81
     * @var    boolean
82
     */
83
    protected $logIncompleteSkipped = FALSE;
84
 
85
    /**
86
     * @var    boolean
87
     */
88
    protected $writeDocument = TRUE;
89
 
90
    /**
91
     * @var    DOMElement[]
92
     */
93
    protected $testSuites = array();
94
 
95
    /**
96
     * @var    integer[]
97
     */
98
    protected $testSuiteTests = array(0);
99
 
100
    /**
101
     * @var    integer[]
102
     */
103
    protected $testSuiteAssertions = array(0);
104
 
105
    /**
106
     * @var    integer[]
107
     */
108
    protected $testSuiteErrors = array(0);
109
 
110
    /**
111
     * @var    integer[]
112
     */
113
    protected $testSuiteFailures = array(0);
114
 
115
    /**
116
     * @var    integer[]
117
     */
118
    protected $testSuiteTimes = array(0);
119
 
120
    /**
121
     * @var    integer
122
     */
123
    protected $testSuiteLevel = 0;
124
 
125
    /**
126
     * @var    DOMElement
127
     */
128
    protected $currentTestCase = NULL;
129
 
130
    /**
131
     * @var    boolean
132
     */
133
    protected $attachCurrentTestCase = TRUE;
134
 
135
    /**
136
     * Constructor.
137
     *
138
     * @param  mixed   $out
139
     * @param  boolean $logIncompleteSkipped
140
     */
141
    public function __construct($out = NULL, $logIncompleteSkipped = FALSE)
142
    {
143
        $this->document = new DOMDocument('1.0', 'UTF-8');
144
        $this->document->formatOutput = TRUE;
145
 
146
        $this->root = $this->document->createElement('testsuites');
147
        $this->document->appendChild($this->root);
148
 
149
        parent::__construct($out);
150
 
151
        $this->logIncompleteSkipped = $logIncompleteSkipped;
152
    }
153
 
154
    /**
155
     * Flush buffer and close output.
156
     *
157
     */
158
    public function flush()
159
    {
160
        if ($this->writeDocument === TRUE) {
161
            $this->write($this->getXML());
162
        }
163
 
164
        parent::flush();
165
    }
166
 
167
    /**
168
     * An error occurred.
169
     *
170
     * @param  PHPUnit_Framework_Test $test
171
     * @param  Exception              $e
172
     * @param  float                  $time
173
     */
174
    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
175
    {
176
        if ($this->currentTestCase !== NULL) {
177
            if ($test instanceof PHPUnit_Framework_SelfDescribing) {
178
                $buffer = $test->toString() . "\n";
179
            } else {
180
                $buffer = '';
181
            }
182
 
183
            $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e) .
184
                       "\n" .
185
                       PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE);
186
 
187
            $error = $this->document->createElement(
188
              'error', PHPUnit_Util_XML::prepareString($buffer)
189
            );
190
 
191
            $error->setAttribute('type', get_class($e));
192
 
193
            $this->currentTestCase->appendChild($error);
194
 
195
            $this->testSuiteErrors[$this->testSuiteLevel]++;
196
        }
197
    }
198
 
199
    /**
200
     * A failure occurred.
201
     *
202
     * @param  PHPUnit_Framework_Test                 $test
203
     * @param  PHPUnit_Framework_AssertionFailedError $e
204
     * @param  float                                  $time
205
     */
206
    public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
207
    {
208
        if ($this->currentTestCase !== NULL) {
209
            if (!$test instanceof PHPUnit_Framework_Warning) {
210
                if ($test instanceof PHPUnit_Framework_SelfDescribing) {
211
                    $buffer = $test->toString() . "\n";
212
                } else {
213
                    $buffer = '';
214
                }
215
 
216
                $buffer .= PHPUnit_Framework_TestFailure::exceptionToString($e).
217
                           "\n" .
218
                           PHPUnit_Util_Filter::getFilteredStacktrace(
219
                             $e, FALSE
220
                           );
221
 
222
                $failure = $this->document->createElement(
223
                  'failure', PHPUnit_Util_XML::prepareString($buffer)
224
                );
225
 
226
                $failure->setAttribute('type', get_class($e));
227
 
228
                $this->currentTestCase->appendChild($failure);
229
 
230
                $this->testSuiteFailures[$this->testSuiteLevel]++;
231
            }
232
        }
233
    }
234
 
235
    /**
236
     * Incomplete test.
237
     *
238
     * @param  PHPUnit_Framework_Test $test
239
     * @param  Exception              $e
240
     * @param  float                  $time
241
     */
242
    public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
243
    {
244
        if ($this->logIncompleteSkipped && $this->currentTestCase !== NULL) {
245
            $error = $this->document->createElement(
246
              'error',
247
              PHPUnit_Util_XML::prepareString(
248
                "Incomplete Test\n" .
249
                PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE)
250
              )
251
            );
252
 
253
            $error->setAttribute('type', get_class($e));
254
 
255
            $this->currentTestCase->appendChild($error);
256
 
257
            $this->testSuiteErrors[$this->testSuiteLevel]++;
258
        } else {
259
            $this->attachCurrentTestCase = FALSE;
260
        }
261
    }
262
 
263
    /**
264
     * Skipped test.
265
     *
266
     * @param  PHPUnit_Framework_Test $test
267
     * @param  Exception              $e
268
     * @param  float                  $time
269
     * @since  Method available since Release 3.0.0
270
     */
271
    public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
272
    {
273
        if ($this->logIncompleteSkipped && $this->currentTestCase !== NULL) {
274
            $error = $this->document->createElement(
275
              'error',
276
              PHPUnit_Util_XML::prepareString(
277
                "Skipped Test\n" .
278
                PHPUnit_Util_Filter::getFilteredStacktrace($e, FALSE)
279
              )
280
            );
281
 
282
            $error->setAttribute('type', get_class($e));
283
 
284
            $this->currentTestCase->appendChild($error);
285
 
286
            $this->testSuiteErrors[$this->testSuiteLevel]++;
287
        } else {
288
            $this->attachCurrentTestCase = FALSE;
289
        }
290
    }
291
 
292
    /**
293
     * A testsuite started.
294
     *
295
     * @param  PHPUnit_Framework_TestSuite $suite
296
     * @since  Method available since Release 2.2.0
297
     */
298
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
299
    {
300
        $testSuite = $this->document->createElement('testsuite');
301
        $testSuite->setAttribute('name', $suite->getName());
302
 
303
        if (class_exists($suite->getName(), FALSE)) {
304
            try {
305
                $class = new ReflectionClass($suite->getName());
306
 
307
                $testSuite->setAttribute('file', $class->getFileName());
308
 
309
                $packageInformation = PHPUnit_Util_Class::getPackageInformation(
310
                  $suite->getName(), $class->getDocComment()
311
                );
312
 
313
                if (!empty($packageInformation['namespace'])) {
314
                    $testSuite->setAttribute(
315
                      'namespace', $packageInformation['namespace']
316
                    );
317
                }
318
 
319
                if (!empty($packageInformation['fullPackage'])) {
320
                    $testSuite->setAttribute(
321
                      'fullPackage', $packageInformation['fullPackage']
322
                    );
323
                }
324
 
325
                if (!empty($packageInformation['category'])) {
326
                    $testSuite->setAttribute(
327
                      'category', $packageInformation['category']
328
                    );
329
                }
330
 
331
                if (!empty($packageInformation['package'])) {
332
                    $testSuite->setAttribute(
333
                      'package', $packageInformation['package']
334
                    );
335
                }
336
 
337
                if (!empty($packageInformation['subpackage'])) {
338
                    $testSuite->setAttribute(
339
                      'subpackage', $packageInformation['subpackage']
340
                    );
341
                }
342
            }
343
 
344
            catch (ReflectionException $e) {
345
            }
346
        }
347
 
348
        if ($this->testSuiteLevel > 0) {
349
            $this->testSuites[$this->testSuiteLevel]->appendChild($testSuite);
350
        } else {
351
            $this->root->appendChild($testSuite);
352
        }
353
 
354
        $this->testSuiteLevel++;
355
        $this->testSuites[$this->testSuiteLevel]          = $testSuite;
356
        $this->testSuiteTests[$this->testSuiteLevel]      = 0;
357
        $this->testSuiteAssertions[$this->testSuiteLevel] = 0;
358
        $this->testSuiteErrors[$this->testSuiteLevel]     = 0;
359
        $this->testSuiteFailures[$this->testSuiteLevel]   = 0;
360
        $this->testSuiteTimes[$this->testSuiteLevel]      = 0;
361
    }
362
 
363
    /**
364
     * A testsuite ended.
365
     *
366
     * @param  PHPUnit_Framework_TestSuite $suite
367
     * @since  Method available since Release 2.2.0
368
     */
369
    public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
370
    {
371
        $this->testSuites[$this->testSuiteLevel]->setAttribute(
372
          'tests', $this->testSuiteTests[$this->testSuiteLevel]
373
        );
374
 
375
        $this->testSuites[$this->testSuiteLevel]->setAttribute(
376
          'assertions', $this->testSuiteAssertions[$this->testSuiteLevel]
377
        );
378
 
379
        $this->testSuites[$this->testSuiteLevel]->setAttribute(
380
          'failures', $this->testSuiteFailures[$this->testSuiteLevel]
381
        );
382
 
383
        $this->testSuites[$this->testSuiteLevel]->setAttribute(
384
          'errors', $this->testSuiteErrors[$this->testSuiteLevel]
385
        );
386
 
387
        $this->testSuites[$this->testSuiteLevel]->setAttribute(
388
          'time', sprintf('%F', $this->testSuiteTimes[$this->testSuiteLevel])
389
        );
390
 
391
        if ($this->testSuiteLevel > 1) {
392
            $this->testSuiteTests[$this->testSuiteLevel - 1]      += $this->testSuiteTests[$this->testSuiteLevel];
393
            $this->testSuiteAssertions[$this->testSuiteLevel - 1] += $this->testSuiteAssertions[$this->testSuiteLevel];
394
            $this->testSuiteErrors[$this->testSuiteLevel - 1]     += $this->testSuiteErrors[$this->testSuiteLevel];
395
            $this->testSuiteFailures[$this->testSuiteLevel - 1]   += $this->testSuiteFailures[$this->testSuiteLevel];
396
            $this->testSuiteTimes[$this->testSuiteLevel - 1]      += $this->testSuiteTimes[$this->testSuiteLevel];
397
        }
398
 
399
        $this->testSuiteLevel--;
400
    }
401
 
402
    /**
403
     * A test started.
404
     *
405
     * @param  PHPUnit_Framework_Test $test
406
     */
407
    public function startTest(PHPUnit_Framework_Test $test)
408
    {
409
        if (!$test instanceof PHPUnit_Framework_Warning) {
410
            $testCase = $this->document->createElement('testcase');
411
            $testCase->setAttribute('name', $test->getName());
412
 
413
            if ($test instanceof PHPUnit_Framework_TestCase) {
414
                $class      = new ReflectionClass($test);
415
                $methodName = $test->getName();
416
 
417
                if ($class->hasMethod($methodName)) {
418
                    $method = $class->getMethod($test->getName());
419
 
420
                    $testCase->setAttribute('class', $class->getName());
421
                    $testCase->setAttribute('file', $class->getFileName());
422
                    $testCase->setAttribute('line', $method->getStartLine());
423
                }
424
            }
425
 
426
            $this->currentTestCase = $testCase;
427
        }
428
    }
429
 
430
    /**
431
     * A test ended.
432
     *
433
     * @param  PHPUnit_Framework_Test $test
434
     * @param  float                  $time
435
     */
436
    public function endTest(PHPUnit_Framework_Test $test, $time)
437
    {
438
        if (!$test instanceof PHPUnit_Framework_Warning) {
439
            if ($this->attachCurrentTestCase) {
440
                if ($test instanceof PHPUnit_Framework_TestCase) {
441
                    $numAssertions = $test->getNumAssertions();
442
                    $this->testSuiteAssertions[$this->testSuiteLevel] += $numAssertions;
443
 
444
                    $this->currentTestCase->setAttribute(
445
                      'assertions', $numAssertions
446
                    );
447
                }
448
 
449
                $this->currentTestCase->setAttribute(
450
                  'time', sprintf('%F', $time)
451
                );
452
 
453
                $this->testSuites[$this->testSuiteLevel]->appendChild(
454
                  $this->currentTestCase
455
                );
456
 
457
                $this->testSuiteTests[$this->testSuiteLevel]++;
458
                $this->testSuiteTimes[$this->testSuiteLevel] += $time;
459
            }
460
        }
461
 
462
        $this->attachCurrentTestCase = TRUE;
463
        $this->currentTestCase       = NULL;
464
    }
465
 
466
    /**
467
     * Returns the XML as a string.
468
     *
469
     * @return string
470
     * @since  Method available since Release 2.2.0
471
     */
472
    public function getXML()
473
    {
474
        return $this->document->saveXML();
475
    }
476
 
477
    /**
478
     * Enables or disables the writing of the document
479
     * in flush().
480
     *
481
     * This is a "hack" needed for the integration of
482
     * PHPUnit with Phing.
483
     *
484
     * @return string
485
     * @since  Method available since Release 2.2.0
486
     */
487
    public function setWriteDocument($flag)
488
    {
489
        if (is_bool($flag)) {
490
            $this->writeDocument = $flag;
491
        }
492
    }
493
}
494
?>