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
786 lines (699 loc) · 29.9 KB
/
ViewBladeCompilerTest.php
File metadata and controls
786 lines (699 loc) · 29.9 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
<?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__.'/'.sha1('foo').'.php')->andReturn(false);
$this->assertTrue($compiler->isExpired('foo'));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testCannotConstructWithBadCachePath()
{
new BladeCompiler($this->getFiles(), null);
}
public function testIsExpiredReturnsTrueWhenModificationTimesWarrant()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('exists')->once()->with(__DIR__.'/'.sha1('foo').'.php')->andReturn(true);
$files->shouldReceive('lastModified')->once()->with('foo')->andReturn(100);
$files->shouldReceive('lastModified')->once()->with(__DIR__.'/'.sha1('foo').'.php')->andReturn(0);
$this->assertTrue($compiler->isExpired('foo'));
}
public function testCompilePathIsProperlyCreated()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals(__DIR__.'/'.sha1('foo').'.php', $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__.'/'.sha1('foo').'.php', 'Hello World');
$compiler->compile('foo');
}
public function testCompileCompilesAndGetThePath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World');
$compiler->compile('foo');
$this->assertEquals('foo', $compiler->getPath());
}
public function testCompileSetAndGetThePath()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$compiler->setPath('foo');
$this->assertEquals('foo', $compiler->getPath());
}
public function testCompileWithPathSetBefore()
{
$compiler = new BladeCompiler($files = $this->getFiles(), __DIR__);
$files->shouldReceive('get')->once()->with('foo')->andReturn('Hello World');
$files->shouldReceive('put')->once()->with(__DIR__.'/'.sha1('foo').'.php', 'Hello World');
// set path before compilation
$compiler->setPath('foo');
// trigger compilation with null $path
$compiler->compile();
$this->assertEquals('foo', $compiler->getPath());
}
public function testEchosAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$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 e($name); ?>', $compiler->compileString('{{{$name}}}'));
$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{$name}}'));
$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{ $name }}'));
$this->assertEquals('<?php echo e($name); ?>', $compiler->compileString('{{
$name
}}'));
$this->assertEquals("<?php echo e(\$name); ?>\n\n", $compiler->compileString("{{ \$name }}\n"));
$this->assertEquals("<?php echo e(\$name); ?>\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n"));
$this->assertEquals("<?php echo e(\$name); ?>\n\n", $compiler->compileString("{{ \$name }}\n"));
$this->assertEquals("<?php echo e(\$name); ?>\r\n\r\n", $compiler->compileString("{{ \$name }}\r\n"));
$this->assertEquals('<?php echo e(isset($name) ? $name : "foo"); ?>', $compiler->compileString('{{ $name or "foo" }}'));
$this->assertEquals('<?php echo e(isset($user->name) ? $user->name : "foo"); ?>', $compiler->compileString('{{ $user->name or "foo" }}'));
$this->assertEquals('<?php echo e(isset($name) ? $name : "foo"); ?>', $compiler->compileString('{{$name or "foo"}}'));
$this->assertEquals('<?php echo e(isset($name) ? $name : "foo"); ?>', $compiler->compileString('{{
$name or "foo"
}}'));
$this->assertEquals('<?php echo e(isset($name) ? $name : \'foo\'); ?>', $compiler->compileString('{{ $name or \'foo\' }}'));
$this->assertEquals('<?php echo e(isset($name) ? $name : \'foo\'); ?>', $compiler->compileString('{{$name or \'foo\'}}'));
$this->assertEquals('<?php echo e(isset($name) ? $name : \'foo\'); ?>', $compiler->compileString('{{
$name or \'foo\'
}}'));
$this->assertEquals('<?php echo e(isset($age) ? $age : 90); ?>', $compiler->compileString('{{ $age or 90 }}'));
$this->assertEquals('<?php echo e(isset($age) ? $age : 90); ?>', $compiler->compileString('{{$age or 90}}'));
$this->assertEquals('<?php echo e(isset($age) ? $age : 90); ?>', $compiler->compileString('{{
$age or 90
}}'));
$this->assertEquals('<?php echo e("Hello world or foo"); ?>', $compiler->compileString('{{ "Hello world or foo" }}'));
$this->assertEquals('<?php echo e("Hello world or foo"); ?>', $compiler->compileString('{{"Hello world or foo"}}'));
$this->assertEquals('<?php echo e($foo + $or + $baz); ?>', $compiler->compileString('{{$foo + $or + $baz}}'));
$this->assertEquals('<?php echo e("Hello world or foo"); ?>', $compiler->compileString('{{
"Hello world or foo"
}}'));
$this->assertEquals('<?php echo e(\'Hello world or foo\'); ?>', $compiler->compileString('{{ \'Hello world or foo\' }}'));
$this->assertEquals('<?php echo e(\'Hello world or foo\'); ?>', $compiler->compileString('{{\'Hello world or foo\'}}'));
$this->assertEquals('<?php echo e(\'Hello world or foo\'); ?>', $compiler->compileString('{{
\'Hello world or foo\'
}}'));
$this->assertEquals('<?php echo e(myfunc(\'foo or bar\')); ?>', $compiler->compileString('{{ myfunc(\'foo or bar\') }}'));
$this->assertEquals('<?php echo e(myfunc("foo or bar")); ?>', $compiler->compileString('{{ myfunc("foo or bar") }}'));
$this->assertEquals('<?php echo e(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
}}'));
$this->assertEquals('{{ $name }}
',
$compiler->compileString('@{{ $name }}
'));
}
public function testEscapedWithAtDirectivesAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('@foreach', $compiler->compileString('@@foreach'));
$this->assertEquals('@verbatim @continue @endverbatim', $compiler->compileString('@@verbatim @@continue @@endverbatim'));
$this->assertEquals('@foreach($i as $x)', $compiler->compileString('@@foreach($i as $x)'));
$this->assertEquals('@continue @break', $compiler->compileString('@@continue @@break'));
$this->assertEquals('@foreach(
$i as $x
)', $compiler->compileString('@@foreach(
$i as $x
)'));
}
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 testPushIsCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@push(\'foo\')
test
@endpush';
$expected = '<?php $__env->startPush(\'foo\'); ?>
test
<?php $__env->stopPush(); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testStackIsCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@stack(\'foo\')';
$expected = '<?php echo $__env->yieldPushContent(\'foo\'); ?>';
$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));
$string = sprintf('{{-- this is an %s long comment --}}', str_repeat('extremely ', 1000));
$expected = sprintf('<?php /* this is an %s long comment */ ?>', str_repeat('extremely ', 1000));
$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 testHasSectionStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@hasSection("section")
breeze
@endif';
$expected = '<?php if (! empty(trim($__env->yieldContent("section")))): ?>
breeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testCanStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@can (\'update\', [$post])
breeze
@elsecan(\'delete\', [$post])
sneeze
@endcan';
$expected = '<?php if (app(\'Illuminate\\Contracts\\Auth\\Access\\Gate\')->check(\'update\', [$post])): ?>
breeze
<?php elseif (app(\'Illuminate\\Contracts\\Auth\\Access\\Gate\')->check(\'delete\', [$post])): ?>
sneeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testCannotStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@cannot (\'update\', [$post])
breeze
@elsecannot(\'delete\', [$post])
sneeze
@endcannot';
$expected = '<?php if (app(\'Illuminate\\Contracts\\Auth\\Access\\Gate\')->denies(\'update\', [$post])): ?>
breeze
<?php elseif (app(\'Illuminate\\Contracts\\Auth\\Access\\Gate\')->denies(\'delete\', [$post])): ?>
sneeze
<?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 testWhileStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@while ($foo)
test
@endwhile';
$expected = '<?php while($foo): ?>
test
<?php endwhile; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testNestedWhileStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@while ($foo)
@while ($bar)
test
@endwhile
@endwhile';
$expected = '<?php while($foo): ?>
<?php while($bar): ?>
test
<?php endwhile; ?>
<?php endwhile; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testForStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testBreakStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@break
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php break; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testBreakStatementsWithExpressionAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@break(TRUE)
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php if(TRUE) break; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testContinueStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@continue
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php continue; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testContinueStatementsWithExpressionAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
test
@continue(TRUE)
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
test
<?php if(TRUE) continue; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testNestedForStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@for ($i = 0; $i < 10; $i++)
@for ($j = 0; $j < 20; $j++)
test
@endfor
@endfor';
$expected = '<?php for($i = 0; $i < 10; $i++): ?>
<?php for($j = 0; $j < 20; $j++): ?>
test
<?php endfor; ?>
<?php endfor; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testForeachStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@foreach ($this->getUsers() as $user)
test
@endforeach';
$expected = '<?php foreach($this->getUsers() as $user): ?>
test
<?php endforeach; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testNestedForeachStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@foreach ($this->getUsers() as $user)
user info
@foreach ($user->tags as $tag)
tag info
@endforeach
@endforeach';
$expected = '<?php foreach($this->getUsers() as $user): ?>
user info
<?php foreach($user->tags as $tag): ?>
tag info
<?php endforeach; ?>
<?php endforeach; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testForelseStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@forelse ($this->getUsers() as $user)
breeze
@empty
empty
@endforelse';
$expected = '<?php $__empty_1 = true; foreach($this->getUsers() as $user): $__empty_1 = false; ?>
breeze
<?php endforeach; if ($__empty_1): ?>
empty
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testNestedForelseStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@forelse ($this->getUsers() as $user)
@forelse ($user->tags as $tag)
breeze
@empty
tag empty
@endforelse
@empty
empty
@endforelse';
$expected = '<?php $__empty_1 = true; foreach($this->getUsers() as $user): $__empty_1 = false; ?>
<?php $__empty_2 = true; foreach($user->tags as $tag): $__empty_2 = false; ?>
breeze
<?php endforeach; if ($__empty_2): ?>
tag empty
<?php endif; ?>
<?php endforeach; if ($__empty_1): ?>
empty
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testPhpStatementsWithExpressionAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@php($set = true)';
$expected = '<?php ($set = true); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testPhpStatementsWithoutExpressionAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@php';
$expected = '<?php ';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testEndphpStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@endphp';
$expected = ' ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testUnsetStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@unset ($unset)';
$expected = '<?php unset($unset); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testVerbatimBlocksAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@verbatim {{ $a }} @if($b) {{ $b }} @endif @endverbatim';
$expected = ' {{ $a }} @if($b) {{ $b }} @endif ';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testVerbatimBlocksWithMultipleLinesAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = 'Some text
@verbatim
{{ $a }}
@if($b)
{{ $b }}
@endif
@endverbatim';
$expected = 'Some text
{{ $a }}
@if($b)
{{ $b }}
@endif
';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testMultipleVerbatimBlocksAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@verbatim {{ $a }} @endverbatim {{ $b }} @verbatim {{ $c }} @endverbatim';
$expected = ' {{ $a }} <?php echo e($b); ?> {{ $c }} ';
$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 app('translator')->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 testIncludeIfsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php if ($__env->exists(\'foo\')) echo $__env->make(\'foo\', array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@includeIf(\'foo\')'));
$this->assertEquals('<?php if ($__env->exists(name(foo))) echo $__env->make(name(foo), array_except(get_defined_vars(), array(\'__data\', \'__path\')))->render(); ?>', $compiler->compileString('@includeIf(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 app(\'translator\')->get(\'foo\'); ?>', $compiler->compileString("@lang('foo')"));
$this->assertEquals('<?php echo app(\'translator\')->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 testOverwriteSectionsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals('<?php $__env->stopSection(true); ?>', $compiler->compileString('@overwrite'));
}
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 e(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 testCustomStatements()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertCount(0, $compiler->getCustomDirectives());
$compiler->directive('customControl', function ($expression) {
return "<?php echo custom_control{$expression}; ?>";
});
$this->assertCount(1, $compiler->getCustomDirectives());
$string = '@if($foo)
@customControl(10, $foo, \'bar\')
@endif';
$expected = '<?php if($foo): ?>
<?php echo custom_control(10, $foo, \'bar\'); ?>
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testCustomShortStatements()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$compiler->directive('customControl', function ($expression) {
return '<?php echo custom_control(); ?>';
});
$string = '@customControl';
$expected = '<?php echo custom_control(); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testCustomExtensionOverwritesCore()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$compiler->directive('foreach', function ($expression) {
return '<?php custom(); ?>';
});
$string = '@foreach';
$expected = '<?php custom(); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}
public function testRawTagsCanBeSetToLegacyValues()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$compiler->setEchoFormat('%s');
$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 app(\'translator\')->get(foo(bar(baz(qux(breeze()))))); ?> space () <?php echo app(\'translator\')->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 e($foo); ?>>', $compiler->compileString('<html {{ $foo }}>'));
$this->assertEquals('<html<?php echo e($foo); ?>>', $compiler->compileString('<html{{ $foo }}>'));
$this->assertEquals('<html <?php echo e($foo); ?> <?php echo app(\'translator\')->get(\'foo\'); ?>>', $compiler->compileString('<html {{ $foo }} @lang(\'foo\')>'));
}
protected function getFiles()
{
return m::mock('Illuminate\Filesystem\Filesystem');
}
public function testRetrieveDefaultContentTags()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals(['{{', '}}'], $compiler->getContentTags());
}
public function testRetrieveDefaultEscapedContentTags()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$this->assertEquals(['{{{', '}}}'], $compiler->getEscapedContentTags());
}
public function testSequentialCompileStringCalls()
{
$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));
// use the same compiler instance to compile another template with @extends directive
$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 testGetTagsProvider()
{
return [
['{{', '}}'],
['{{{', '}}}'],
['[[', ']]'],
['[[[', ']]]'],
['((', '))'],
['(((', ')))'],
];
}
}