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 * Base class for all PHPUnit Framework exceptions.
13 *
14 * Ensures that exceptions thrown during a test run do not leave stray
15 * references behind.
16 *
17 * Every Exception contains a stack trace. Each stack frame contains the 'args'
18 * of the called function. The function arguments can contain references to
19 * instantiated objects. The references prevent the objects from being
20 * destructed (until test results are eventually printed), so memory cannot be
21 * freed up.
22 *
23 * With enabled process isolation, test results are serialized in the child
24 * process and unserialized in the parent process. The stack trace of Exceptions
25 * may contain objects that cannot be serialized or unserialized (e.g., PDO
26 * connections). Unserializing user-space objects from the child process into
27 * the parent would break the intended encapsulation of process isolation.
28 *
29 * @see http://fabien.potencier.org/article/9/php-serialization-stack-traces-and-exceptions
30 */
31class PHPUnit_Framework_Exception extends RuntimeException implements PHPUnit_Exception
32{
33    /**
34     * @var array
35     */
36    protected $serializableTrace;
37
38    public function __construct($message = '', $code = 0, Exception $previous = null)
39    {
40        parent::__construct($message, $code, $previous);
41
42        $this->serializableTrace = $this->getTrace();
43        foreach ($this->serializableTrace as $i => $call) {
44            unset($this->serializableTrace[$i]['args']);
45        }
46    }
47
48    /**
49     * Returns the serializable trace (without 'args').
50     *
51     * @return array
52     */
53    public function getSerializableTrace()
54    {
55        return $this->serializableTrace;
56    }
57
58    /**
59     * @return string
60     */
61    public function __toString()
62    {
63        $string = PHPUnit_Framework_TestFailure::exceptionToString($this);
64
65        if ($trace = PHPUnit_Util_Filter::getFilteredStacktrace($this)) {
66            $string .= "\n" . $trace;
67        }
68
69        return $string;
70    }
71
72    public function __sleep()
73    {
74        return array_keys(get_object_vars($this));
75    }
76}
77