StubCasterTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\ArgsStub;
  13. use Symfony\Component\VarDumper\Caster\ClassStub;
  14. use Symfony\Component\VarDumper\Caster\LinkStub;
  15. use Symfony\Component\VarDumper\Cloner\VarCloner;
  16. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  17. use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
  18. use Symfony\Component\VarDumper\Tests\Fixtures\FooInterface;
  19. class StubCasterTest extends TestCase
  20. {
  21. use VarDumperTestTrait;
  22. public function testArgsStubWithDefaults($foo = 234, $bar = 456)
  23. {
  24. $args = array(new ArgsStub(array(123), __FUNCTION__, __CLASS__));
  25. $expectedDump = <<<'EODUMP'
  26. array:1 [
  27. 0 => {
  28. $foo: 123
  29. }
  30. ]
  31. EODUMP;
  32. $this->assertDumpMatchesFormat($expectedDump, $args);
  33. }
  34. public function testArgsStubWithExtraArgs($foo = 234)
  35. {
  36. $args = array(new ArgsStub(array(123, 456), __FUNCTION__, __CLASS__));
  37. $expectedDump = <<<'EODUMP'
  38. array:1 [
  39. 0 => {
  40. $foo: 123
  41. ...: {
  42. 456
  43. }
  44. }
  45. ]
  46. EODUMP;
  47. $this->assertDumpMatchesFormat($expectedDump, $args);
  48. }
  49. public function testArgsStubNoParamWithExtraArgs()
  50. {
  51. $args = array(new ArgsStub(array(123), __FUNCTION__, __CLASS__));
  52. $expectedDump = <<<'EODUMP'
  53. array:1 [
  54. 0 => {
  55. 123
  56. }
  57. ]
  58. EODUMP;
  59. $this->assertDumpMatchesFormat($expectedDump, $args);
  60. }
  61. public function testArgsStubWithClosure()
  62. {
  63. $args = array(new ArgsStub(array(123), '{closure}', null));
  64. $expectedDump = <<<'EODUMP'
  65. array:1 [
  66. 0 => {
  67. 123
  68. }
  69. ]
  70. EODUMP;
  71. $this->assertDumpMatchesFormat($expectedDump, $args);
  72. }
  73. public function testLinkStub()
  74. {
  75. $var = array(new LinkStub(__CLASS__, 0, __FILE__));
  76. $cloner = new VarCloner();
  77. $dumper = new HtmlDumper();
  78. $dumper->setDumpHeader('<foo></foo>');
  79. $dumper->setDumpBoundaries('<bar>', '</bar>');
  80. $dumper->setDisplayOptions(array('fileLinkFormat' => '%f:%l'));
  81. $dump = $dumper->dump($cloner->cloneVar($var), true);
  82. $expectedDump = <<<'EODUMP'
  83. <foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
  84. <span class=sf-dump-index>0</span> => "<a href="%sStubCasterTest.php:0" rel="noopener noreferrer"><span class=sf-dump-str title="55 characters">Symfony\Component\VarDumper\Tests\Caster\StubCasterTest</span></a>"
  85. </samp>]
  86. </bar>
  87. EODUMP;
  88. $this->assertStringMatchesFormat($expectedDump, $dump);
  89. }
  90. public function testLinkStubWithNoFileLink()
  91. {
  92. $var = array(new LinkStub('example.com', 0, 'http://example.com'));
  93. $cloner = new VarCloner();
  94. $dumper = new HtmlDumper();
  95. $dumper->setDumpHeader('<foo></foo>');
  96. $dumper->setDumpBoundaries('<bar>', '</bar>');
  97. $dumper->setDisplayOptions(array('fileLinkFormat' => '%f:%l'));
  98. $dump = $dumper->dump($cloner->cloneVar($var), true);
  99. $expectedDump = <<<'EODUMP'
  100. <foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
  101. <span class=sf-dump-index>0</span> => "<a href="http://example.com" target="_blank" rel="noopener noreferrer"><span class=sf-dump-str title="11 characters">example.com</span></a>"
  102. </samp>]
  103. </bar>
  104. EODUMP;
  105. $this->assertStringMatchesFormat($expectedDump, $dump);
  106. }
  107. public function testClassStub()
  108. {
  109. $var = array(new ClassStub('hello', array(FooInterface::class, 'foo')));
  110. $cloner = new VarCloner();
  111. $dumper = new HtmlDumper();
  112. $dumper->setDumpHeader('<foo></foo>');
  113. $dumper->setDumpBoundaries('<bar>', '</bar>');
  114. $dump = $dumper->dump($cloner->cloneVar($var), true, array('fileLinkFormat' => '%f:%l'));
  115. $expectedDump = <<<'EODUMP'
  116. <foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
  117. <span class=sf-dump-index>0</span> => "<a href="%sFooInterface.php:10" rel="noopener noreferrer"><span class=sf-dump-str title="5 characters">hello</span></a>"
  118. </samp>]
  119. </bar>
  120. EODUMP;
  121. $this->assertStringMatchesFormat($expectedDump, $dump);
  122. }
  123. public function testClassStubWithNotExistingClass()
  124. {
  125. $var = array(new ClassStub(NotExisting::class));
  126. $cloner = new VarCloner();
  127. $dumper = new HtmlDumper();
  128. $dumper->setDumpHeader('<foo></foo>');
  129. $dumper->setDumpBoundaries('<bar>', '</bar>');
  130. $dump = $dumper->dump($cloner->cloneVar($var), true);
  131. $expectedDump = <<<'EODUMP'
  132. <foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
  133. <span class=sf-dump-index>0</span> => "<span class=sf-dump-str title="Symfony\Component\VarDumper\Tests\Caster\NotExisting
  134. 52 characters"><span class="sf-dump-ellipsis sf-dump-ellipsis-class">Symfony\Component\VarDumper\Tests\Caster</span><span class=sf-dump-ellipsis>\</span>NotExisting</span>"
  135. </samp>]
  136. </bar>
  137. EODUMP;
  138. $this->assertStringMatchesFormat($expectedDump, $dump);
  139. }
  140. public function testClassStubWithNotExistingMethod()
  141. {
  142. $var = array(new ClassStub('hello', array(FooInterface::class, 'missing')));
  143. $cloner = new VarCloner();
  144. $dumper = new HtmlDumper();
  145. $dumper->setDumpHeader('<foo></foo>');
  146. $dumper->setDumpBoundaries('<bar>', '</bar>');
  147. $dump = $dumper->dump($cloner->cloneVar($var), true, array('fileLinkFormat' => '%f:%l'));
  148. $expectedDump = <<<'EODUMP'
  149. <foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
  150. <span class=sf-dump-index>0</span> => "<a href="%sFooInterface.php:5" rel="noopener noreferrer"><span class=sf-dump-str title="5 characters">hello</span></a>"
  151. </samp>]
  152. </bar>
  153. EODUMP;
  154. $this->assertStringMatchesFormat($expectedDump, $dump);
  155. }
  156. }