-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModuleTestCommand.php
More file actions
323 lines (277 loc) · 9.71 KB
/
ModuleTestCommand.php
File metadata and controls
323 lines (277 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
declare(strict_types=1);
namespace PanicDevs\Modules\Console;
use Illuminate\Console\Command;
use PanicDevs\Modules\ManifestBuilder;
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\info;
use function Laravel\Prompts\warning;
use function Laravel\Prompts\table;
use function Laravel\Prompts\spin;
/**
* ModuleTestCommand
*
* Console command for testing module system performance and functionality.
* Provides cache performance testing, module discovery benchmarks,
* and detailed module information display.
*
* Usage:
* - php artisan modules:test # Basic module discovery test
* - php artisan modules:test --cache # Include cache performance testing
* - php artisan modules:test --details # Show detailed module information
*
* @package PanicDevs\Modules\Console
* @author PanicDevs
* @since 1.0.0
*/
class ModuleTestCommand extends Command
{
protected $signature = 'modules:test {--details : Show detailed module information} {--cache : Test cache performance} {--interactive : Interactive test mode}';
protected $description = 'Test module loading performance and functionality';
/**
* Execute the console command.
*
* Runs various tests and benchmarks on the module system based on provided options.
*/
public function handle(): void
{
if ($this->option('interactive'))
{
$this->handleInteractiveMode();
return;
}
info('🧪 Module System Performance Test');
$this->line('<fg=gray>═══════════════════════════════════════════════════════════</fg=gray>');
$this->newLine();
if ($this->option('cache'))
{
$this->testCachePerformance();
}
$this->testModuleDiscovery();
if ($this->option('details'))
{
$this->showModuleDetails();
}
$this->newLine();
info('🎉 All tests completed!');
}
/**
* Handle interactive test mode with Laravel Prompts.
*/
protected function handleInteractiveMode(): void
{
info('🧪 Welcome to Interactive Module Testing!');
$testOptions = multiselect(
label: 'Which tests would you like to run?',
options: [
'discovery' => '🔍 Module Discovery Test',
'cache' => '⚡ Cache Performance Test',
'details' => '📋 Detailed Module Information',
'validation' => '✅ Module Validation Test'
],
default: ['discovery'],
hint: 'Select one or more tests to run'
);
if (empty($testOptions))
{
warning('No tests selected. Exiting.');
return;
}
if (in_array('discovery', $testOptions))
{
$this->testModuleDiscovery();
}
if (in_array('cache', $testOptions))
{
$this->testCachePerformance();
}
if (in_array('validation', $testOptions))
{
$this->testModuleValidation();
}
if (in_array('details', $testOptions))
{
$this->showModuleDetails();
}
info('🎉 All selected tests completed!');
}
/**
* Test module validation by checking configurations and files.
*/
protected function testModuleValidation(): void
{
info('✅ Testing Module Validation');
$modules = spin(
callback: fn() => (new ManifestBuilder())->build(),
message: '🔍 Validating modules...'
);
$issues = [];
foreach ($modules as $name => $module)
{
// Check if module.json exists
$moduleJsonPath = $module['path'].'/module.json';
if (!file_exists($moduleJsonPath))
{
$issues[] = "❌ {$name}: Missing module.json";
continue;
}
// Check required files
if (!empty($module['files']))
{
foreach ($module['files'] as $file)
{
$filePath = $module['path'].'/'.$file;
if (!file_exists($filePath))
{
$issues[] = "⚠️ {$name}: Missing required file '{$file}'";
}
}
}
// Check providers
if (!empty($module['providers']))
{
foreach ($module['providers'] as $provider)
{
if (!class_exists($provider))
{
$issues[] = "⚠️ {$name}: Provider class '{$provider}' not found";
}
}
}
// Check dependencies
if (!empty($module['depends_on']))
{
foreach ($module['depends_on'] as $dependency)
{
if (!isset($modules[$dependency]))
{
$issues[] = "❌ {$name}: Missing dependency '{$dependency}'";
} elseif (!$modules[$dependency]['enabled'])
{
$issues[] = "⚠️ {$name}: Dependency '{$dependency}' is disabled";
}
}
}
}
if (empty($issues))
{
info('🎉 All modules passed validation!');
} else
{
warning('Found '.count($issues).' validation issues:');
foreach ($issues as $issue)
{
$this->line(" {$issue}");
}
}
$this->newLine();
}
/**
* Test cache performance by comparing cold vs warm start times.
*
* Clears cache, measures cold start time, then measures warm start time
* to demonstrate caching effectiveness.
*/
protected function testCachePerformance(): void
{
$this->info('📊 <fg=yellow>Testing Cache Performance</fg=yellow>');
$this->newLine();
$builder = new ManifestBuilder();
$builder->clear();
$this->line(' 🗑️ Cache cleared');
// Cold start
$start = microtime(true);
$modules = $builder->build();
$coldTime = (microtime(true) - $start) * 1000;
$this->line(" 🥶 Cold start: <fg=magenta>{$coldTime}ms</fg=magenta>");
// Warm start
$start = microtime(true);
$modules = $builder->build();
$warmTime = (microtime(true) - $start) * 1000;
$this->line(" 🔥 Warm start: <fg=green>{$warmTime}ms</fg=green>");
$improvement = $coldTime > 0 ? round((($coldTime - $warmTime) / $coldTime) * 100, 1) : 0;
$this->line(" ⚡️ Improvement: <fg=cyan>{$improvement}%</fg=cyan>");
$this->newLine();
}
/**
* Test module discovery functionality and performance.
*
* Measures discovery time and displays module statistics organized by type.
*/
protected function testModuleDiscovery(): void
{
$this->info('🔍 <fg=yellow>Testing Module Discovery</fg=yellow>');
$this->newLine();
$start = microtime(true);
$manifestBuilder = new ManifestBuilder();
$modules = $manifestBuilder->build();
$end = microtime(true);
$loadTime = ($end - $start) * 1000;
if ($loadTime > 1)
{
$this->line(" ⏱️ Load time: <fg=red>{$loadTime}ms</fg=red> (⚠️ Over 1ms!)");
} else
{
$this->line(" ⏱️ Load time: <fg=green>{$loadTime}ms</fg=green> (✅ Under 1ms)");
}
$counts = [];
$totalEnabled = 0;
foreach ($modules as $name => $module)
{
$type = $module['type'] ?? 'unknown';
$counts[$type] = ($counts[$type] ?? 0) + 1;
if ($module['enabled'])
{
$totalEnabled++;
}
}
$this->line(" 📦 Total: <fg=cyan>".count($modules)."</fg=cyan> | ✅ Enabled: <fg=green>{$totalEnabled}</fg=green>");
foreach ($counts as $type => $count)
{
$this->line(" 📁 {$type}: <fg=yellow>{$count}</fg=yellow>");
}
$this->newLine();
}
/**
* Display detailed information about all discovered modules.
*
* Shows module name, status, type, and priority for each module using table format.
*/
protected function showModuleDetails(): void
{
info('📋 Module Details');
$modules = spin(
callback: fn() => (new ManifestBuilder())->build(),
message: '🔍 Loading module details...'
);
$rows = [];
foreach ($modules as $name => $module)
{
$status = $module['enabled'] ? '✅ Enabled' : '❌ Disabled';
$priority = (string)($module['priority'] ?? 0);
$type = ucfirst($module['type'] ?? 'unknown');
$version = $module['version'] ?? '0.0.0';
$icon = $this->getModuleTypeIcon($module['type'] ?? 'unknown');
$rows[] = [
"{$icon} {$name}",
$type,
$status,
$priority,
$version
];
}
table(
headers: ['Module', 'Type', 'Status', 'Priority', 'Version'],
rows: $rows
);
$this->newLine();
}
/**
* Get the icon for a module type from configuration.
*/
protected function getModuleTypeIcon(string $type): string
{
$paths = config('modules.paths', []);
return $paths[$type]['icon'] ?? '📦';
}
}