Skip to content

Commit 512a989

Browse files
committed
Added tests for version hash
1 parent 33aeb80 commit 512a989

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/MiddlewareTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Inertia\Tests;
44

5+
use Illuminate\Filesystem\Filesystem;
56
use Illuminate\Http\Request;
67
use Illuminate\Session\Middleware\StartSession;
78
use Illuminate\Support\Facades\Route;
@@ -13,9 +14,16 @@
1314
use Inertia\Middleware;
1415
use Inertia\Tests\Stubs\ExampleMiddleware;
1516
use LogicException;
17+
use PHPUnit\Framework\Attributes\After;
1618

1719
class MiddlewareTest extends TestCase
1820
{
21+
#[After]
22+
public function cleanupPublicFolder(): void
23+
{
24+
(new Filesystem)->cleanDirectory(public_path());
25+
}
26+
1927
public function test_no_response_value_by_default_means_automatically_redirecting_back_for_inertia_requests(): void
2028
{
2129
$fooCalled = false;
@@ -241,6 +249,49 @@ public function rootView(Request $request): string
241249
$response->assertViewIs('welcome');
242250
}
243251

252+
public function test_determine_the_version_by_a_hash_of_the_asset_url(): void
253+
{
254+
config(['app.asset_url' => $url = 'https://example.com/assets']);
255+
256+
$this->prepareMockEndpoint(middleware: new Middleware);
257+
258+
$response = $this->get('/');
259+
$response->assertOk();
260+
$response->assertViewHas('page.version', hash('xxh128', $url));
261+
}
262+
263+
public function test_determine_the_version_by_a_hash_of_the_vite_manifest(): void
264+
{
265+
$filesystem = new Filesystem;
266+
$filesystem->ensureDirectoryExists(public_path('build'));
267+
$filesystem->put(
268+
public_path('build/manifest.json'),
269+
$contents = json_encode(['vite' => true])
270+
);
271+
272+
$this->prepareMockEndpoint(middleware: new Middleware);
273+
274+
$response = $this->get('/');
275+
$response->assertOk();
276+
$response->assertViewHas('page.version', hash('xxh128', $contents));
277+
}
278+
279+
public function test_determine_the_version_by_a_hash_of_the_mix_manifest(): void
280+
{
281+
$filesystem = new Filesystem;
282+
$filesystem->ensureDirectoryExists(public_path());
283+
$filesystem->put(
284+
public_path('mix-manifest.json'),
285+
$contents = json_encode(['mix' => true])
286+
);
287+
288+
$this->prepareMockEndpoint(middleware: new Middleware);
289+
290+
$response = $this->get('/');
291+
$response->assertOk();
292+
$response->assertViewHas('page.version', hash('xxh128', $contents));
293+
}
294+
244295
private function prepareMockEndpoint($version = null, $shared = [], $middleware = null): \Illuminate\Routing\Route
245296
{
246297
if (is_null($middleware)) {

0 commit comments

Comments
 (0)