4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Tests\Caster;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\VarDumper\Caster\Caster;
16 use Symfony\Component\VarDumper\Caster\ExceptionCaster;
17 use Symfony\Component\VarDumper\Caster\FrameStub;
18 use Symfony\Component\VarDumper\Cloner\VarCloner;
19 use Symfony\Component\VarDumper\Dumper\HtmlDumper;
20 use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
22 class ExceptionCasterTest extends TestCase
24 use VarDumperTestTrait;
26 private function getTestException($msg, &$ref = null)
28 return new \Exception(''.$msg);
31 protected function tearDown()
33 ExceptionCaster::$srcContext = 1;
34 ExceptionCaster::$traceArgs = true;
37 public function testDefaultSettings()
40 $e = $this->getTestException('foo', $ref);
42 $expectedDump = <<<'EODUMP'
46 #file: "%sExceptionCasterTest.php"
49 %s%eTests%eCaster%eExceptionCasterTest.php:28 {
51 › return new \Exception(''.$msg);
54 %s%eTests%eCaster%eExceptionCasterTest.php:40 { …}
55 Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->testDefaultSettings() {}
59 $this->assertDumpMatchesFormat($expectedDump, $e);
60 $this->assertSame(array('foo'), $ref);
63 public function testSeek()
65 $e = $this->getTestException(2);
67 $expectedDump = <<<'EODUMP'
69 %s%eTests%eCaster%eExceptionCasterTest.php:28 {
71 › return new \Exception(''.$msg);
74 %s%eTests%eCaster%eExceptionCasterTest.php:65 { …}
75 Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->testSeek() {}
79 $this->assertStringMatchesFormat($expectedDump, $this->getDump($e, 'trace'));
82 public function testNoArgs()
84 $e = $this->getTestException(1);
85 ExceptionCaster::$traceArgs = false;
87 $expectedDump = <<<'EODUMP'
91 #file: "%sExceptionCasterTest.php"
94 %sExceptionCasterTest.php:28 {
96 › return new \Exception(''.$msg);
99 %s%eTests%eCaster%eExceptionCasterTest.php:84 { …}
100 Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->testNoArgs() {}
104 $this->assertDumpMatchesFormat($expectedDump, $e);
107 public function testNoSrcContext()
109 $e = $this->getTestException(1);
110 ExceptionCaster::$srcContext = -1;
112 $expectedDump = <<<'EODUMP'
116 #file: "%sExceptionCasterTest.php"
119 %s%eTests%eCaster%eExceptionCasterTest.php:28
120 %s%eTests%eCaster%eExceptionCasterTest.php:%d
124 $this->assertDumpMatchesFormat($expectedDump, $e);
127 public function testHtmlDump()
129 if (ini_get('xdebug.file_link_format') || get_cfg_var('xdebug.file_link_format')) {
130 $this->markTestSkipped('A custom file_link_format is defined.');
133 $e = $this->getTestException(1);
134 ExceptionCaster::$srcContext = -1;
136 $cloner = new VarCloner();
137 $cloner->setMaxItems(1);
138 $dumper = new HtmlDumper();
139 $dumper->setDumpHeader('<foo></foo>');
140 $dumper->setDumpBoundaries('<bar>', '</bar>');
141 $dump = $dumper->dump($cloner->cloneVar($e)->withRefHandles(false), true);
143 $expectedDump = <<<'EODUMP'
144 <foo></foo><bar><span class=sf-dump-note>Exception</span> {<samp>
145 #<span class=sf-dump-protected title="Protected property">message</span>: "<span class=sf-dump-str>1</span>"
146 #<span class=sf-dump-protected title="Protected property">code</span>: <span class=sf-dump-num>0</span>
147 #<span class=sf-dump-protected title="Protected property">file</span>: "<span class=sf-dump-str title="%sExceptionCasterTest.php
148 %d characters"><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class=sf-dump-ellipsis>%e</span>Tests%eCaster%eExceptionCasterTest.php</span>"
149 #<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>28</span>
150 <span class=sf-dump-meta>trace</span>: {<samp>
151 <span class=sf-dump-meta title="%sExceptionCasterTest.php
152 Stack level %d."><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class=sf-dump-ellipsis>%e</span>Tests%eCaster%eExceptionCasterTest.php</span>:<span class=sf-dump-num>28</span>
159 $this->assertStringMatchesFormat($expectedDump, $dump);
163 * @requires function Twig\Template::getSourceContext
165 public function testFrameWithTwig()
167 require_once \dirname(__DIR__).'/Fixtures/Twig.php';
171 'file' => \dirname(__DIR__).'/Fixtures/Twig.php',
173 'class' => '__TwigTemplate_VarDumperFixture_u75a09',
176 'file' => \dirname(__DIR__).'/Fixtures/Twig.php',
178 'class' => '__TwigTemplate_VarDumperFixture_u75a09',
179 'object' => new \__TwigTemplate_VarDumperFixture_u75a09(null, __FILE__),
183 $expectedDump = <<<'EODUMP'
186 class: "__TwigTemplate_VarDumperFixture_u75a09"
196 class: "__TwigTemplate_VarDumperFixture_u75a09"
197 object: __TwigTemplate_VarDumperFixture_u75a09 {
201 %sExceptionCasterTest.php:2 {
212 $this->assertDumpMatchesFormat($expectedDump, $f);
215 public function testExcludeVerbosity()
217 $e = $this->getTestException('foo');
219 $expectedDump = <<<'EODUMP'
223 #file: "%sExceptionCasterTest.php"
228 $this->assertDumpMatchesFormat($expectedDump, $e, Caster::EXCLUDE_VERBOSE);