HtmlDescriptorTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\Command\Descriptor;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Output\BufferedOutput;
  13. use Symfony\Component\VarDumper\Cloner\Data;
  14. use Symfony\Component\VarDumper\Command\Descriptor\HtmlDescriptor;
  15. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  16. class HtmlDescriptorTest extends TestCase
  17. {
  18. private static $timezone;
  19. public static function setUpBeforeClass()
  20. {
  21. self::$timezone = date_default_timezone_get();
  22. date_default_timezone_set('UTC');
  23. }
  24. public static function tearDownAfterClass()
  25. {
  26. date_default_timezone_set(self::$timezone);
  27. }
  28. public function testItOutputsStylesAndScriptsOnFirstDescribeCall()
  29. {
  30. $output = new BufferedOutput();
  31. $dumper = $this->createMock(HtmlDumper::class);
  32. $dumper->method('dump')->willReturn('[DUMPED]');
  33. $descriptor = new HtmlDescriptor($dumper);
  34. $descriptor->describe($output, new Data([[123]]), ['timestamp' => 1544804268.3668], 1);
  35. $this->assertStringMatchesFormat('<style>%A</style><script>%A</script>%A', $output->fetch(), 'styles & scripts are output');
  36. $descriptor->describe($output, new Data([[123]]), ['timestamp' => 1544804268.3668], 1);
  37. $this->assertStringNotMatchesFormat('<style>%A</style><script>%A</script>%A', $output->fetch(), 'styles & scripts are output only once');
  38. }
  39. /**
  40. * @dataProvider provideContext
  41. */
  42. public function testDescribe(array $context, string $expectedOutput)
  43. {
  44. $output = new BufferedOutput();
  45. $dumper = $this->createMock(HtmlDumper::class);
  46. $dumper->method('dump')->willReturn('[DUMPED]');
  47. $descriptor = new HtmlDescriptor($dumper);
  48. $descriptor->describe($output, new Data([[123]]), $context + ['timestamp' => 1544804268.3668], 1);
  49. $this->assertStringMatchesFormat(trim($expectedOutput), trim(preg_replace('@<style>.*</style><script>.*</script>@s', '', $output->fetch())));
  50. }
  51. public function provideContext()
  52. {
  53. yield 'source' => [
  54. [
  55. 'source' => [
  56. 'name' => 'CliDescriptorTest.php',
  57. 'line' => 30,
  58. 'file' => '/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
  59. ],
  60. ],
  61. <<<TXT
  62. <article data-dedup-id="%s">
  63. <header>
  64. <div class="row">
  65. <h2 class="col">-</h2>
  66. <time class="col text-small" title="2018-12-14T16:17:48+00:00" datetime="2018-12-14T16:17:48+00:00">
  67. Fri, 14 Dec 2018 16:17:48 +0000
  68. </time>
  69. </div>
  70. </header>
  71. <section class="body">
  72. <p class="text-small">
  73. CliDescriptorTest.php on line 30
  74. </p>
  75. [DUMPED]
  76. </section>
  77. </article>
  78. TXT
  79. ];
  80. yield 'source full' => [
  81. [
  82. 'source' => [
  83. 'name' => 'CliDescriptorTest.php',
  84. 'project_dir' => 'src/Symfony/',
  85. 'line' => 30,
  86. 'file_relative' => 'src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
  87. 'file' => '/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
  88. 'file_link' => 'phpstorm://open?file=/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php&line=30',
  89. ],
  90. ],
  91. <<<TXT
  92. <article data-dedup-id="%s">
  93. <header>
  94. <div class="row">
  95. <h2 class="col">-</h2>
  96. <time class="col text-small" title="2018-12-14T16:17:48+00:00" datetime="2018-12-14T16:17:48+00:00">
  97. Fri, 14 Dec 2018 16:17:48 +0000
  98. </time>
  99. </div>
  100. <div class="row">
  101. <ul class="tags">
  102. <li><span class="badge">project dir</span>src/Symfony/</li>
  103. </ul>
  104. </div>
  105. </header>
  106. <section class="body">
  107. <p class="text-small">
  108. <a href="phpstorm://open?file=/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php&line=30">CliDescriptorTest.php on line 30</a>
  109. </p>
  110. [DUMPED]
  111. </section>
  112. </article>
  113. TXT
  114. ];
  115. yield 'cli' => [
  116. [
  117. 'cli' => [
  118. 'identifier' => 'd8bece1c',
  119. 'command_line' => 'bin/phpunit',
  120. ],
  121. ],
  122. <<<TXT
  123. <article data-dedup-id="d8bece1c">
  124. <header>
  125. <div class="row">
  126. <h2 class="col"><code>$ </code>bin/phpunit</h2>
  127. <time class="col text-small" title="2018-12-14T16:17:48+00:00" datetime="2018-12-14T16:17:48+00:00">
  128. Fri, 14 Dec 2018 16:17:48 +0000
  129. </time>
  130. </div>
  131. </header>
  132. <section class="body">
  133. <p class="text-small">
  134. </p>
  135. [DUMPED]
  136. </section>
  137. </article>
  138. TXT
  139. ];
  140. yield 'request' => [
  141. [
  142. 'request' => [
  143. 'identifier' => 'd8bece1c',
  144. 'controller' => new Data([['FooController.php']]),
  145. 'method' => 'GET',
  146. 'uri' => 'http://localhost/foo',
  147. ],
  148. ],
  149. <<<TXT
  150. <article data-dedup-id="d8bece1c">
  151. <header>
  152. <div class="row">
  153. <h2 class="col"><code>GET</code> <a href="http://localhost/foo">http://localhost/foo</a></h2>
  154. <time class="col text-small" title="2018-12-14T16:17:48+00:00" datetime="2018-12-14T16:17:48+00:00">
  155. Fri, 14 Dec 2018 16:17:48 +0000
  156. </time>
  157. </div>
  158. <div class="row">
  159. <ul class="tags">
  160. <li><span class="badge">controller</span><span class='dumped-tag'>[DUMPED]</span></li>
  161. </ul>
  162. </div>
  163. </header>
  164. <section class="body">
  165. <p class="text-small">
  166. </p>
  167. [DUMPED]
  168. </section>
  169. </article>
  170. TXT
  171. ];
  172. }
  173. }