|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Inertia\Tests; |
| 4 | + |
| 5 | +use Illuminate\Filesystem\Filesystem; |
| 6 | +use Illuminate\Support\Facades\Config; |
| 7 | +use Illuminate\View\Compilers\BladeCompiler; |
| 8 | +use Illuminate\View\Engines\PhpEngine; |
| 9 | +use Illuminate\View\Factory; |
| 10 | +use Illuminate\View\View; |
| 11 | +use Inertia\Directive; |
| 12 | +use Inertia\Ssr\Gateway; |
| 13 | +use Inertia\Tests\Stubs\FakeGateway; |
| 14 | +use Mockery as m; |
| 15 | + |
| 16 | +class DirectiveTest extends TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var Filesystem|m\MockInterface |
| 20 | + */ |
| 21 | + private $filesystem; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var BladeCompiler |
| 25 | + */ |
| 26 | + protected $compiler; |
| 27 | + |
| 28 | + /** |
| 29 | + * Example Page Objects. |
| 30 | + */ |
| 31 | + protected const EXAMPLE_PAGE_OBJECT = ['component' => 'Foo/Bar', 'props' => ['foo' => 'bar'], 'url' => '/test', 'version' => '']; |
| 32 | + |
| 33 | + public function setUp(): void |
| 34 | + { |
| 35 | + parent::setUp(); |
| 36 | + |
| 37 | + $this->app->bind(Gateway::class, FakeGateway::class); |
| 38 | + $this->filesystem = m::mock(Filesystem::class); |
| 39 | + |
| 40 | + $this->compiler = new BladeCompiler($this->filesystem, __DIR__.'/cache/views'); |
| 41 | + $this->compiler->directive('inertia', [Directive::class, 'compile']); |
| 42 | + $this->compiler->directive('inertiaHead', [Directive::class, 'compileHead']); |
| 43 | + } |
| 44 | + |
| 45 | + protected function tearDown(): void |
| 46 | + { |
| 47 | + m::close(); |
| 48 | + parent::tearDown(); |
| 49 | + } |
| 50 | + |
| 51 | + protected function renderView($contents, $data = []) |
| 52 | + { |
| 53 | + // First, we'll create a temporary file, and use compileString to 'emulate' compilation of our view. |
| 54 | + // This skips caching, and a bunch of other logic that's not relevant for what we need here. |
| 55 | + $path = tempnam(sys_get_temp_dir(), 'inertia_tests_render_'); |
| 56 | + file_put_contents($path, $this->compiler->compileString($contents)); |
| 57 | + |
| 58 | + // Next, we'll 'render' out compiled view. |
| 59 | + $view = new View( |
| 60 | + m::mock(Factory::class), |
| 61 | + new PhpEngine(new Filesystem()), |
| 62 | + 'fake-view', |
| 63 | + $path, |
| 64 | + $data |
| 65 | + ); |
| 66 | + |
| 67 | + // Then, we'll just hack and slash our way to success.. |
| 68 | + $view->getFactory()->allows('incrementRender')->once(); |
| 69 | + $view->getFactory()->allows('callComposer')->once(); |
| 70 | + $view->getFactory()->allows('getShared')->once()->andReturn([]); |
| 71 | + $view->getFactory()->allows('decrementRender')->once(); |
| 72 | + $view->getFactory()->allows('flushStateIfDoneRendering')->once(); |
| 73 | + $view->getFactory()->allows('flushState'); |
| 74 | + |
| 75 | + try { |
| 76 | + $output = $view->render(); |
| 77 | + @unlink($path); |
| 78 | + } catch (\Throwable $e) { |
| 79 | + @unlink($path); |
| 80 | + throw $e; |
| 81 | + } |
| 82 | + |
| 83 | + return $output; |
| 84 | + } |
| 85 | + |
| 86 | + public function test_inertia_directive_renders_the_root_element(): void |
| 87 | + { |
| 88 | + $html = '<div id="app" data-page="{"component":"Foo\/Bar","props":{"foo":"bar"},"url":"\/test","version":""}"></div>'; |
| 89 | + |
| 90 | + $this->assertSame($html, $this->renderView('@inertia', ['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 91 | + $this->assertSame($html, $this->renderView('@inertia()', ['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 92 | + $this->assertSame($html, $this->renderView('@inertia("")', ['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 93 | + $this->assertSame($html, $this->renderView("@inertia('')", ['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 94 | + } |
| 95 | + |
| 96 | + public function test_inertia_directive_renders_server_side_rendered_content_when_enabled(): void |
| 97 | + { |
| 98 | + Config::set(['inertia.ssr.enabled' => true]); |
| 99 | + |
| 100 | + $this->assertSame( |
| 101 | + '<p>This is some example SSR content</p>', |
| 102 | + $this->renderView('@inertia', ['page' => self::EXAMPLE_PAGE_OBJECT]) |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + public function test_inertia_directive_can_use_a_different_root_element_id(): void |
| 107 | + { |
| 108 | + $html = '<div id="foo" data-page="{"component":"Foo\/Bar","props":{"foo":"bar"},"url":"\/test","version":""}"></div>'; |
| 109 | + |
| 110 | + $this->assertSame($html, $this->renderView('@inertia(foo)', ['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 111 | + $this->assertSame($html, $this->renderView("@inertia('foo')", ['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 112 | + $this->assertSame($html, $this->renderView('@inertia("foo")', ['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 113 | + } |
| 114 | + |
| 115 | + public function test_inertia_head_directive_renders_nothing(): void |
| 116 | + { |
| 117 | + $this->assertEmpty($this->renderView('@inertiaHead', ['page' => self::EXAMPLE_PAGE_OBJECT])); |
| 118 | + } |
| 119 | + |
| 120 | + public function test_inertia_head_directive_renders_server_side_rendered_head_elements_when_enabled(): void |
| 121 | + { |
| 122 | + Config::set(['inertia.ssr.enabled' => true]); |
| 123 | + |
| 124 | + $this->assertSame( |
| 125 | + "<meta charset=\"UTF-8\" />\n<title inertia>Example SSR Title</title>\n", |
| 126 | + $this->renderView('@inertiaHead', ['page' => self::EXAMPLE_PAGE_OBJECT]) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + public function test_the_server_side_rendering_request_is_dispatched_only_once_per_request(): void |
| 131 | + { |
| 132 | + Config::set(['inertia.ssr.enabled' => true]); |
| 133 | + $this->app->instance(Gateway::class, $gateway = new FakeGateway()); |
| 134 | + |
| 135 | + $view = "<!DOCTYPE html>\n<html>\n<head>\n@inertiaHead\n</head>\n<body>\n@inertia\n</body>\n</html>"; |
| 136 | + $expected = "<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\" />\n<title inertia>Example SSR Title</title>\n</head>\n<body>\n<p>This is some example SSR content</p></body>\n</html>"; |
| 137 | + |
| 138 | + $this->assertSame( |
| 139 | + $expected, |
| 140 | + $this->renderView($view, ['page' => self::EXAMPLE_PAGE_OBJECT]) |
| 141 | + ); |
| 142 | + |
| 143 | + $this->assertSame(1, $gateway->times); |
| 144 | + } |
| 145 | +} |
0 commit comments