1+ <?php
2+
3+ use Illuminate \Support \Facades \File ;
4+ use Redberry \MailboxForLaravel \Http \Controllers \PublicAssetController ;
5+
6+ describe (PublicAssetController::class, function () {
7+ beforeEach (function () {
8+ // Ensure we have a clean dist directory for testing
9+ $ testDir = __DIR__ . '/../../dist/test-assets ' ;
10+ if (!File::exists ($ testDir )) {
11+ File::makeDirectory ($ testDir , 0755 , true );
12+ }
13+
14+ // Create test files
15+ File::put ($ testDir . '/test-file.js ' , 'console.log("test"); ' );
16+ File::put ($ testDir . '/test-file.css ' , 'body { color: red; } ' );
17+ File::put ($ testDir . '/test-file.js.map ' , '{"version":3} ' );
18+ File::put ($ testDir . '/unknown-file.xyz ' , 'binary data ' );
19+ });
20+
21+ afterEach (function () {
22+ // Clean up test files
23+ $ testDir = __DIR__ . '/../../dist/test-assets ' ;
24+ if (File::exists ($ testDir )) {
25+ File::deleteDirectory ($ testDir );
26+ }
27+ });
28+
29+ it ('serves JavaScript files with correct content type ' , function () {
30+ $ controller = new PublicAssetController ();
31+ $ response = $ controller ->__invoke ('test-assets/test-file.js ' );
32+
33+ expect ($ response )->toBeInstanceOf (Symfony \Component \HttpFoundation \BinaryFileResponse::class);
34+ expect ($ response ->headers ->get ('Content-Type ' ))->toContain ('application/javascript ' );
35+ });
36+
37+ it ('serves CSS files with correct content type ' , function () {
38+ $ controller = new PublicAssetController ();
39+ $ response = $ controller ->__invoke ('test-assets/test-file.css ' );
40+
41+ expect ($ response )->toBeInstanceOf (Symfony \Component \HttpFoundation \BinaryFileResponse::class);
42+ expect ($ response ->headers ->get ('Content-Type ' ))->toContain ('text/css ' );
43+ });
44+
45+ it ('serves source map files with correct content type ' , function () {
46+ $ controller = new PublicAssetController ();
47+ $ response = $ controller ->__invoke ('test-assets/test-file.js.map ' );
48+
49+ expect ($ response )->toBeInstanceOf (Symfony \Component \HttpFoundation \BinaryFileResponse::class);
50+ expect ($ response ->headers ->get ('Content-Type ' ))->toContain ('application/json ' );
51+ });
52+
53+ it ('returns 404 for non-existent files ' , function () {
54+ $ controller = new PublicAssetController ();
55+
56+ // This should trigger an abort(404)
57+ expect (fn () => $ controller ->__invoke ('non-existent-file.js ' ))
58+ ->toThrow (Symfony \Component \HttpKernel \Exception \HttpException::class);
59+ });
60+
61+ it ('uses fallback mime type for unknown file extensions ' , function () {
62+ $ controller = new PublicAssetController ();
63+ $ response = $ controller ->__invoke ('test-assets/unknown-file.xyz ' );
64+
65+ expect ($ response )->toBeInstanceOf (Symfony \Component \HttpFoundation \BinaryFileResponse::class);
66+ // Should fall back to File::mimeType() detection
67+ });
68+
69+ it ('sets appropriate cache headers for asset files ' , function () {
70+ $ controller = new PublicAssetController ();
71+ $ response = $ controller ->__invoke ('test-assets/test-file.js ' );
72+
73+ $ cacheControl = $ response ->headers ->get ('Cache-Control ' );
74+ expect ($ cacheControl )->toContain ('public ' );
75+ expect ($ cacheControl )->toContain ('max-age=31536000 ' );
76+ expect ($ cacheControl )->toContain ('immutable ' );
77+ });
78+
79+ it ('determines correct MIME types based on file extensions ' , function () {
80+ $ controller = new PublicAssetController ();
81+
82+ // Test JS files
83+ $ jsResponse = $ controller ->__invoke ('test-assets/test-file.js ' );
84+ expect ($ jsResponse ->headers ->get ('Content-Type ' ))->toContain ('application/javascript ' );
85+
86+ // Test CSS files
87+ $ cssResponse = $ controller ->__invoke ('test-assets/test-file.css ' );
88+ expect ($ cssResponse ->headers ->get ('Content-Type ' ))->toContain ('text/css ' );
89+
90+ // Test map files
91+ $ mapResponse = $ controller ->__invoke ('test-assets/test-file.js.map ' );
92+ expect ($ mapResponse ->headers ->get ('Content-Type ' ))->toContain ('application/json ' );
93+ });
94+ });
0 commit comments