10
10
11
11
class PublishedResourceTest extends TestUnitCase
12
12
{
13
+ public function testFilesArePublished (): void
14
+ {
15
+ $ this ->assertFileExists (Dashboard::path ('/public/assets/app.css ' ));
16
+ $ this ->assertFileExists (Dashboard::path ('/public/assets/app.js ' ));
17
+ }
18
+
13
19
/**
14
20
* This test will check the size of the published files,
15
21
* so as not to accidentally publish non-minified versions.
16
22
*
17
23
* Usually to solve this problem you only need to run the Laravel Mix:
18
- * `npm run production `
24
+ * `npm run build `
19
25
*
20
26
* These are approximate values that can be changed.
21
27
*/
@@ -26,12 +32,71 @@ public function testFilesAreMinified(): void
26
32
27
33
$ this ->assertLessThan ($ maxCssSize ,
28
34
filesize (Dashboard::path ('/public/assets/app.css ' )),
29
- 'File orchid.css more ' . Formats::formatBytes ($ maxCssSize )
35
+ 'File orchid.css more ' . Formats::formatBytes ($ maxCssSize )
30
36
);
31
37
32
38
$ this ->assertLessThan ($ maxJsSize ,
33
39
filesize (Dashboard::path ('/public/assets/app.js ' )),
34
- 'File orchid.js more ' . Formats::formatBytes ($ maxJsSize )
40
+ 'File orchid.js more ' . Formats::formatBytes ($ maxJsSize )
35
41
);
36
42
}
43
+
44
+ public function testManifestFilesHaveVersion (): void
45
+ {
46
+ $ manifestPath = Dashboard::path ('/public/manifest.json ' );
47
+ $ this ->assertFileExists ($ manifestPath , 'Manifest file does not exist. ' );
48
+
49
+ $ manifestContent = file_get_contents ($ manifestPath );
50
+ $ this ->assertNotFalse ($ manifestContent , 'Failed to read manifest file. ' );
51
+
52
+ $ manifest = json_decode ($ manifestContent , true );
53
+ $ this ->assertIsArray ($ manifest , 'Manifest is not a valid JSON array. ' );
54
+
55
+ foreach ($ manifest as $ source => $ data ) {
56
+ $ this ->assertArrayHasKey ('file ' , $ data , "Missing 'file' key in manifest entry: $ source " );
57
+
58
+ $ file = $ data ['file ' ];
59
+
60
+ $ this ->assertStringContainsString (
61
+ '?v= ' ,
62
+ $ file ,
63
+ "File ' $ file' for source ' $ source' is missing version query parameter '?v=' "
64
+ );
65
+ }
66
+ }
67
+
68
+ public function testManifestFilesExist (): void
69
+ {
70
+ $ manifestPath = Dashboard::path ('/public/manifest.json ' );
71
+ $ manifest = json_decode (file_get_contents ($ manifestPath ), true );
72
+
73
+ foreach ($ manifest as $ data ) {
74
+ $ filePath = explode ('? ' , $ data ['file ' ])[0 ]; // Remove query string
75
+ $ fullPath = Dashboard::path ("/public/ {$ filePath }" );
76
+
77
+ $ this ->assertFileExists ($ fullPath , "Asset file does not exist: {$ filePath }" );
78
+ }
79
+ }
80
+
81
+ public function testManifestEntriesHaveIntegrity (): void
82
+ {
83
+ $ manifestPath = Dashboard::path ('/public/manifest.json ' );
84
+ $ manifest = json_decode (file_get_contents ($ manifestPath ), true );
85
+
86
+ foreach ($ manifest as $ source => $ data ) {
87
+ $ this ->assertArrayHasKey ('integrity ' , $ data , "Missing 'integrity' for $ source " );
88
+ $ this ->assertMatchesRegularExpression ('/^sha(256|384|512)-/ ' , $ data ['integrity ' ], "Invalid integrity format for $ source " );
89
+ }
90
+ }
91
+
92
+ public function testManifestIsValidJson (): void
93
+ {
94
+ $ manifestPath = Dashboard::path ('/public/manifest.json ' );
95
+ $ json = file_get_contents ($ manifestPath );
96
+ $ this ->assertNotFalse ($ json , 'Manifest file cannot be read. ' );
97
+
98
+ $ decoded = json_decode ($ json , true );
99
+ $ this ->assertIsArray ($ decoded , 'Manifest JSON is not an array. ' );
100
+ $ this ->assertSame (JSON_ERROR_NONE , json_last_error (), 'Invalid JSON: ' . json_last_error_msg ());
101
+ }
37
102
}
0 commit comments