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