44
55use Illuminate \Support \Facades \Artisan ;
66use Illuminate \Support \Facades \File ;
7+ use Illuminate \Support \Facades \Storage ;
78use Tests \TestCase ;
89
910
@@ -21,4 +22,90 @@ public function test_command_creates_contract_and_response()
2122 Artisan::call ('storage:link-custom ' );
2223 $ this ->assertTrue (true );
2324 }
25+
26+ public function test_deletes_logs_by_default ()
27+ {
28+ Storage::fake ();
29+
30+ // Create log files
31+ Storage::put ('logs/laravel.log ' , 'Dummy content ' );
32+ Storage::put ('logs/error.log ' , 'Error log ' );
33+
34+ // Run command
35+ $ this ->artisan ('files:delete-all ' )
36+ ->assertExitCode (0 );
37+
38+ // Ensure files are deleted
39+ $ this ->assertFalse (Storage::exists ('logs/laravel.log ' ));
40+ $ this ->assertFalse (Storage::exists ('logs/error.log ' ));
41+ }
42+ public function test_deletes_files_from_custom_path ()
43+ {
44+ Storage::fake ();
45+
46+ // Create dummy files in custom directory
47+ Storage::put ('custom/logs/app.log ' , 'Log file ' );
48+ Storage::put ('custom/logs/debug.log ' , 'Debugging ' );
49+
50+ // Run command with a custom path
51+ $ this ->artisan ('files:delete-all custom/logs ' )
52+ ->expectsOutput ('All files and folders deleted successfully! ' )
53+ ->assertExitCode (0 );
54+
55+ // Assert files are deleted
56+ $ this ->assertFalse (Storage::exists ('custom/logs/app.log ' ));
57+ $ this ->assertFalse (Storage::exists ('custom/logs/debug.log ' ));
58+ }
59+ public function test_handles_missing_files_gracefully ()
60+ {
61+ Storage::fake ();
62+
63+ // Run command when no logs exist
64+ $ this ->artisan ('files:delete-all ' )
65+ ->expectsOutput ('No files found to delete. ' )
66+ ->assertExitCode (0 );
67+ }
68+
69+ public function test_handles_permission_errors_gracefully ()
70+ {
71+ Storage::fake ();
72+
73+ // Create a log file and make it read-only
74+ $ filePath = storage_path ('logs/protected.log ' );
75+ file_put_contents ($ filePath , 'Protected log ' );
76+ chmod ($ filePath , 0444 ); // Read-only (no delete permission)
77+
78+ // Run command
79+ $ this ->artisan ('files:delete-all ' )
80+ ->expectsOutput ('Files that couldn \'t be deleted: ' );
81+
82+ // Ensure file still exists
83+ $ this ->assertFileExists ($ filePath );
84+
85+ // Reset permissions
86+ chmod ($ filePath , 0644 );
87+ }
88+
89+
90+ public function test_command_help_message ()
91+ {
92+ $ this ->artisan ('files:delete-all --help ' )
93+ ->expectsOutputToContain ('Delete all files and folders recursively, skipping undeletable ones and logging them ' );
94+ }
95+
96+ public function test_deletes_only_specific_file_extensions ()
97+ {
98+ Storage::fake ();
99+
100+ // Create multiple file types
101+ Storage::put ('logs/app.log ' , 'Log content ' );
102+ Storage::put ('logs/debug.txt ' , 'Debug content ' );
103+
104+ // Run command with `--ext=log`
105+ $ this ->artisan ('files:delete-all --ext=log ' );
106+
107+ // Ensure only `.log` is deleted
108+ $ this ->assertFalse (Storage::exists ('logs/app.log ' ));
109+ $ this ->assertTrue (Storage::exists ('logs/debug.txt ' ));
110+ }
24111}
0 commit comments