BladeForeachStatementsTest.php 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Illuminate\Tests\View\Blade;
  3. class BladeForeachStatementsTest extends AbstractBladeTestCase
  4. {
  5. public function testForeachStatementsAreCompiled()
  6. {
  7. $string = '@foreach ($this->getUsers() as $user)
  8. test
  9. @endforeach';
  10. $expected = '<?php $__currentLoopData = $this->getUsers(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
  11. test
  12. <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
  13. $this->assertEquals($expected, $this->compiler->compileString($string));
  14. }
  15. public function testForeachStatementsAreCompileWithUppercaseSyntax()
  16. {
  17. $string = '@foreach ($this->getUsers() AS $user)
  18. test
  19. @endforeach';
  20. $expected = '<?php $__currentLoopData = $this->getUsers(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
  21. test
  22. <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
  23. $this->assertEquals($expected, $this->compiler->compileString($string));
  24. }
  25. public function testForeachStatementsAreCompileWithMultipleLine()
  26. {
  27. $string = '@foreach ([
  28. foo,
  29. bar,
  30. ] as $label)
  31. test
  32. @endforeach';
  33. $expected = '<?php $__currentLoopData = [
  34. foo,
  35. bar,
  36. ]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
  37. test
  38. <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
  39. $this->assertEquals($expected, $this->compiler->compileString($string));
  40. }
  41. public function testNestedForeachStatementsAreCompiled()
  42. {
  43. $string = '@foreach ($this->getUsers() as $user)
  44. user info
  45. @foreach ($user->tags as $tag)
  46. tag info
  47. @endforeach
  48. @endforeach';
  49. $expected = '<?php $__currentLoopData = $this->getUsers(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
  50. user info
  51. <?php $__currentLoopData = $user->tags; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tag): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
  52. tag info
  53. <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
  54. <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
  55. $this->assertEquals($expected, $this->compiler->compileString($string));
  56. }
  57. public function testLoopContentHolderIsExtractedFromForeachStatements()
  58. {
  59. $string = '@foreach ($some_uSers1 as $user)';
  60. $expected = '<?php $__currentLoopData = $some_uSers1; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>';
  61. $this->assertEquals($expected, $this->compiler->compileString($string));
  62. $string = '@foreach ($users->get() as $user)';
  63. $expected = '<?php $__currentLoopData = $users->get(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>';
  64. $this->assertEquals($expected, $this->compiler->compileString($string));
  65. $string = '@foreach (range(1, 4) as $user)';
  66. $expected = '<?php $__currentLoopData = range(1, 4); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>';
  67. $this->assertEquals($expected, $this->compiler->compileString($string));
  68. $string = '@foreach ( $users as $user)';
  69. $expected = '<?php $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>';
  70. $this->assertEquals($expected, $this->compiler->compileString($string));
  71. $string = '@foreach ($tasks as $task)';
  72. $expected = '<?php $__currentLoopData = $tasks; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $task): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>';
  73. $this->assertEquals($expected, $this->compiler->compileString($string));
  74. $string = "@foreach(resolve('App\\\\DataProviders\\\\'.\$provider)->data() as \$key => \$value)
  75. <input {{ \$foo ? 'bar': 'baz' }}>
  76. @endforeach";
  77. $expected = "<?php \$__currentLoopData = resolve('App\\\\DataProviders\\\\'.\$provider)->data(); \$__env->addLoop(\$__currentLoopData); foreach(\$__currentLoopData as \$key => \$value): \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); ?>
  78. <input <?php echo e(\$foo ? 'bar': 'baz'); ?>>
  79. <?php endforeach; \$__env->popLoop(); \$loop = \$__env->getLastLoop(); ?>";
  80. $this->assertEquals($expected, $this->compiler->compileString($string));
  81. }
  82. }