123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace Illuminate\Tests\View\Blade;
- use Exception;
- use Illuminate\Support\Fluent;
- use Illuminate\Support\Str;
- class BladeEchoHandlerTest extends AbstractBladeTestCase
- {
- protected function setUp(): void
- {
- parent::setUp();
- $this->compiler->stringable(function (Fluent $object) {
- return 'Hello World';
- });
- }
- public function testBladeHandlerCanInterceptRegularEchos()
- {
- $this->assertSame(
- "<?php \$__bladeCompiler = app('blade.compiler'); ?><?php echo e(\$__bladeCompiler->applyEchoHandler(\$exampleObject)); ?>",
- $this->compiler->compileString('{{$exampleObject}}')
- );
- }
- public function testBladeHandlerCanInterceptRawEchos()
- {
- $this->assertSame(
- "<?php \$__bladeCompiler = app('blade.compiler'); ?><?php echo \$__bladeCompiler->applyEchoHandler(\$exampleObject); ?>",
- $this->compiler->compileString('{!!$exampleObject!!}')
- );
- }
- public function testBladeHandlerCanInterceptEscapedEchos()
- {
- $this->assertSame(
- "<?php \$__bladeCompiler = app('blade.compiler'); ?><?php echo e(\$__bladeCompiler->applyEchoHandler(\$exampleObject)); ?>",
- $this->compiler->compileString('{{{$exampleObject}}}')
- );
- }
- public function testWhitespaceIsPreservedCorrectly()
- {
- $this->assertSame(
- "<?php \$__bladeCompiler = app('blade.compiler'); ?><?php echo e(\$__bladeCompiler->applyEchoHandler(\$exampleObject)); ?>\n\n",
- $this->compiler->compileString("{{\$exampleObject}}\n")
- );
- }
- /**
- * @dataProvider handlerLogicDataProvider
- */
- public function testHandlerLogicWorksCorrectly($blade)
- {
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('The fluent object has been successfully handled!');
- $this->compiler->stringable(Fluent::class, function ($object) {
- throw new Exception('The fluent object has been successfully handled!');
- });
- app()->singleton('blade.compiler', function () {
- return $this->compiler;
- });
- $exampleObject = new Fluent();
- eval(Str::of($this->compiler->compileString($blade))->remove(['<?php', '?>']));
- }
- public function handlerLogicDataProvider()
- {
- return [
- ['{{$exampleObject}}'],
- ['{{$exampleObject;}}'],
- ['{{{$exampleObject;}}}'],
- ['{!!$exampleObject;!!}'],
- ];
- }
- /**
- * @dataProvider nonStringableDataProvider
- */
- public function testHandlerWorksWithNonStringables($blade, $expectedOutput)
- {
- app()->singleton('blade.compiler', function () {
- return $this->compiler;
- });
- ob_start();
- eval(Str::of($this->compiler->compileString($blade))->remove(['<?php', '?>']));
- $output = ob_get_contents();
- ob_end_clean();
- $this->assertSame($expectedOutput, $output);
- }
- public function nonStringableDataProvider()
- {
- return [
- ['{{"foo" . "bar"}}', 'foobar'],
- ['{{ 1 + 2 }}{{ "test"; }}', '3test'],
- ['@php($test = "hi"){{ $test }}', 'hi'],
- ['{!! " " !!}', ' '],
- ];
- }
- }
|