BladePhpStatementsTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Illuminate\Tests\View\Blade;
  3. class BladePhpStatementsTest extends AbstractBladeTestCase
  4. {
  5. public function testPhpStatementsWithExpressionAreCompiled()
  6. {
  7. $string = '@php($set = true)';
  8. $expected = '<?php ($set = true); ?>';
  9. $this->assertEquals($expected, $this->compiler->compileString($string));
  10. }
  11. public function testPhpStatementsWithoutExpressionAreIgnored()
  12. {
  13. $string = '@php';
  14. $expected = '@php';
  15. $this->assertEquals($expected, $this->compiler->compileString($string));
  16. $string = '{{ "Ignore: @php" }}';
  17. $expected = '<?php echo e("Ignore: @php"); ?>';
  18. $this->assertEquals($expected, $this->compiler->compileString($string));
  19. }
  20. public function testPhpStatementsDontParseBladeCode()
  21. {
  22. $string = '@php echo "{{ This is a blade tag }}" @endphp';
  23. $expected = '<?php echo "{{ This is a blade tag }}" ?>';
  24. $this->assertEquals($expected, $this->compiler->compileString($string));
  25. }
  26. public function testVerbatimAndPhpStatementsDontGetMixedUp()
  27. {
  28. $string = "@verbatim {{ Hello, I'm not blade! }}"
  29. ."\n@php echo 'And I'm not PHP!' @endphp"
  30. ."\n@endverbatim {{ 'I am Blade' }}"
  31. ."\n@php echo 'I am PHP {{ not Blade }}' @endphp";
  32. $expected = " {{ Hello, I'm not blade! }}"
  33. ."\n@php echo 'And I'm not PHP!' @endphp"
  34. ."\n <?php echo e('I am Blade'); ?>"
  35. ."\n\n<?php echo 'I am PHP {{ not Blade }}' ?>";
  36. $this->assertEquals($expected, $this->compiler->compileString($string));
  37. }
  38. public function testStringWithParenthesisCannotBeCompiled()
  39. {
  40. $string = "@php(\$data = ['test' => ')'])";
  41. $expected = "<?php (\$data = ['test' => ')']); ?>";
  42. $actual = "<?php (\$data = ['test' => '); ?>'])";
  43. $this->assertEquals($actual, $this->compiler->compileString($string));
  44. }
  45. public function testStringWithEmptyStringDataValue()
  46. {
  47. $string = "@php(\$data = ['test' => ''])";
  48. $expected = "<?php (\$data = ['test' => '']); ?>";
  49. $this->assertEquals($expected, $this->compiler->compileString($string));
  50. $string = "@php(\$data = ['test' => \"\"])";
  51. $expected = "<?php (\$data = ['test' => \"\"]); ?>";
  52. $this->assertEquals($expected, $this->compiler->compileString($string));
  53. }
  54. public function testStringWithEscapingDataValue()
  55. {
  56. $string = "@php(\$data = ['test' => 'won\\'t break'])";
  57. $expected = "<?php (\$data = ['test' => 'won\\'t break']); ?>";
  58. $this->assertEquals($expected, $this->compiler->compileString($string));
  59. $string = "@php(\$data = ['test' => \"\\\"escaped\\\"\"])";
  60. $expected = "<?php (\$data = ['test' => \"\\\"escaped\\\"\"]); ?>";
  61. $this->assertEquals($expected, $this->compiler->compileString($string));
  62. }
  63. }