BladeEchoHandlerTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Illuminate\Tests\View\Blade;
  3. use Exception;
  4. use Illuminate\Support\Fluent;
  5. use Illuminate\Support\Str;
  6. class BladeEchoHandlerTest extends AbstractBladeTestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. parent::setUp();
  11. $this->compiler->stringable(function (Fluent $object) {
  12. return 'Hello World';
  13. });
  14. }
  15. public function testBladeHandlerCanInterceptRegularEchos()
  16. {
  17. $this->assertSame(
  18. "<?php \$__bladeCompiler = app('blade.compiler'); ?><?php echo e(\$__bladeCompiler->applyEchoHandler(\$exampleObject)); ?>",
  19. $this->compiler->compileString('{{$exampleObject}}')
  20. );
  21. }
  22. public function testBladeHandlerCanInterceptRawEchos()
  23. {
  24. $this->assertSame(
  25. "<?php \$__bladeCompiler = app('blade.compiler'); ?><?php echo \$__bladeCompiler->applyEchoHandler(\$exampleObject); ?>",
  26. $this->compiler->compileString('{!!$exampleObject!!}')
  27. );
  28. }
  29. public function testBladeHandlerCanInterceptEscapedEchos()
  30. {
  31. $this->assertSame(
  32. "<?php \$__bladeCompiler = app('blade.compiler'); ?><?php echo e(\$__bladeCompiler->applyEchoHandler(\$exampleObject)); ?>",
  33. $this->compiler->compileString('{{{$exampleObject}}}')
  34. );
  35. }
  36. public function testWhitespaceIsPreservedCorrectly()
  37. {
  38. $this->assertSame(
  39. "<?php \$__bladeCompiler = app('blade.compiler'); ?><?php echo e(\$__bladeCompiler->applyEchoHandler(\$exampleObject)); ?>\n\n",
  40. $this->compiler->compileString("{{\$exampleObject}}\n")
  41. );
  42. }
  43. /**
  44. * @dataProvider handlerLogicDataProvider
  45. */
  46. public function testHandlerLogicWorksCorrectly($blade)
  47. {
  48. $this->expectException(Exception::class);
  49. $this->expectExceptionMessage('The fluent object has been successfully handled!');
  50. $this->compiler->stringable(Fluent::class, function ($object) {
  51. throw new Exception('The fluent object has been successfully handled!');
  52. });
  53. app()->singleton('blade.compiler', function () {
  54. return $this->compiler;
  55. });
  56. $exampleObject = new Fluent();
  57. eval(Str::of($this->compiler->compileString($blade))->remove(['<?php', '?>']));
  58. }
  59. public function handlerLogicDataProvider()
  60. {
  61. return [
  62. ['{{$exampleObject}}'],
  63. ['{{$exampleObject;}}'],
  64. ['{{{$exampleObject;}}}'],
  65. ['{!!$exampleObject;!!}'],
  66. ];
  67. }
  68. /**
  69. * @dataProvider nonStringableDataProvider
  70. */
  71. public function testHandlerWorksWithNonStringables($blade, $expectedOutput)
  72. {
  73. app()->singleton('blade.compiler', function () {
  74. return $this->compiler;
  75. });
  76. ob_start();
  77. eval(Str::of($this->compiler->compileString($blade))->remove(['<?php', '?>']));
  78. $output = ob_get_contents();
  79. ob_end_clean();
  80. $this->assertSame($expectedOutput, $output);
  81. }
  82. public function nonStringableDataProvider()
  83. {
  84. return [
  85. ['{{"foo" . "bar"}}', 'foobar'],
  86. ['{{ 1 + 2 }}{{ "test"; }}', '3test'],
  87. ['@php($test = "hi"){{ $test }}', 'hi'],
  88. ['{!! "&nbsp;" !!}', '&nbsp;'],
  89. ];
  90. }
  91. }