ExceptionCasterTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Tests\Caster;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\VarDumper\Caster\Caster;
  13. use Symfony\Component\VarDumper\Caster\ExceptionCaster;
  14. use Symfony\Component\VarDumper\Caster\FrameStub;
  15. use Symfony\Component\VarDumper\Cloner\VarCloner;
  16. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  17. use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
  18. class ExceptionCasterTest extends TestCase
  19. {
  20. use VarDumperTestTrait;
  21. private function getTestException($msg, &$ref = null)
  22. {
  23. return new \Exception(''.$msg);
  24. }
  25. protected function tearDown()
  26. {
  27. ExceptionCaster::$srcContext = 1;
  28. ExceptionCaster::$traceArgs = true;
  29. }
  30. public function testDefaultSettings()
  31. {
  32. $ref = array('foo');
  33. $e = $this->getTestException('foo', $ref);
  34. $expectedDump = <<<'EODUMP'
  35. Exception {
  36. #message: "foo"
  37. #code: 0
  38. #file: "%sExceptionCasterTest.php"
  39. #line: 28
  40. trace: {
  41. %s%eTests%eCaster%eExceptionCasterTest.php:28 {
  42. › {
  43. › return new \Exception(''.$msg);
  44. › }
  45. }
  46. %s%eTests%eCaster%eExceptionCasterTest.php:40 { …}
  47. Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->testDefaultSettings() {}
  48. %A
  49. EODUMP;
  50. $this->assertDumpMatchesFormat($expectedDump, $e);
  51. $this->assertSame(array('foo'), $ref);
  52. }
  53. public function testSeek()
  54. {
  55. $e = $this->getTestException(2);
  56. $expectedDump = <<<'EODUMP'
  57. {
  58. %s%eTests%eCaster%eExceptionCasterTest.php:28 {
  59. › {
  60. › return new \Exception(''.$msg);
  61. › }
  62. }
  63. %s%eTests%eCaster%eExceptionCasterTest.php:65 { …}
  64. Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->testSeek() {}
  65. %A
  66. EODUMP;
  67. $this->assertStringMatchesFormat($expectedDump, $this->getDump($e, 'trace'));
  68. }
  69. public function testNoArgs()
  70. {
  71. $e = $this->getTestException(1);
  72. ExceptionCaster::$traceArgs = false;
  73. $expectedDump = <<<'EODUMP'
  74. Exception {
  75. #message: "1"
  76. #code: 0
  77. #file: "%sExceptionCasterTest.php"
  78. #line: 28
  79. trace: {
  80. %sExceptionCasterTest.php:28 {
  81. › {
  82. › return new \Exception(''.$msg);
  83. › }
  84. }
  85. %s%eTests%eCaster%eExceptionCasterTest.php:84 { …}
  86. Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->testNoArgs() {}
  87. %A
  88. EODUMP;
  89. $this->assertDumpMatchesFormat($expectedDump, $e);
  90. }
  91. public function testNoSrcContext()
  92. {
  93. $e = $this->getTestException(1);
  94. ExceptionCaster::$srcContext = -1;
  95. $expectedDump = <<<'EODUMP'
  96. Exception {
  97. #message: "1"
  98. #code: 0
  99. #file: "%sExceptionCasterTest.php"
  100. #line: 28
  101. trace: {
  102. %s%eTests%eCaster%eExceptionCasterTest.php:28
  103. %s%eTests%eCaster%eExceptionCasterTest.php:%d
  104. %A
  105. EODUMP;
  106. $this->assertDumpMatchesFormat($expectedDump, $e);
  107. }
  108. public function testHtmlDump()
  109. {
  110. if (ini_get('xdebug.file_link_format') || get_cfg_var('xdebug.file_link_format')) {
  111. $this->markTestSkipped('A custom file_link_format is defined.');
  112. }
  113. $e = $this->getTestException(1);
  114. ExceptionCaster::$srcContext = -1;
  115. $cloner = new VarCloner();
  116. $cloner->setMaxItems(1);
  117. $dumper = new HtmlDumper();
  118. $dumper->setDumpHeader('<foo></foo>');
  119. $dumper->setDumpBoundaries('<bar>', '</bar>');
  120. $dump = $dumper->dump($cloner->cloneVar($e)->withRefHandles(false), true);
  121. $expectedDump = <<<'EODUMP'
  122. <foo></foo><bar><span class=sf-dump-note>Exception</span> {<samp>
  123. #<span class=sf-dump-protected title="Protected property">message</span>: "<span class=sf-dump-str>1</span>"
  124. #<span class=sf-dump-protected title="Protected property">code</span>: <span class=sf-dump-num>0</span>
  125. #<span class=sf-dump-protected title="Protected property">file</span>: "<span class=sf-dump-str title="%sExceptionCasterTest.php
  126. %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>"
  127. #<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>28</span>
  128. <span class=sf-dump-meta>trace</span>: {<samp>
  129. <span class=sf-dump-meta title="%sExceptionCasterTest.php
  130. 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>
  131. &hellip;%d
  132. </samp>}
  133. </samp>}
  134. </bar>
  135. EODUMP;
  136. $this->assertStringMatchesFormat($expectedDump, $dump);
  137. }
  138. /**
  139. * @requires function Twig\Template::getSourceContext
  140. */
  141. public function testFrameWithTwig()
  142. {
  143. require_once \dirname(__DIR__).'/Fixtures/Twig.php';
  144. $f = array(
  145. new FrameStub(array(
  146. 'file' => \dirname(__DIR__).'/Fixtures/Twig.php',
  147. 'line' => 20,
  148. 'class' => '__TwigTemplate_VarDumperFixture_u75a09',
  149. )),
  150. new FrameStub(array(
  151. 'file' => \dirname(__DIR__).'/Fixtures/Twig.php',
  152. 'line' => 21,
  153. 'class' => '__TwigTemplate_VarDumperFixture_u75a09',
  154. 'object' => new \__TwigTemplate_VarDumperFixture_u75a09(null, __FILE__),
  155. )),
  156. );
  157. $expectedDump = <<<'EODUMP'
  158. array:2 [
  159. 0 => {
  160. class: "__TwigTemplate_VarDumperFixture_u75a09"
  161. src: {
  162. %sTwig.php:1 {
  163. › foo bar
  164. › twig source
  165. }
  166. }
  167. }
  168. 1 => {
  169. class: "__TwigTemplate_VarDumperFixture_u75a09"
  170. object: __TwigTemplate_VarDumperFixture_u75a09 {
  171. %A
  172. }
  173. src: {
  174. %sExceptionCasterTest.php:2 {
  175. › foo bar
  176. › twig source
  177. }
  178. }
  179. }
  180. ]
  181. EODUMP;
  182. $this->assertDumpMatchesFormat($expectedDump, $f);
  183. }
  184. public function testExcludeVerbosity()
  185. {
  186. $e = $this->getTestException('foo');
  187. $expectedDump = <<<'EODUMP'
  188. Exception {
  189. #message: "foo"
  190. #code: 0
  191. #file: "%sExceptionCasterTest.php"
  192. #line: 28
  193. }
  194. EODUMP;
  195. $this->assertDumpMatchesFormat($expectedDump, $e, Caster::EXCLUDE_VERBOSE);
  196. }
  197. }