ComponentTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Illuminate\Tests\View;
  3. use Illuminate\Config\Repository as Config;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Contracts\Support\Htmlable;
  6. use Illuminate\Contracts\View\Factory as FactoryContract;
  7. use Illuminate\Support\Facades\Facade;
  8. use Illuminate\Support\HtmlString;
  9. use Illuminate\View\Component;
  10. use Illuminate\View\Factory;
  11. use Illuminate\View\View;
  12. use Mockery as m;
  13. use PHPUnit\Framework\TestCase;
  14. class ComponentTest extends TestCase
  15. {
  16. protected $viewFactory;
  17. protected $config;
  18. protected function setUp(): void
  19. {
  20. $this->config = m::mock(Config::class);
  21. $container = new Container;
  22. $this->viewFactory = m::mock(Factory::class);
  23. $container->instance('view', $this->viewFactory);
  24. $container->alias('view', FactoryContract::class);
  25. $container->instance('config', $this->config);
  26. Container::setInstance($container);
  27. Facade::setFacadeApplication($container);
  28. parent::setUp();
  29. }
  30. protected function tearDown(): void
  31. {
  32. m::close();
  33. Facade::clearResolvedInstances();
  34. Facade::setFacadeApplication(null);
  35. Container::setInstance(null);
  36. }
  37. public function testInlineViewsGetCreated()
  38. {
  39. $this->config->shouldReceive('get')->once()->with('view.compiled')->andReturn('/tmp');
  40. $this->viewFactory->shouldReceive('exists')->once()->andReturn(false);
  41. $this->viewFactory->shouldReceive('addNamespace')->once()->with('__components', '/tmp');
  42. $component = new TestInlineViewComponent;
  43. $this->assertSame('__components::c6327913fef3fca4518bcd7df1d0ff630758e241', $component->resolveView());
  44. }
  45. public function testRegularViewsGetReturned()
  46. {
  47. $view = m::mock(View::class);
  48. $this->viewFactory->shouldReceive('make')->once()->with('alert', [], [])->andReturn($view);
  49. $component = new TestRegularViewComponent;
  50. $this->assertSame($view, $component->resolveView());
  51. }
  52. public function testRegularViewNamesGetReturned()
  53. {
  54. $this->viewFactory->shouldReceive('exists')->once()->andReturn(true);
  55. $this->viewFactory->shouldReceive('addNamespace')->never();
  56. $component = new TestRegularViewNameViewComponent;
  57. $this->assertSame('alert', $component->resolveView());
  58. }
  59. public function testHtmlablesGetReturned()
  60. {
  61. $component = new TestHtmlableReturningViewComponent;
  62. $view = $component->resolveView();
  63. $this->assertInstanceOf(Htmlable::class, $view);
  64. $this->assertSame('<p>Hello foo</p>', $view->toHtml());
  65. }
  66. }
  67. class TestInlineViewComponent extends Component
  68. {
  69. public $title;
  70. public function __construct($title = 'foo')
  71. {
  72. $this->title = $title;
  73. }
  74. public function render()
  75. {
  76. return 'Hello {{ $title }}';
  77. }
  78. }
  79. class TestRegularViewComponent extends Component
  80. {
  81. public $title;
  82. public function __construct($title = 'foo')
  83. {
  84. $this->title = $title;
  85. }
  86. public function render()
  87. {
  88. return view('alert');
  89. }
  90. }
  91. class TestRegularViewNameViewComponent extends Component
  92. {
  93. public $title;
  94. public function __construct($title = 'foo')
  95. {
  96. $this->title = $title;
  97. }
  98. public function render()
  99. {
  100. return 'alert';
  101. }
  102. }
  103. class TestHtmlableReturningViewComponent extends Component
  104. {
  105. protected $title;
  106. public function __construct($title = 'foo')
  107. {
  108. $this->title = $title;
  109. }
  110. public function render()
  111. {
  112. return new HtmlString("<p>Hello {$this->title}</p>");
  113. }
  114. }