-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: The Symfony VarDumper dumps correctly when in HTML context
- Loading branch information
a.stecher
committed
Mar 15, 2024
1 parent
41051a6
commit 99e9068
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Laravel\Octane\Listeners; | ||
|
||
use Symfony\Component\VarDumper\Caster\ReflectionCaster; | ||
use Symfony\Component\VarDumper\Cloner\VarCloner; | ||
use Symfony\Component\VarDumper\Dumper\CliDumper; | ||
use Symfony\Component\VarDumper\Dumper\HtmlDumper; | ||
use Symfony\Component\VarDumper\VarDumper; | ||
|
||
class EnsureTheCorrectVarDumperIsUsed | ||
{ | ||
/** | ||
* Handle the event. | ||
* | ||
* @param mixed $event | ||
*/ | ||
public function handle($event): void | ||
{ | ||
VarDumper::setHandler(function ($var, ?string $label = null) { | ||
$cloner = new VarCloner(); | ||
$cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO); | ||
$var = $cloner->cloneVar($var); | ||
if (null !== $label) { | ||
$var = $var->withContext(['label' => $label]); | ||
} | ||
$dumper = app()->runningInConsole() ? new CliDumper() : new HtmlDumper(); | ||
$dumper->dump($var); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Laravel\Octane\Tests\Listeners; | ||
|
||
use Illuminate\Foundation\Application; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Blade; | ||
use Laravel\Octane\Tests\TestCase; | ||
|
||
class EnsureTheCorrectVarDumperIsUsedTest extends TestCase | ||
{ | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$_ENV['APP_RUNNING_IN_CONSOLE'] = false; | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
unset($_ENV['APP_RUNNING_IN_CONSOLE']); | ||
parent::tearDown(); | ||
} | ||
|
||
public function test_a_variable_is_contained_within_the_html_if_dumped_in_a_blade_view() | ||
{ | ||
$variableToDump = "someString"; | ||
[$app, $worker, $client] = $this->createOctaneContext([ | ||
Request::create('/view', 'GET') | ||
]); | ||
$app['router']->get('/view', function (Application $app) use ($variableToDump) { | ||
return Blade::render('@dump($variableToDump)', [ | ||
'variableToDump' => $variableToDump | ||
]); | ||
}); | ||
|
||
$worker->run(); | ||
|
||
$htmlFromResponse = $client->responses[0]->getContent(); | ||
$this->assertStringContainsString($variableToDump, $htmlFromResponse); | ||
} | ||
|
||
} |