-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
BladePhpStatementsTest.php
181 lines (133 loc) · 8.52 KB
/
BladePhpStatementsTest.php
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace Illuminate\Tests\View\Blade;
class BladePhpStatementsTest extends AbstractBladeTestCase
{
public function testPhpStatementsWithExpressionAreCompiled()
{
$string = '@php($set = true)';
$expected = '<?php ($set = true); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testStringWithParenthesisWithEndPHP()
{
$string = "@php(\$data = ['related_to' => 'issue#45388'];) {{ \$data }} @endphp";
$expected = "<?php(\$data = ['related_to' => 'issue#45388'];) {{ \$data }} ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testPhpStatementsWithoutExpressionAreIgnored()
{
$string = '@php';
$expected = '@php';
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = '{{ "Ignore: @php" }}';
$expected = '<?php echo e("Ignore: @php"); ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testPhpStatementsDontParseBladeCode()
{
$string = '@php echo "{{ This is a blade tag }}" @endphp';
$expected = '<?php echo "{{ This is a blade tag }}" ?>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testVerbatimAndPhpStatementsDontGetMixedUp()
{
$string = "@verbatim {{ Hello, I'm not blade! }}"
."\n@php echo 'And I'm not PHP!' @endphp"
."\n@endverbatim {{ 'I am Blade' }}"
."\n@php echo 'I am PHP {{ not Blade }}' @endphp";
$expected = " {{ Hello, I'm not blade! }}"
."\n@php echo 'And I'm not PHP!' @endphp"
."\n <?php echo e('I am Blade'); ?>"
."\n\n<?php echo 'I am PHP {{ not Blade }}' ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testStringWithOpeningParenthesisCanBeCompiled()
{
$string = "@php(\$data = ['single' => ':(('])";
$expected = "<?php (\$data = ['single' => ':((']); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "@php(\$data = ['single' => (string)':(('])";
$expected = "<?php (\$data = ['single' => (string)':((']); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "@php(\$data = ['single' => '(()(('])";
$expected = "<?php (\$data = ['single' => '(()((']); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testStringWithParenthesisCanBeCompiled()
{
$string = "@php(\$data = ['single' => ')'])";
$expected = "<?php (\$data = ['single' => ')']); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "@php(\$data = ['(multiple)-))' => '((-))'])";
$expected = "<?php (\$data = ['(multiple)-))' => '((-))']); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "@php(\$data = [(int)'(multiple)-))' => (bool)'((casty))'])";
$expected = "<?php (\$data = [(int)'(multiple)-))' => (bool)'((casty))']); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$this->assertSame('<?php echo $__env->renderEach(\'foo\', \'b)a)r\'); ?>', $this->compiler->compileString('@each(\'foo\', \'b)a)r\')'));
$this->assertSame('<?php echo $__env->make(\'test_for\', [\'issue))\' => \'(issue#45424))\'], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@include(\'test_for\', [\'issue))\' => \'(issue#45424))\'])'));
$this->assertSame('( <?php echo $__env->make(\'test_for\', [\'not_too_much))\' => \'(issue#45424))\'], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>))', $this->compiler->compileString('( @include(\'test_for\', [\'not_too_much))\' => \'(issue#45424))\'])))'));
}
public function testStringWithEmptyStringDataValue()
{
$string = "@php(\$data = ['test' => ''])";
$expected = "<?php (\$data = ['test' => '']); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "@php(\$data = ['test' => \"\"])";
$expected = "<?php (\$data = ['test' => \"\"]); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testStringWithEscapingDataValue()
{
$string = "@php(\$data = ['test' => 'won\\'t break'])";
$expected = "<?php (\$data = ['test' => 'won\\'t break']); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "@php(\$data = ['test' => \"\\\"escaped\\\"\"])";
$expected = "<?php (\$data = ['test' => \"\\\"escaped\\\"\"]); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testUnclosedParenthesisForBladeTags()
{
$string = "<span @class(['(']></span>";
$expected = "<span class=\"<?php echo \Illuminate\Support\Arr::toCssClasses([]); ?>\"(['(']></span>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "<span @class(['']></span>";
$expected = "<span class=\"<?php echo \Illuminate\Support\Arr::toCssClasses([]); ?>\"(['']></span>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "<span @class([')']></span>";
$expected = "<span @class([')']></span>";
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "<span @class(['))']></span>";
$expected = "<span @class(['))']></span>";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testNestedTagCalls()
{
$string = "<span @class(['k' => @empty(\$v)])></span>";
$expected = '<span class="<?php echo \Illuminate\Support\Arr::toCssClasses([\'k\' => @empty($v)]); ?>"></span>';
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "<span @class(['k))' => @empty(\$v)])></span>";
$expected = '<span class="<?php echo \Illuminate\Support\Arr::toCssClasses([\'k))\' => @empty($v)]); ?>"></span>';
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "<span @class(['k' => @empty(\$v), 't' => @empty(\$v1)])></span>";
$expected = '<span class="<?php echo \Illuminate\Support\Arr::toCssClasses([\'k\' => @empty($v), \'t\' => @empty($v1)]); ?>"></span>';
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "<span @class(['k' => @empty(\$v), 't' => @empty(\$v1)])></span>";
$expected = '<span class="<?php echo \Illuminate\Support\Arr::toCssClasses([\'k\' => @empty($v), \'t\' => @empty($v1)]); ?>"></span>';
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "<span @class(['k' => @empty(\$v), 't' => @empty(\$v1), 'r' => @empty(\$v2)])></span>";
$expected = '<span class="<?php echo \Illuminate\Support\Arr::toCssClasses([\'k\' => @empty($v), \'t\' => @empty($v1), \'r\' => @empty($v2)]); ?>"></span>';
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "<span @class(['k' => @empty(\$v), 't))' => @empty(\$v1), 'r' => @empty(\$v2)])></span>";
$expected = '<span class="<?php echo \Illuminate\Support\Arr::toCssClasses([\'k\' => @empty($v), \'t))\' => @empty($v1), \'r\' => @empty($v2)]); ?>"></span>';
$this->assertEquals($expected, $this->compiler->compileString($string));
$string = "<span @class(['k' => @empty(\$v), 't' => @empty(\$v1), 'r' => @empty(\$v2), 'l' => 'l'])></span><span @class(['k' => @empty(\$v)])></span>";
$expected = '<span class="<?php echo \Illuminate\Support\Arr::toCssClasses([\'k\' => @empty($v), \'t\' => @empty($v1), \'r\' => @empty($v2), \'l\' => \'l\']); ?>"></span><span class="<?php echo \Illuminate\Support\Arr::toCssClasses([\'k\' => @empty($v)]); ?>"></span>';
$this->assertEquals($expected, $this->compiler->compileString($string));
}
public function testItDoesNotCompileInvalidSyntax()
{
$template = "<a @class(['k\' => ()])></a>";
$this->assertEquals($template, $this->compiler->compileString($template));
}
}