StubCasterTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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" target="_blank" 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 testClassStub()
  91. {
  92. $var = array(new ClassStub('hello', array(FooInterface::class, 'foo')));
  93. $cloner = new VarCloner();
  94. $dumper = new HtmlDumper();
  95. $dumper->setDumpHeader('<foo></foo>');
  96. $dumper->setDumpBoundaries('<bar>', '</bar>');
  97. $dump = $dumper->dump($cloner->cloneVar($var), true, array('fileLinkFormat' => '%f:%l'));
  98. $expectedDump = <<<'EODUMP'
  99. <foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
  100. <span class=sf-dump-index>0</span> => "<a href="%sFooInterface.php:10" target="_blank" rel="noopener noreferrer"><span class=sf-dump-str title="5 characters">hello</span></a>"
  101. </samp>]
  102. </bar>
  103. EODUMP;
  104. $this->assertStringMatchesFormat($expectedDump, $dump);
  105. }
  106. public function testClassStubWithNotExistingClass()
  107. {
  108. $var = array(new ClassStub(NotExisting::class));
  109. $cloner = new VarCloner();
  110. $dumper = new HtmlDumper();
  111. $dumper->setDumpHeader('<foo></foo>');
  112. $dumper->setDumpBoundaries('<bar>', '</bar>');
  113. $dump = $dumper->dump($cloner->cloneVar($var), true);
  114. $expectedDump = <<<'EODUMP'
  115. <foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
  116. <span class=sf-dump-index>0</span> => "<span class=sf-dump-str title="Symfony\Component\VarDumper\Tests\Caster\NotExisting
  117. 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>"
  118. </samp>]
  119. </bar>
  120. EODUMP;
  121. $this->assertStringMatchesFormat($expectedDump, $dump);
  122. }
  123. public function testClassStubWithNotExistingMethod()
  124. {
  125. $var = array(new ClassStub('hello', array(FooInterface::class, 'missing')));
  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, array('fileLinkFormat' => '%f:%l'));
  131. $expectedDump = <<<'EODUMP'
  132. <foo></foo><bar><span class=sf-dump-note>array:1</span> [<samp>
  133. <span class=sf-dump-index>0</span> => "<a href="%sFooInterface.php:5" target="_blank" rel="noopener noreferrer"><span class=sf-dump-str title="5 characters">hello</span></a>"
  134. </samp>]
  135. </bar>
  136. EODUMP;
  137. $this->assertStringMatchesFormat($expectedDump, $dump);
  138. }
  139. }