BladeCustomTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Illuminate\Tests\View\Blade;
  3. use InvalidArgumentException;
  4. class BladeCustomTest extends AbstractBladeTestCase
  5. {
  6. public function testCustomPhpCodeIsCorrectlyHandled()
  7. {
  8. $this->assertSame('<?php if($test): ?> <?php @show(\'test\'); ?> <?php endif; ?>', $this->compiler->compileString("@if(\$test) <?php @show('test'); ?> @endif"));
  9. }
  10. public function testMixingYieldAndEcho()
  11. {
  12. $this->assertSame('<?php echo $__env->yieldContent(\'title\'); ?> - <?php echo e(Config::get(\'site.title\')); ?>', $this->compiler->compileString("@yield('title') - {{Config::get('site.title')}}"));
  13. }
  14. public function testCustomExtensionsAreCompiled()
  15. {
  16. $this->compiler->extend(function ($value) {
  17. return str_replace('foo', 'bar', $value);
  18. });
  19. $this->assertSame('bar', $this->compiler->compileString('foo'));
  20. }
  21. public function testCustomStatements()
  22. {
  23. $this->assertCount(0, $this->compiler->getCustomDirectives());
  24. $this->compiler->directive('customControl', function ($expression) {
  25. return "<?php echo custom_control({$expression}); ?>";
  26. });
  27. $this->assertCount(1, $this->compiler->getCustomDirectives());
  28. $string = '@if($foo)
  29. @customControl(10, $foo, \'bar\')
  30. @endif';
  31. $expected = '<?php if($foo): ?>
  32. <?php echo custom_control(10, $foo, \'bar\'); ?>
  33. <?php endif; ?>';
  34. $this->assertEquals($expected, $this->compiler->compileString($string));
  35. }
  36. public function testCustomShortStatements()
  37. {
  38. $this->compiler->directive('customControl', function ($expression) {
  39. return '<?php echo custom_control(); ?>';
  40. });
  41. $string = '@customControl';
  42. $expected = '<?php echo custom_control(); ?>';
  43. $this->assertEquals($expected, $this->compiler->compileString($string));
  44. }
  45. public function testValidCustomNames()
  46. {
  47. $this->assertNull($this->compiler->directive('custom', function () {
  48. //
  49. }));
  50. $this->assertNull($this->compiler->directive('custom_custom', function () {
  51. //
  52. }));
  53. $this->assertNull($this->compiler->directive('customCustom', function () {
  54. //
  55. }));
  56. $this->assertNull($this->compiler->directive('custom::custom', function () {
  57. //
  58. }));
  59. }
  60. public function testInvalidCustomNames()
  61. {
  62. $this->expectException(InvalidArgumentException::class);
  63. $this->expectExceptionMessage('The directive name [custom-custom] is not valid.');
  64. $this->compiler->directive('custom-custom', function () {
  65. //
  66. });
  67. }
  68. public function testInvalidCustomNames2()
  69. {
  70. $this->expectException(InvalidArgumentException::class);
  71. $this->expectExceptionMessage('The directive name [custom:custom] is not valid.');
  72. $this->compiler->directive('custom:custom', function () {
  73. //
  74. });
  75. }
  76. public function testCustomExtensionOverwritesCore()
  77. {
  78. $this->compiler->directive('foreach', function ($expression) {
  79. return '<?php custom(); ?>';
  80. });
  81. $string = '@foreach';
  82. $expected = '<?php custom(); ?>';
  83. $this->assertEquals($expected, $this->compiler->compileString($string));
  84. }
  85. public function testCustomConditions()
  86. {
  87. $this->compiler->if('custom', function ($user) {
  88. return true;
  89. });
  90. $string = '@custom($user)
  91. @endcustom';
  92. $expected = '<?php if (\Illuminate\Support\Facades\Blade::check(\'custom\', $user)): ?>
  93. <?php endif; ?>';
  94. $this->assertEquals($expected, $this->compiler->compileString($string));
  95. }
  96. public function testCustomIfElseConditions()
  97. {
  98. $this->compiler->if('custom', function ($anything) {
  99. return true;
  100. });
  101. $string = '@custom($user)
  102. @elsecustom($product)
  103. @else
  104. @endcustom';
  105. $expected = '<?php if (\Illuminate\Support\Facades\Blade::check(\'custom\', $user)): ?>
  106. <?php elseif (\Illuminate\Support\Facades\Blade::check(\'custom\', $product)): ?>
  107. <?php else: ?>
  108. <?php endif; ?>';
  109. $this->assertEquals($expected, $this->compiler->compileString($string));
  110. }
  111. public function testCustomUnlessConditions()
  112. {
  113. $this->compiler->if('custom', function ($anything) {
  114. return true;
  115. });
  116. $string = '@unlesscustom($user)
  117. @endcustom';
  118. $expected = '<?php if (! \Illuminate\Support\Facades\Blade::check(\'custom\', $user)): ?>
  119. <?php endif; ?>';
  120. $this->assertEquals($expected, $this->compiler->compileString($string));
  121. }
  122. public function testCustomConditionsAccepts0AsArgument()
  123. {
  124. $this->compiler->if('custom', function ($number) {
  125. return true;
  126. });
  127. $string = '@custom(0)
  128. @elsecustom(0)
  129. @endcustom';
  130. $expected = '<?php if (\Illuminate\Support\Facades\Blade::check(\'custom\', 0)): ?>
  131. <?php elseif (\Illuminate\Support\Facades\Blade::check(\'custom\', 0)): ?>
  132. <?php endif; ?>';
  133. $this->assertEquals($expected, $this->compiler->compileString($string));
  134. }
  135. public function testCustomComponents()
  136. {
  137. $this->compiler->aliasComponent('app.components.alert', 'alert');
  138. $string = '@alert
  139. @endalert';
  140. $expected = '<?php $__env->startComponent(\'app.components.alert\'); ?>
  141. <?php echo $__env->renderComponent(); ?>';
  142. $this->assertEquals($expected, $this->compiler->compileString($string));
  143. }
  144. public function testCustomComponentsWithSlots()
  145. {
  146. $this->compiler->aliasComponent('app.components.alert', 'alert');
  147. $string = '@alert([\'type\' => \'danger\'])
  148. @endalert';
  149. $expected = '<?php $__env->startComponent(\'app.components.alert\', [\'type\' => \'danger\']); ?>
  150. <?php echo $__env->renderComponent(); ?>';
  151. $this->assertEquals($expected, $this->compiler->compileString($string));
  152. }
  153. public function testCustomComponentsWithExistingDirective()
  154. {
  155. $this->compiler->aliasComponent('app.components.foreach', 'foreach');
  156. $string = '@foreach
  157. @endforeach';
  158. $expected = '<?php $__env->startComponent(\'app.components.foreach\'); ?>
  159. <?php echo $__env->renderComponent(); ?>';
  160. $this->assertEquals($expected, $this->compiler->compileString($string));
  161. }
  162. public function testCustomIncludes()
  163. {
  164. $this->compiler->include('app.includes.input', 'input');
  165. $string = '@input';
  166. $expected = '<?php echo $__env->make(\'app.includes.input\', [], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>';
  167. $this->assertEquals($expected, $this->compiler->compileString($string));
  168. }
  169. public function testCustomIncludesWithData()
  170. {
  171. $this->compiler->include('app.includes.input', 'input');
  172. $string = '@input([\'type\' => \'email\'])';
  173. $expected = '<?php echo $__env->make(\'app.includes.input\', [\'type\' => \'email\'], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>';
  174. $this->assertEquals($expected, $this->compiler->compileString($string));
  175. }
  176. public function testCustomIncludesDefaultAlias()
  177. {
  178. $this->compiler->include('app.includes.input');
  179. $string = '@input';
  180. $expected = '<?php echo $__env->make(\'app.includes.input\', [], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>';
  181. $this->assertEquals($expected, $this->compiler->compileString($string));
  182. }
  183. public function testCustomIncludesWithExistingDirective()
  184. {
  185. $this->compiler->include('app.includes.foreach');
  186. $string = '@foreach';
  187. $expected = '<?php echo $__env->make(\'app.includes.foreach\', [], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>';
  188. $this->assertEquals($expected, $this->compiler->compileString($string));
  189. }
  190. }