BladeTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Illuminate\Tests\Integration\View;
  3. use Illuminate\Support\Facades\Blade;
  4. use Illuminate\Support\Facades\View;
  5. use Illuminate\View\Component;
  6. use Orchestra\Testbench\TestCase;
  7. class BladeTest extends TestCase
  8. {
  9. public function test_rendering_blade_string()
  10. {
  11. $this->assertSame('Hello Taylor', Blade::render('Hello {{ $name }}', ['name' => 'Taylor']));
  12. }
  13. public function test_rendering_blade_long_maxpathlen_string()
  14. {
  15. $longString = str_repeat('a', PHP_MAXPATHLEN);
  16. $result = Blade::render($longString.'{{ $name }}', ['name' => 'a']);
  17. $this->assertSame($longString.'a', $result);
  18. }
  19. public function test_rendering_blade_component_instance()
  20. {
  21. $component = new HelloComponent('Taylor');
  22. $this->assertSame('Hello Taylor', Blade::renderComponent($component));
  23. }
  24. public function test_basic_blade_rendering()
  25. {
  26. $view = View::make('hello', ['name' => 'Taylor'])->render();
  27. $this->assertSame('Hello Taylor', trim($view));
  28. }
  29. public function test_rendering_a_component()
  30. {
  31. $view = View::make('uses-panel', ['name' => 'Taylor'])->render();
  32. $this->assertSame('<div class="ml-2">
  33. Hello Taylor
  34. </div>', trim($view));
  35. }
  36. public function test_rendering_a_dynamic_component()
  37. {
  38. $view = View::make('uses-panel-dynamically', ['name' => 'Taylor'])->render();
  39. $this->assertSame('<div class="ml-2" wire:model="foo" wire:model.lazy="bar">
  40. Hello Taylor
  41. </div>', trim($view));
  42. }
  43. public function test_rendering_the_same_dynamic_component_with_different_attributes()
  44. {
  45. $view = View::make('varied-dynamic-calls')->render();
  46. $this->assertSame('<span class="text-medium">
  47. Hello Taylor
  48. </span>
  49. <span >
  50. Hello Samuel
  51. </span>', trim($view));
  52. }
  53. public function test_inline_link_type_attributes_dont_add_extra_spacing_at_end()
  54. {
  55. $view = View::make('uses-link')->render();
  56. $this->assertSame('This is a sentence with a <a href="https://laravel.com">link</a>.', trim($view));
  57. }
  58. public function test_appendable_attributes()
  59. {
  60. $view = View::make('uses-appendable-panel', ['name' => 'Taylor', 'withInjectedValue' => true])->render();
  61. $this->assertSame('<div class="mt-4 bg-gray-100" data-controller="inside-controller outside-controller" foo="bar">
  62. Hello Taylor
  63. </div>', trim($view));
  64. $view = View::make('uses-appendable-panel', ['name' => 'Taylor', 'withInjectedValue' => false])->render();
  65. $this->assertSame('<div class="mt-4 bg-gray-100" data-controller="inside-controller" foo="bar">
  66. Hello Taylor
  67. </div>', trim($view));
  68. }
  69. public function tested_nested_anonymous_attribute_proxying_works_correctly()
  70. {
  71. $view = View::make('uses-child-input')->render();
  72. $this->assertSame('<input class="disabled-class" foo="bar" type="text" disabled />', trim($view));
  73. }
  74. public function test_consume_defaults()
  75. {
  76. $view = View::make('consume')->render();
  77. $this->assertSame('<h1>Menu</h1>
  78. <div>Slot: A, Color: orange, Default: foo</div>
  79. <div>Slot: B, Color: red, Default: foo</div>
  80. <div>Slot: C, Color: blue, Default: foo</div>
  81. <div>Slot: D, Color: red, Default: foo</div>
  82. <div>Slot: E, Color: red, Default: foo</div>
  83. <div>Slot: F, Color: yellow, Default: foo</div>', trim($view));
  84. }
  85. public function test_consume_with_props()
  86. {
  87. $view = View::make('consume', ['color' => 'rebeccapurple'])->render();
  88. $this->assertSame('<h1>Menu</h1>
  89. <div>Slot: A, Color: orange, Default: foo</div>
  90. <div>Slot: B, Color: rebeccapurple, Default: foo</div>
  91. <div>Slot: C, Color: blue, Default: foo</div>
  92. <div>Slot: D, Color: rebeccapurple, Default: foo</div>
  93. <div>Slot: E, Color: rebeccapurple, Default: foo</div>
  94. <div>Slot: F, Color: yellow, Default: foo</div>', trim($view));
  95. }
  96. protected function getEnvironmentSetUp($app)
  97. {
  98. $app['config']->set('view.paths', [__DIR__.'/templates']);
  99. }
  100. }
  101. class HelloComponent extends Component
  102. {
  103. public $name;
  104. public function __construct(string $name)
  105. {
  106. $this->name = $name;
  107. }
  108. public function render()
  109. {
  110. return 'Hello {{ $name }}';
  111. }
  112. }