Skip to content

Commit 3bd5f9c

Browse files
committed
Added more tests
1 parent 7bba04a commit 3bd5f9c

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

tests/Unit/PublishedResourceTest.php

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010

1111
class PublishedResourceTest extends TestUnitCase
1212
{
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+
1319
/**
1420
* This test will check the size of the published files,
1521
* so as not to accidentally publish non-minified versions.
1622
*
1723
* Usually to solve this problem you only need to run the Laravel Mix:
18-
* `npm run production`
24+
* `npm run build`
1925
*
2026
* These are approximate values that can be changed.
2127
*/
@@ -26,12 +32,71 @@ public function testFilesAreMinified(): void
2632

2733
$this->assertLessThan($maxCssSize,
2834
filesize(Dashboard::path('/public/assets/app.css')),
29-
'File orchid.css more '.Formats::formatBytes($maxCssSize)
35+
'File orchid.css more ' . Formats::formatBytes($maxCssSize)
3036
);
3137

3238
$this->assertLessThan($maxJsSize,
3339
filesize(Dashboard::path('/public/assets/app.js')),
34-
'File orchid.js more '.Formats::formatBytes($maxJsSize)
40+
'File orchid.js more ' . Formats::formatBytes($maxJsSize)
3541
);
3642
}
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+
}
37102
}

0 commit comments

Comments
 (0)