c9cc24f2dff620258b4c1f51c0a8b0ef760faa7f
[yaffs-website] / vendor / symfony / var-dumper / Tests / Caster / ExceptionCasterTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\VarDumper\Tests\Caster;
13
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;
21
22 class ExceptionCasterTest extends TestCase
23 {
24     use VarDumperTestTrait;
25
26     private function getTestException($msg, &$ref = null)
27     {
28         return new \Exception(''.$msg);
29     }
30
31     protected function tearDown()
32     {
33         ExceptionCaster::$srcContext = 1;
34         ExceptionCaster::$traceArgs = true;
35     }
36
37     public function testDefaultSettings()
38     {
39         $ref = array('foo');
40         $e = $this->getTestException('foo', $ref);
41
42         $expectedDump = <<<'EODUMP'
43 Exception {
44   #message: "foo"
45   #code: 0
46   #file: "%sExceptionCasterTest.php"
47   #line: 28
48   trace: {
49     %s%eTests%eCaster%eExceptionCasterTest.php:28 {
50       › {
51       ›     return new \Exception(''.$msg);
52       › }
53     }
54     %s%eTests%eCaster%eExceptionCasterTest.php:40 { …}
55     Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->testDefaultSettings() {}
56 %A
57 EODUMP;
58
59         $this->assertDumpMatchesFormat($expectedDump, $e);
60         $this->assertSame(array('foo'), $ref);
61     }
62
63     public function testSeek()
64     {
65         $e = $this->getTestException(2);
66
67         $expectedDump = <<<'EODUMP'
68 {
69   %s%eTests%eCaster%eExceptionCasterTest.php:28 {
70     › {
71     ›     return new \Exception(''.$msg);
72     › }
73   }
74   %s%eTests%eCaster%eExceptionCasterTest.php:65 { …}
75   Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->testSeek() {}
76 %A
77 EODUMP;
78
79         $this->assertStringMatchesFormat($expectedDump, $this->getDump($e, 'trace'));
80     }
81
82     public function testNoArgs()
83     {
84         $e = $this->getTestException(1);
85         ExceptionCaster::$traceArgs = false;
86
87         $expectedDump = <<<'EODUMP'
88 Exception {
89   #message: "1"
90   #code: 0
91   #file: "%sExceptionCasterTest.php"
92   #line: 28
93   trace: {
94     %sExceptionCasterTest.php:28 {
95       › {
96       ›     return new \Exception(''.$msg);
97       › }
98     }
99     %s%eTests%eCaster%eExceptionCasterTest.php:84 { …}
100     Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->testNoArgs() {}
101 %A
102 EODUMP;
103
104         $this->assertDumpMatchesFormat($expectedDump, $e);
105     }
106
107     public function testNoSrcContext()
108     {
109         $e = $this->getTestException(1);
110         ExceptionCaster::$srcContext = -1;
111
112         $expectedDump = <<<'EODUMP'
113 Exception {
114   #message: "1"
115   #code: 0
116   #file: "%sExceptionCasterTest.php"
117   #line: 28
118   trace: {
119     %s%eTests%eCaster%eExceptionCasterTest.php:28
120     %s%eTests%eCaster%eExceptionCasterTest.php:%d
121 %A
122 EODUMP;
123
124         $this->assertDumpMatchesFormat($expectedDump, $e);
125     }
126
127     public function testHtmlDump()
128     {
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.');
131         }
132
133         $e = $this->getTestException(1);
134         ExceptionCaster::$srcContext = -1;
135
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);
142
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>
153      &hellip;%d
154   </samp>}
155 </samp>}
156 </bar>
157 EODUMP;
158
159         $this->assertStringMatchesFormat($expectedDump, $dump);
160     }
161
162     /**
163      * @requires function Twig\Template::getSourceContext
164      */
165     public function testFrameWithTwig()
166     {
167         require_once dirname(__DIR__).'/Fixtures/Twig.php';
168
169         $f = array(
170             new FrameStub(array(
171                 'file' => dirname(__DIR__).'/Fixtures/Twig.php',
172                 'line' => 20,
173                 'class' => '__TwigTemplate_VarDumperFixture_u75a09',
174             )),
175             new FrameStub(array(
176                 'file' => dirname(__DIR__).'/Fixtures/Twig.php',
177                 'line' => 21,
178                 'class' => '__TwigTemplate_VarDumperFixture_u75a09',
179                 'object' => new \__TwigTemplate_VarDumperFixture_u75a09(null, __FILE__),
180             )),
181         );
182
183         $expectedDump = <<<'EODUMP'
184 array:2 [
185   0 => {
186     class: "__TwigTemplate_VarDumperFixture_u75a09"
187     src: {
188       %sTwig.php:1 {
189         › 
190         › foo bar
191         ›   twig source
192       }
193     }
194   }
195   1 => {
196     class: "__TwigTemplate_VarDumperFixture_u75a09"
197     object: __TwigTemplate_VarDumperFixture_u75a09 {
198     %A
199     }
200     src: {
201       %sExceptionCasterTest.php:2 {
202         › foo bar
203         ›   twig source
204         › 
205       }
206     }
207   }
208 ]
209
210 EODUMP;
211
212         $this->assertDumpMatchesFormat($expectedDump, $f);
213     }
214
215     public function testExcludeVerbosity()
216     {
217         $e = $this->getTestException('foo');
218
219         $expectedDump = <<<'EODUMP'
220 Exception {
221   #message: "foo"
222   #code: 0
223   #file: "%sExceptionCasterTest.php"
224   #line: 28
225 }
226 EODUMP;
227
228         $this->assertDumpMatchesFormat($expectedDump, $e, Caster::EXCLUDE_VERBOSE);
229     }
230 }