Skip to content

Commit 8570b8a

Browse files
committed
[VarDumper] Add descriptors tests
1 parent 1dbb374 commit 8570b8a

File tree

4 files changed

+331
-1
lines changed

4 files changed

+331
-1
lines changed

src/Symfony/Component/VarDumper/Command/Descriptor/HtmlDescriptor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function describe(OutputInterface $output, Data $data, array $context, in
5757
$sourceDescription = '';
5858
if (isset($context['source'])) {
5959
$source = $context['source'];
60-
$projectDir = $source['project_dir'];
60+
$projectDir = $source['project_dir'] ?? null;
6161
$sourceDescription = sprintf('%s on line %d', $source['name'], $source['line']);
6262
if (isset($source['file_link'])) {
6363
$sourceDescription = sprintf('<a href="%s">%s</a>', $source['file_link'], $sourceDescription);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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\CliDescriptor;
18+
use Symfony\Component\VarDumper\Dumper\CliDumper;
19+
20+
class CliDescriptorTest 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+
/**
36+
* @dataProvider provideContext
37+
*/
38+
public function testDescribe(array $context, string $expectedOutput)
39+
{
40+
$output = new BufferedOutput();
41+
$descriptor = new CliDescriptor(new CliDumper(function ($s) {
42+
return $s;
43+
}));
44+
45+
$descriptor->describe($output, new Data(array(array(123))), $context + array('timestamp' => 1544804268.3668), 1);
46+
47+
$this->assertStringMatchesFormat(trim($expectedOutput), str_replace(PHP_EOL, "\n", trim($output->fetch())));
48+
}
49+
50+
public function provideContext()
51+
{
52+
yield 'source' => array(
53+
array(
54+
'source' => array(
55+
'name' => 'CliDescriptorTest.php',
56+
'line' => 30,
57+
'file' => '/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
58+
),
59+
),
60+
<<<TXT
61+
Received from client #1
62+
-----------------------
63+
64+
-------- ---------------------------------------------------------------------------------------------------
65+
date Fri, 14 Dec 2018 16:17:48 +0000
66+
source CliDescriptorTest.php on line 30
67+
file /Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php
68+
-------- ---------------------------------------------------------------------------------------------------
69+
TXT
70+
);
71+
72+
yield 'source full' => array(
73+
array(
74+
'source' => array(
75+
'name' => 'CliDescriptorTest.php',
76+
'line' => 30,
77+
'file_relative' => 'src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
78+
'file' => '/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
79+
'file_link' => 'phpstorm://open?file=/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php&line=30',
80+
),
81+
),
82+
<<<TXT
83+
Received from client #1
84+
-----------------------
85+
86+
-------- --------------------------------------------------------------------------------
87+
date Fri, 14 Dec 2018 16:17:48 +0000
88+
source CliDescriptorTest.php on line 30
89+
file src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php
90+
-------- --------------------------------------------------------------------------------
91+
92+
Open source in your IDE/browser:
93+
phpstorm://open?file=/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php&line=30
94+
TXT
95+
);
96+
97+
yield 'cli' => array(
98+
array(
99+
'cli' => array(
100+
'identifier' => 'd8bece1c',
101+
'command_line' => 'bin/phpunit',
102+
),
103+
),
104+
<<<TXT
105+
$ bin/phpunit
106+
-------------
107+
108+
------ ---------------------------------
109+
date Fri, 14 Dec 2018 16:17:48 +0000
110+
------ ---------------------------------
111+
TXT
112+
);
113+
114+
yield 'request' => array(
115+
array(
116+
'request' => array(
117+
'identifier' => 'd8bece1c',
118+
'controller' => new Data(array(array('FooController.php'))),
119+
'method' => 'GET',
120+
'uri' => 'http://localhost/foo',
121+
),
122+
),
123+
<<<TXT
124+
GET http://localhost/foo
125+
------------------------
126+
127+
------------ ---------------------------------
128+
date Fri, 14 Dec 2018 16:17:48 +0000
129+
controller "FooController.php"
130+
------------ ---------------------------------
131+
TXT
132+
);
133+
}
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
}

src/Symfony/Component/VarDumper/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"require-dev": {
2424
"ext-iconv": "*",
25+
"symfony/console": "~3.4|~4.0",
2526
"symfony/process": "~3.4|~4.0",
2627
"twig/twig": "~1.34|~2.4"
2728
},

0 commit comments

Comments
 (0)