Moodle 2.3.1 release
[moodle.git] / lib / phpunit / classes / hint_resultprinter.php
CommitLineData
ed7259d1
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Helper test listener.
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27/**
28 * Helper test listener that prints command necessary
29 * for execution of failed test.
30 *
31 * @package core
32 * @category phpunit
33 * @copyright 2012 Petr Skoda {@link http://skodak.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class Hint_ResultPrinter extends PHPUnit_TextUI_ResultPrinter {
37 protected function printDefectTrace(PHPUnit_Framework_TestFailure $defect) {
38 global $CFG;
39
40 parent::printDefectTrace($defect);
41
42 $failedTest = $defect->failedTest();
43 $testName = get_class($failedTest);
44
45 $exception = $defect->thrownException();
46 $trace = $exception->getTrace();
f32ecd2f
PS
47
48 if (class_exists('ReflectionClass')) {
49 $reflection = new ReflectionClass($testName);
50 $file = $reflection->getFileName();
51
52 } else {
53 $file = false;
54 $dirroot = realpath($CFG->dirroot).DIRECTORY_SEPARATOR;
55 $classpath = realpath("$CFG->dirroot/lib/phpunit/classes").DIRECTORY_SEPARATOR;
56 foreach ($trace as $item) {
57 if (strpos($item['file'], $dirroot) === 0 and strpos($item['file'], $classpath) !== 0) {
58 if ($content = file_get_contents($item['file'])) {
59 if (preg_match('/class\s+'.$testName.'\s+extends/', $content)) {
60 $file = $item['file'];
61 break;
62 }
63 }
64 }
ed7259d1
PS
65 }
66 }
f32ecd2f
PS
67
68 if ($file === false) {
ed7259d1
PS
69 return;
70 }
71
72 $cwd = getcwd();
73 if (strpos($file, $cwd) === 0) {
74 $file = substr($file, strlen($cwd)+1);
75 }
76
36e686a0 77 $this->write("\nTo re-run:\n phpunit $testName $file\n");
ed7259d1
PS
78 }
79}