fa3e9ee14632d4662516eb0571265c8ddfaeb227
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / ExceptionWrapper.php
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  * Wraps Exceptions thrown by code under test.
13  *
14  * Re-instantiates Exceptions thrown by user-space code to retain their original
15  * class names, properties, and stack traces (but without arguments).
16  *
17  * Unlike PHPUnit_Framework_Exception, the complete stack of previous Exceptions
18  * is processed.
19  *
20  * @since Class available since Release 4.3.0
21  */
22 class PHPUnit_Framework_ExceptionWrapper extends PHPUnit_Framework_Exception
23 {
24     /**
25      * @var string
26      */
27     protected $classname;
28
29     /**
30      * @var PHPUnit_Framework_ExceptionWrapper|null
31      */
32     protected $previous;
33
34     /**
35      * @param Throwable|Exception $e
36      */
37     public function __construct($e)
38     {
39         // PDOException::getCode() is a string.
40         // @see http://php.net/manual/en/class.pdoexception.php#95812
41         parent::__construct($e->getMessage(), (int) $e->getCode());
42
43         $this->classname = get_class($e);
44         $this->file      = $e->getFile();
45         $this->line      = $e->getLine();
46
47         $this->serializableTrace = $e->getTrace();
48
49         foreach ($this->serializableTrace as $i => $call) {
50             unset($this->serializableTrace[$i]['args']);
51         }
52
53         if ($e->getPrevious()) {
54             $this->previous = new self($e->getPrevious());
55         }
56     }
57
58     /**
59      * @return string
60      */
61     public function getClassname()
62     {
63         return $this->classname;
64     }
65
66     /**
67      * @return PHPUnit_Framework_ExceptionWrapper
68      */
69     public function getPreviousWrapped()
70     {
71         return $this->previous;
72     }
73
74     /**
75      * @return string
76      */
77     public function __toString()
78     {
79         $string = PHPUnit_Framework_TestFailure::exceptionToString($this);
80
81         if ($trace = PHPUnit_Util_Filter::getFilteredStacktrace($this)) {
82             $string .= "\n" . $trace;
83         }
84
85         if ($this->previous) {
86             $string .= "\nCaused by\n" . $this->previous;
87         }
88
89         return $string;
90     }
91 }