MDL-32923 on failure print hint how to execute the failed test case
[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();
47 $file = null;
48 $dirroot = realpath($CFG->dirroot).DIRECTORY_SEPARATOR;
49 $classpath = realpath("$CFG->dirroot/lib/phpunit/classes").DIRECTORY_SEPARATOR;
50 foreach ($trace as $item) {
51 if (strpos($item['file'], $dirroot) === 0 and strpos($item['file'], $classpath) !== 0) {
52 $file = $item['file'];
53 break;
54 }
55 }
56 if ($file === null) {
57 return;
58 }
59
60 $cwd = getcwd();
61 if (strpos($file, $cwd) === 0) {
62 $file = substr($file, strlen($cwd)+1);
63 }
64
65 $this->write("\n phpunit $testName $file\n");
66 }
67}