ExceptionCasterTest.php 5.8 KB

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