1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
<?php
namespace Illuminate\Tests\View\Blade;
use Illuminate\Contracts\View\ViewCompilationException;
class BladeForelseStatementsTest extends AbstractBladeTestCase
{
public function testForelseStatementsAreCompiled()
{
$string = '@forelse ($this->getUsers() as $user)
breeze
@empty
empty
@endforelse';
$expected = '<?php $__empty_1 = true; $__currentLoopData = $this->getUsers(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
breeze
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
empty
<?php endif; ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testForelseStatementsAreCompiledWithUppercaseSyntax()
{
$string = '@forelse ($this->getUsers() AS $user)
breeze
@empty
empty
@endforelse';
$expected = '<?php $__empty_1 = true; $__currentLoopData = $this->getUsers(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
breeze
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
empty
<?php endif; ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testForelseStatementsAreCompiledWithMultipleLine()
{
$string = '@forelse ([
foo,
bar,
] as $label)
breeze
@empty
empty
@endforelse';
$expected = '<?php $__empty_1 = true; $__currentLoopData = [
foo,
bar,
]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
breeze
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
empty
<?php endif; ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testNestedForelseStatementsAreCompiled()
{
$string = '@forelse ($this->getUsers() as $user)
@forelse ($user->tags as $tag)
breeze
@empty
tag empty
@endforelse
@empty
empty
@endforelse';
$expected = '<?php $__empty_1 = true; $__currentLoopData = $this->getUsers(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
<?php $__empty_2 = true; $__currentLoopData = $user->tags; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tag): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_2 = false; ?>
breeze
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_2): ?>
tag empty
<?php endif; ?>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
empty
<?php endif; ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
/**
* @dataProvider invalidForelseStatementsDataProvider
*/
public function testForelseStatementsThrowHumanizedMessageWhenInvalidStatement($initialStatement)
{
$this->expectException(ViewCompilationException::class);
$this->expectExceptionMessage('Malformed @forelse statement.');
$string = "$initialStatement
breeze
@empty
tag empty
@endforelse";
$this->compiler->compileString($string);
}
public static function invalidForelseStatementsDataProvider()
{
return [
['@forelse'],
['@forelse()'],
['@forelse ()'],
['@forelse($test)'],
['@forelse($test as)'],
['@forelse(as)'],
['@forelse ( as )'],
];
}
}
|