forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewBladeCompilerTest.php
More file actions
executable file
·369 lines (291 loc) · 13.6 KB
/
ViewBladeCompilerTest.php
File metadata and controls
executable file
·369 lines (291 loc) · 13.6 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
use Mockery as m;
use Illuminate\View\Compilers\BladeCompiler;
class ViewBladeCompilerTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
m::close();
}
public function testIsExpiredReturnsTrueIfCompiledFileDoesntExist()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.md5('foo'))->andReturn(false);
$this->assertTrue($compiler->isExpired('foo'));
}
public function testIsExpiredReturnsTrueIfCachePathIsNull()
{
$compiler = new BladeCompiler($files = $this->getFiles(), null);
$files->shouldReceive('exists')->never();
$this->assertTrue($compiler->isExpired('foo'));
}
public function testIsExpiredReturnsTrueWhenModificationTimesWarrant()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.md5('foo'))->andReturn(true);
$files->shouldReceive('lastModified')->once()->with('foo')->andReturn(100);
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.md5('foo'))->andReturn(0);
$this->assertTrue($compiler->isExpired('foo'));
}
public function testCompilePathIsProperlyCreated()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals(__DIR__.'/'.md5('foo'), $compiler->getCompiledPath('foo'));
}
public function testCompileCompilesFileAndReturnsContents()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.md5('foo'), 'Hello World');
$compiler->compile('foo');
}
public function testCompileDoesntStoreFilesWhenCachePathIsNull()
{
$compiler = new BladeCompiler($files = $this->getFiles(), null);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('put')->never();
$compiler->compile('foo');
}
public function testEchosAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{{$name}}}'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{$name}}'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{ $name }}'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{
$name
}}'));
$this->assertEquals('<?php echo isset($name) ? $name : "foo"; ?>', $compiler->compileString('{{ $name or "foo" }}'));
$this->assertEquals('<?php echo isset($user->name) ? $user->name : "foo"; ?>', $compiler->compileString('{{ $user->name or "foo" }}'));
$this->assertEquals('<?php echo isset($name) ? $name : "foo"; ?>', $compiler->compileString('{{$name or "foo"}}'));
$this->assertEquals('<?php echo isset($name) ? $name : "foo"; ?>', $compiler->compileString('{{
$name or "foo"
}}'));
$this->assertEquals('<?php echo isset($name) ? $name : \'foo\'; ?>', $compiler->compileString('{{ $name or \'foo\' }}'));
$this->assertEquals('<?php echo isset($name) ? $name : \'foo\'; ?>', $compiler->compileString('{{$name or \'foo\'}}'));
$this->assertEquals('<?php echo isset($name) ? $name : \'foo\'; ?>', $compiler->compileString('{{
$name or \'foo\'
}}'));
$this->assertEquals('<?php echo isset($age) ? $age : 90; ?>', $compiler->compileString('{{ $age or 90 }}'));
$this->assertEquals('<?php echo isset($age) ? $age : 90; ?>', $compiler->compileString('{{$age or 90}}'));
$this->assertEquals('<?php echo isset($age) ? $age : 90; ?>', $compiler->compileString('{{
$age or 90
}}'));
$this->assertEquals('<?php echo "Hello world or foo"; ?>', $compiler->compileString('{{ "Hello world or foo" }}'));
$this->assertEquals('<?php echo "Hello world or foo"; ?>', $compiler->compileString('{{"Hello world or foo"}}'));
$this->assertEquals('<?php echo $foo + $or + $baz; ?>', $compiler->compileString('{{$foo + $or + $baz}}'));
$this->assertEquals('<?php echo "Hello world or foo"; ?>', $compiler->compileString('{{
"Hello world or foo"
}}'));
$this->assertEquals('<?php echo \'Hello world or foo\'; ?>', $compiler->compileString('{{ \'Hello world or foo\' }}'));
$this->assertEquals('<?php echo \'Hello world or foo\'; ?>', $compiler->compileString('{{\'Hello world or foo\'}}'));
$this->assertEquals('<?php echo \'Hello world or foo\'; ?>', $compiler->compileString('{{
\'Hello world or foo\'
}}'));
$this->assertEquals('<?php echo myfunc(\'foo or bar\'); ?>', $compiler->compileString('{{ myfunc(\'foo or bar\') }}'));
$this->assertEquals('<?php echo myfunc("foo or bar"); ?>', $compiler->compileString('{{ myfunc("foo or bar") }}'));
$this->assertEquals('<?php echo myfunc("$name or \'foo\'"); ?>', $compiler->compileString('{{ myfunc("$name or \'foo\'") }}'));
}
public function testEscapedWithAtEchosAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('{{$name}}', $compiler->compileString('@{{$name}}'));
$this->assertEquals('{{ $name }}', $compiler->compileString('@{{ $name }}'));
$this->assertEquals('{{
$name
}}',
$compiler->compileString('@{{
$name
}}'));
}
public function testReversedEchosAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$compiler->setEscapedContentTags('{{', '}}');
$compiler->setContentTags('{{{', '}}}');
$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{$name}}'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{{$name}}}'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{{ $name }}}'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('{{{
$name
}}}'));
}
public function testExtendsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@extends(\'foo\')
test';
$expected = "test".PHP_EOL.'<?php echo $__env->make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@extends(name(foo))'.PHP_EOL.'test';
$expected = "test".PHP_EOL.'<?php echo $__env->make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testCommentsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '{{--this is a comment--}}';
$expected = '<?php /*this is a comment*/ ?>';
$this->assertEquals($expected, $compiler->compileString($string));
$string = '{{--
this is a comment
--}}';
$expected = '<?php /*
this is a comment
*/ ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testIfStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@if (name(foo(bar)))
breeze
@endif';
$expected = '<?php if(name(foo(bar))): ?>
breeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testElseStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@if (name(foo(bar)))
breeze
@else
boom
@endif';
$expected = '<?php if(name(foo(bar))): ?>
breeze
<?php else: ?>
boom
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testElseIfStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@if(name(foo(bar)))
breeze
@elseif(boom(breeze))
boom
@endif';
$expected = '<?php if(name(foo(bar))): ?>
breeze
<?php elseif(boom(breeze)): ?>
boom
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testUnlessStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@unless (name(foo(bar)))
breeze
@endunless';
$expected = '<?php if ( ! (name(foo(bar)))): ?>
breeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testStatementThatContainsNonConsecutiveParanthesisAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = "Foo @lang(function_call('foo(blah)')) bar";
$expected = "Foo <?php echo \Illuminate\Support\Facades\Lang::get(function_call('foo(blah)')); ?> bar";
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testIncludesAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo $__env->make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@include(\'foo\')'));
$this->assertEquals('<?php echo $__env->make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@include(name(foo))'));
}
public function testShowEachAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo $__env->renderEach(\'foo\', \'bar\'); ?>', $compiler->compileString('@each(\'foo\', \'bar\')'));
$this->assertEquals('<?php echo $__env->renderEach(name(foo)); ?>', $compiler->compileString('@each(name(foo))'));
}
public function testYieldsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo $__env->yieldContent(\'foo\'); ?>', $compiler->compileString('@yield(\'foo\')'));
$this->assertEquals('<?php echo $__env->yieldContent(\'foo\', \'bar\'); ?>', $compiler->compileString('@yield(\'foo\', \'bar\')'));
$this->assertEquals('<?php echo $__env->yieldContent(name(foo)); ?>', $compiler->compileString('@yield(name(foo))'));
}
public function testShowsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo $__env->yieldSection(); ?>', $compiler->compileString('@show'));
}
public function testLanguageAndChoicesAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo \Illuminate\Support\Facades\Lang::get(\'foo\'); ?>', $compiler->compileString("@lang('foo')"));
$this->assertEquals('<?php echo \Illuminate\Support\Facades\Lang::choice(\'foo\', 1); ?>', $compiler->compileString("@choice('foo', 1)"));
}
public function testSectionStartsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php $__env->startSection(\'foo\'); ?>', $compiler->compileString('@section(\'foo\')'));
$this->assertEquals('<?php $__env->startSection(name(foo)); ?>', $compiler->compileString('@section(name(foo))'));
}
public function testStopSectionsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php $__env->stopSection(); ?>', $compiler->compileString('@stop'));
}
public function testEndSectionsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php $__env->stopSection(); ?>', $compiler->compileString('@endsection'));
}
public function testAppendSectionsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php $__env->appendSection(); ?>', $compiler->compileString('@append'));
}
public function testCustomPhpCodeIsCorrectlyHandled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php if($test): ?> <?php @show(\'test\'); ?> <?php endif; ?>', $compiler->compileString("@if(\$test) <?php @show('test'); ?> @endif"));
}
public function testMixingYieldAndEcho()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo $__env->yieldContent(\'title\'); ?> - <?php echo Config::get(\'site.title\'); ?>', $compiler->compileString("@yield('title') - {{Config::get('site.title')}}"));
}
public function testCustomExtensionsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$compiler->extend(function($value) { return str_replace('foo', 'bar', $value); });
$this->assertEquals('bar', $compiler->compileString('foo'));
}
public function testConfiguringContentTags()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$compiler->setContentTags('[[', ']]');
$compiler->setEscapedContentTags('[[[', ']]]');
$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('[[[ $name ]]]'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('[[ $name ]]'));
$this->assertEquals('<?php echo $name; ?>', $compiler->compileString('[[
$name
]]'));
}
public function testExpressionsOnTheSameLine()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php echo \Illuminate\Support\Facades\Lang::get(foo(bar(baz(qux(breeze()))))); ?> space () <?php echo \Illuminate\Support\Facades\Lang::get(foo(bar)); ?>', $compiler->compileString('@lang(foo(bar(baz(qux(breeze()))))) space () @lang(foo(bar))'));
}
public function testExpressionWithinHTML()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<html <?php echo $foo; ?>>', $compiler->compileString('<html {{ $foo }}>'));
$this->assertEquals('<html<?php echo $foo; ?>>', $compiler->compileString('<html{{ $foo }}>'));
$this->assertEquals('<html <?php echo $foo; ?> <?php echo \Illuminate\Support\Facades\Lang::get(\'foo\'); ?>>', $compiler->compileString('<html {{ $foo }} @lang(\'foo\')>'));
}
protected function getFiles()
{
return m::mock('Illuminate\Filesystem\Filesystem');
}
}