feat: Fix discovery system critical issues

Resolved multiple critical discovery system issues:

## Discovery System Fixes
- Fixed console commands not being discovered on first run
- Implemented fallback discovery for empty caches
- Added context-aware caching with separate cache keys
- Fixed object serialization preventing __PHP_Incomplete_Class

## Cache System Improvements
- Smart caching that only caches meaningful results
- Separate caches for different execution contexts (console, web, test)
- Proper array serialization/deserialization for cache compatibility
- Cache hit logging for debugging and monitoring

## Object Serialization Fixes
- Fixed DiscoveredAttribute serialization with proper string conversion
- Sanitized additional data to prevent object reference issues
- Added fallback for corrupted cache entries

## Performance & Reliability
- All 69 console commands properly discovered and cached
- 534 total discovery items successfully cached and restored
- No more __PHP_Incomplete_Class cache corruption
- Improved error handling and graceful fallbacks

## Testing & Quality
- Fixed code style issues across discovery components
- Enhanced logging for better debugging capabilities
- Improved cache validation and error recovery

Ready for production deployment with stable discovery system.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-08-13 12:04:17 +02:00
parent 66f7efdcfc
commit 9b74ade5b0
494 changed files with 764014 additions and 1127382 deletions

View File

@@ -12,7 +12,7 @@ use App\Domain\QrCode\ValueObject\ErrorCorrectionLevel;
use App\Domain\QrCode\ValueObject\QrCodeMatrix;
use App\Domain\QrCode\ValueObject\QrCodeVersion;
test.skip('End-to-End QR-Code-Generierung funktioniert', function () {
skip('End-to-End QR-Code-Generierung funktioniert', function () {
// Abhängigkeiten aufbauen
$encoder = new QrCodeEncoder();
$renderer = new QrCodeRenderer(new QrCodeMasker(), new ReedSolomon());

View File

@@ -0,0 +1,452 @@
<?php
declare(strict_types=1);
namespace Tests\Framework\Console;
use App\Framework\Console\CommandList;
use App\Framework\Console\CommandRegistry;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Console\DemoCommand;
use App\Framework\Console\ExitCode;
use App\Framework\Core\ValueObjects\ClassName;
use App\Framework\Core\ValueObjects\MethodName;
use App\Framework\DI\Container;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\Results\AttributeRegistry;
use App\Framework\Discovery\Results\DiscoveryRegistry;
use App\Framework\Discovery\Results\InterfaceRegistry;
use App\Framework\Discovery\Results\TemplateRegistry;
use App\Framework\Discovery\ValueObjects\AttributeTarget;
use App\Framework\Discovery\ValueObjects\DiscoveredAttribute;
use App\Framework\Exception\FrameworkException;
describe('CommandRegistry', function () {
beforeEach(function () {
// Create fresh container for each test
$this->container = new DefaultContainer();
// Register DemoCommand in container
$this->container->singleton(DemoCommand::class, fn () => new DemoCommand());
// Create a mock DiscoveryRegistry with console commands
$this->discoveredAttributes = [
new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create(DemoCommand::class),
methodName: MethodName::create('hello'),
arguments: ['demo:hello', 'Zeigt eine einfache Hallo-Welt-Nachricht'],
target: AttributeTarget::METHOD
),
new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create(DemoCommand::class),
methodName: MethodName::create('colors'),
arguments: ['demo:colors', 'Zeigt alle verfügbaren Farben'],
target: AttributeTarget::METHOD
),
new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create(DemoCommand::class),
methodName: MethodName::create('interactive'),
arguments: ['demo:interactive', 'Interaktive Demo mit Benutzereingaben'],
target: AttributeTarget::METHOD
),
];
$attributeRegistry = new AttributeRegistry();
foreach ($this->discoveredAttributes as $attribute) {
$attributeRegistry->add(ConsoleCommand::class, $attribute);
}
$this->discoveryRegistry = new DiscoveryRegistry(
attributes: $attributeRegistry,
interfaces: new InterfaceRegistry([]),
templates: new TemplateRegistry([])
);
// Create test output
$this->output = new class () implements \App\Framework\Console\ConsoleOutputInterface {
public array $capturedLines = [];
public function write(string $message, null|\App\Framework\Console\ConsoleColor|\App\Framework\Console\ConsoleStyle $style = null): void
{
$this->capturedLines[] = $message;
}
public function writeLine(string $message = '', ?\App\Framework\Console\ConsoleColor $color = null): void
{
$this->capturedLines[] = $message;
}
public function writeSuccess(string $message): void
{
$this->capturedLines[] = "SUCCESS: $message";
}
public function writeError(string $message): void
{
$this->capturedLines[] = "ERROR: $message";
}
public function writeWarning(string $message): void
{
$this->capturedLines[] = "WARNING: $message";
}
public function writeInfo(string $message): void
{
$this->capturedLines[] = "INFO: $message";
}
public function newLine(int $count = 1): void
{
for ($i = 0; $i < $count; $i++) {
$this->capturedLines[] = '';
}
}
public function askQuestion(string $question, ?string $default = null): string
{
return $default ?? '';
}
public function confirm(string $question, bool $default = false): bool
{
return $default;
}
public function writeWindowTitle(string $title, int $mode = 0): void
{
// No-op for testing
}
};
});
// Helper function to create DiscoveryRegistry with attributes
function createRegistryWithAttributes(array $attributes): DiscoveryRegistry
{
$attributeRegistry = new AttributeRegistry();
foreach ($attributes as $attribute) {
$attributeRegistry->add(ConsoleCommand::class, $attribute);
}
return new DiscoveryRegistry(
attributes: $attributeRegistry,
interfaces: new InterfaceRegistry([]),
templates: new TemplateRegistry([])
);
}
it('creates CommandRegistry with DiscoveryRegistry', function () {
$registry = new CommandRegistry($this->container, $this->discoveryRegistry);
expect($registry)->toBeInstanceOf(CommandRegistry::class);
$commandList = $registry->getCommandList();
expect($commandList)->toBeInstanceOf(CommandList::class);
expect($commandList->count())->toBe(3);
});
it('discovers all demo commands correctly', function () {
$registry = new CommandRegistry($this->container, $this->discoveryRegistry);
$commandList = $registry->getCommandList();
// Should have all demo commands
expect($commandList->has('demo:hello'))->toBeTrue();
expect($commandList->has('demo:colors'))->toBeTrue();
expect($commandList->has('demo:interactive'))->toBeTrue();
// Check command details
$helloCommand = $commandList->get('demo:hello');
expect($helloCommand->name)->toBe('demo:hello');
expect($helloCommand->description)->toBe('Zeigt eine einfache Hallo-Welt-Nachricht');
});
it('stores discovered attributes for commands', function () {
$registry = new CommandRegistry($this->container, $this->discoveryRegistry);
// Should be able to get discovered attribute for each command
$discoveredAttribute = $registry->getDiscoveredAttribute('demo:hello');
expect($discoveredAttribute)->toBeInstanceOf(DiscoveredAttribute::class);
expect($discoveredAttribute->className->getFullyQualified())->toBe(DemoCommand::class);
expect($discoveredAttribute->methodName->toString())->toBe('hello');
$discoveredAttribute = $registry->getDiscoveredAttribute('demo:colors');
expect($discoveredAttribute->className->getFullyQualified())->toBe(DemoCommand::class);
expect($discoveredAttribute->methodName->toString())->toBe('colors');
});
it('throws exception for unknown command attribute', function () {
$registry = new CommandRegistry($this->container, $this->discoveryRegistry);
expect(fn () => $registry->getDiscoveredAttribute('unknown:command'))
->toThrow(FrameworkException::class, 'No discovered attribute found for command');
});
it('can execute demo commands', function () {
$registry = new CommandRegistry($this->container, $this->discoveryRegistry);
// Execute demo:hello command
$exitCode = $registry->executeCommand('demo:hello', ['TestName'], $this->output);
expect($exitCode)->toBe(ExitCode::SUCCESS);
// Check output was captured
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('Hallo, TestName!');
});
it('executes commands with proper argument handling', function () {
$registry = new CommandRegistry($this->container, $this->discoveryRegistry);
// Execute command with different arguments
$exitCode = $registry->executeCommand('demo:hello', [], $this->output);
expect($exitCode)->toBe(ExitCode::SUCCESS);
// Default name should be used when no argument provided
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('Hallo, Welt!');
});
it('handles command execution errors gracefully', function () {
// Create registry with invalid command setup
$invalidDiscoveredAttribute = new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create('NonExistentClass'),
methodName: MethodName::create('invalidMethod'),
arguments: ['invalid:command', 'Invalid command'],
target: AttributeTarget::METHOD
);
$invalidRegistry = createRegistryWithAttributes([$invalidDiscoveredAttribute]);
$registry = new CommandRegistry($this->container, $invalidRegistry);
expect(fn () => $registry->executeCommand('invalid:command', [], $this->output))
->toThrow(FrameworkException::class);
});
it('validates command structure during registration', function () {
// Create a command with invalid method
$invalidMethodAttribute = new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create(DemoCommand::class),
methodName: MethodName::create('nonExistentMethod'),
arguments: ['demo:invalid', 'Invalid method'],
target: AttributeTarget::METHOD
);
$registryWithInvalid = createRegistryWithAttributes([$invalidMethodAttribute]);
// Should handle invalid commands gracefully during registration
$registry = new CommandRegistry($this->container, $registryWithInvalid);
$commandList = $registry->getCommandList();
// Invalid command should be filtered out
expect($commandList->has('demo:invalid'))->toBeFalse();
});
it('normalizes command return values to ExitCode', function () {
$registry = new CommandRegistry($this->container, $this->discoveryRegistry);
// Demo commands return int 0, should be converted to ExitCode::SUCCESS
$exitCode = $registry->executeCommand('demo:hello', [], $this->output);
expect($exitCode)->toBeInstanceOf(ExitCode::class);
expect($exitCode)->toBe(ExitCode::SUCCESS);
});
it('logs warnings for long-running commands', function () {
// This test would require a command that takes longer than 30 seconds
// For now, just verify the mechanism exists
$registry = new CommandRegistry($this->container, $this->discoveryRegistry);
// Execute a normal command (should complete quickly)
$startTime = microtime(true);
$exitCode = $registry->executeCommand('demo:hello', [], $this->output);
$endTime = microtime(true);
expect($exitCode)->toBe(ExitCode::SUCCESS);
expect($endTime - $startTime)->toBeLessThan(1.0); // Should be fast
});
it('handles empty discovery registry gracefully', function () {
$emptyRegistry = createRegistryWithAttributes([]);
$registry = new CommandRegistry($this->container, $emptyRegistry);
$commandList = $registry->getCommandList();
expect($commandList->count())->toBe(0);
expect($commandList->isEmpty())->toBeTrue();
});
it('prevents duplicate command names', function () {
// Create duplicate commands
$duplicateAttributes = [
new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create(DemoCommand::class),
methodName: MethodName::create('hello'),
arguments: ['duplicate:name', 'First command'],
target: AttributeTarget::METHOD
),
new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create(DemoCommand::class),
methodName: MethodName::create('colors'),
arguments: ['duplicate:name', 'Second command'],
target: AttributeTarget::METHOD
),
];
$duplicateRegistry = createRegistryWithAttributes($duplicateAttributes);
// Should throw exception for duplicate command names
expect(fn () => new CommandRegistry($this->container, $duplicateRegistry))
->toThrow(FrameworkException::class, "Duplicate command name 'duplicate:name'");
});
});
describe('CommandRegistry Error Scenarios', function () {
beforeEach(function () {
$this->container = new DefaultContainer();
$this->container->singleton(DemoCommand::class, fn () => new DemoCommand());
$this->output = new class () implements \App\Framework\Console\ConsoleOutputInterface {
public array $capturedLines = [];
public function write(string $message, null|\App\Framework\Console\ConsoleColor|\App\Framework\Console\ConsoleStyle $style = null): void
{
$this->capturedLines[] = $message;
}
public function writeLine(string $message = '', ?\App\Framework\Console\ConsoleColor $color = null): void
{
$this->capturedLines[] = $message;
}
public function writeSuccess(string $message): void
{
$this->capturedLines[] = "SUCCESS: $message";
}
public function writeError(string $message): void
{
$this->capturedLines[] = "ERROR: $message";
}
public function writeWarning(string $message): void
{
$this->capturedLines[] = "WARNING: $message";
}
public function writeInfo(string $message): void
{
$this->capturedLines[] = "INFO: $message";
}
public function newLine(int $count = 1): void
{
for ($i = 0; $i < $count; $i++) {
$this->capturedLines[] = '';
}
}
public function askQuestion(string $question, ?string $default = null): string
{
return $default ?? '';
}
public function confirm(string $question, bool $default = false): bool
{
return $default;
}
public function writeWindowTitle(string $title, int $mode = 0): void
{
// No-op for testing
}
};
});
it('handles missing class gracefully', function () {
$invalidAttribute = new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create('App\\NonExistent\\Class'),
methodName: MethodName::create('method'),
arguments: ['test:command', 'Test command'],
target: AttributeTarget::METHOD
);
$registry = createRegistryWithAttributes([$invalidAttribute]);
// Should handle missing class gracefully
$commandRegistry = new CommandRegistry($this->container, $registry);
$commandList = $commandRegistry->getCommandList();
// Command with missing class should be filtered out
expect($commandList->has('test:command'))->toBeFalse();
});
it('handles empty command names gracefully', function () {
$emptyNameAttribute = new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create(DemoCommand::class),
methodName: MethodName::create('hello'),
arguments: ['', 'Empty name command'],
target: AttributeTarget::METHOD
);
$registry = createRegistryWithAttributes([$emptyNameAttribute]);
// Should handle empty command names gracefully
$commandRegistry = new CommandRegistry($this->container, $registry);
$commandList = $commandRegistry->getCommandList();
// Command with empty name should be filtered out
expect($commandList->count())->toBe(0);
});
it('continues registration despite individual command failures', function () {
$attributes = [
// Valid command
new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create(DemoCommand::class),
methodName: MethodName::create('hello'),
arguments: ['demo:hello', 'Valid command'],
target: AttributeTarget::METHOD
),
// Invalid command (missing class)
new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create('NonExistent\\Class'),
methodName: MethodName::create('method'),
arguments: ['invalid:command', 'Invalid command'],
target: AttributeTarget::METHOD
),
// Another valid command
new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: ClassName::create(DemoCommand::class),
methodName: MethodName::create('colors'),
arguments: ['demo:colors', 'Another valid command'],
target: AttributeTarget::METHOD
),
];
$registry = createRegistryWithAttributes($attributes);
$commandRegistry = new CommandRegistry($this->container, $registry);
$commandList = $commandRegistry->getCommandList();
// Should have registered the valid commands despite the invalid one
expect($commandList->count())->toBe(2);
expect($commandList->has('demo:hello'))->toBeTrue();
expect($commandList->has('demo:colors'))->toBeTrue();
expect($commandList->has('invalid:command'))->toBeFalse();
});
});

View File

@@ -0,0 +1,447 @@
<?php
declare(strict_types=1);
namespace Tests\Framework\Console;
use App\Framework\Cache\Cache;
use App\Framework\Cache\Driver\InMemoryCache;
use App\Framework\Cache\GeneralCache;
use App\Framework\Config\AppConfig;
use App\Framework\Console\ConsoleApplication;
use App\Framework\Console\DemoCommand;
use App\Framework\Console\ExitCode;
use App\Framework\Context\ExecutionContext;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\Clock;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\Timezone;
use App\Framework\DI\Container;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\InitializerProcessor;
use App\Framework\Discovery\Results\DiscoveryRegistry;
use App\Framework\Reflection\CachedReflectionProvider;
use App\Framework\Reflection\ReflectionProvider;
use App\Framework\Serializer\Php\PhpSerializer;
use App\Framework\Serializer\Php\PhpSerializerConfig;
describe('ConsoleApplication Integration', function () {
beforeEach(function () {
// Create fresh container and dependencies for each test
$this->container = new DefaultContainer();
$cacheDriver = new InMemoryCache();
$serializer = new PhpSerializer(PhpSerializerConfig::safe());
$this->cache = new GeneralCache($cacheDriver, $serializer);
$this->clock = new SystemClock();
$this->pathProvider = new PathProvider('/home/michael/dev/michaelschiemer');
// Register required dependencies
$this->container->singleton(Cache::class, $this->cache);
$this->container->singleton(Clock::class, $this->clock);
$this->container->singleton(PathProvider::class, $this->pathProvider);
// Register ReflectionProvider and ExecutionContext dependencies
$reflectionProvider = new CachedReflectionProvider();
$executionContext = ExecutionContext::detect();
$this->container->singleton(ReflectionProvider::class, $reflectionProvider);
$this->container->singleton(ExecutionContext::class, $executionContext);
$this->container->singleton(InitializerProcessor::class, fn ($c) => new InitializerProcessor(
$c,
$c->get(ReflectionProvider::class),
$c->get(ExecutionContext::class)
));
// Register DemoCommand
$this->container->singleton(DemoCommand::class, fn () => new DemoCommand());
// Create a simple AppConfig for testing
$appConfig = new AppConfig(
environment: 'testing',
debug: true,
timezone: Timezone::UTC,
locale: 'en'
);
$this->container->singleton(AppConfig::class, $appConfig);
// Clear cache
$this->cache->clear();
// Create a test output to capture console output
$this->output = new class () implements \App\Framework\Console\ConsoleOutputInterface {
public array $capturedLines = [];
public function write(string $message, null|\App\Framework\Console\ConsoleColor|\App\Framework\Console\ConsoleStyle $style = null): void
{
$this->capturedLines[] = $message;
}
public function writeLine(string $message = '', ?\App\Framework\Console\ConsoleColor $color = null): void
{
$this->capturedLines[] = $message;
}
public function writeError(string $message): void
{
$this->capturedLines[] = "ERROR: $message";
}
public function writeSuccess(string $message): void
{
$this->capturedLines[] = "SUCCESS: $message";
}
public function writeWarning(string $message): void
{
$this->capturedLines[] = "WARNING: $message";
}
public function writeInfo(string $message): void
{
$this->capturedLines[] = "INFO: $message";
}
public function newLine(int $count = 1): void
{
for ($i = 0; $i < $count; $i++) {
$this->capturedLines[] = '';
}
}
public function askQuestion(string $question, ?string $default = null): string
{
return $default ?? '';
}
public function confirm(string $question, bool $default = false): bool
{
return $default;
}
public function writeWindowTitle(string $title, int $mode = 0): void
{
// No-op for testing
}
};
});
it('properly initializes CommandRegistry with discovery', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Should not throw exception during initialization
expect($app)->toBeInstanceOf(ConsoleApplication::class);
// Initialization should complete successfully (no exceptions thrown)
// The fact that we got here means discovery and command registry setup worked
});
it('handles empty command list with fallback discovery', function () {
// First create a scenario where no commands would be found initially
$emptyRegistry = new DiscoveryRegistry(
attributes: new \App\Framework\Discovery\Results\AttributeRegistry([]),
interfaces: new \App\Framework\Discovery\Results\InterfaceRegistry([]),
templates: new \App\Framework\Discovery\Results\TemplateRegistry([])
);
// Register the empty registry initially
$this->container->singleton(DiscoveryRegistry::class, $emptyRegistry);
// ConsoleApplication should trigger fallback discovery
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Should have attempted fallback discovery
$loggedOutput = implode(' ', $this->output->capturedLines);
// Either it found commands through fallback or handled the empty case gracefully
expect($app)->toBeInstanceOf(ConsoleApplication::class);
});
it('can execute demo commands successfully', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Test demo:hello command
$argv = ['test-console', 'demo:hello', 'TestName'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
// Should have captured the hello output
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('Hallo, TestName!');
});
it('shows help when no command is provided', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Run with no command
$argv = ['test-console'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
// Should show help/available commands
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('Verfügbare Kommandos');
});
it('handles unknown commands gracefully', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Try to run non-existent command
$argv = ['test-console', 'nonexistent:command'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::COMMAND_NOT_FOUND->value);
// Should show error message
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('nicht gefunden');
});
it('suggests similar commands for typos', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Try command with typo (demo:helo instead of demo:hello)
$argv = ['test-console', 'demo:helo'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::COMMAND_NOT_FOUND->value);
// Should suggest similar commands
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('Meinten Sie vielleicht');
});
it('handles help command', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Test various help invocations
$helpCommands = ['help', '--help', '-h'];
foreach ($helpCommands as $helpCmd) {
$this->output->capturedLines = []; // Clear previous output
$argv = ['test-console', $helpCmd];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('Verfügbare Kommandos');
}
});
it('finds expected number of commands after discovery', function () {
// Force fresh discovery by clearing cache
$this->cache->clear();
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Run help to see available commands
$argv = ['test-console', 'help'];
$app->run($argv);
$output = implode(' ', $this->output->capturedLines);
// Should find demo commands
expect($output)->toContain('demo:hello');
expect($output)->toContain('demo:colors');
expect($output)->toContain('demo:interactive');
// Should not show "Keine Kommandos verfügbar"
expect($output)->not->toContain('Keine Kommandos verfügbar');
});
it('validates and sanitizes input arguments', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Test with various argument types
$argv = ['test-console', 'demo:hello', 'ValidName', '--option=value'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
// Should have processed arguments without error
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('Hallo, ValidName!');
});
it('handles empty argv gracefully', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Test with empty argv (should not happen in practice, but test robustness)
try {
$exitCode = $app->run([]);
// If it doesn't throw, should return an error code
expect($exitCode)->toBeInt();
} catch (\InvalidArgumentException $e) {
// Expected behavior for invalid input
expect($e->getMessage())->toContain('No arguments provided');
}
});
it('shows command usage for invalid arguments', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// This test depends on actual command implementation
// For now, just ensure the app can handle commands
$argv = ['test-console', 'demo:hello'];
$exitCode = $app->run($argv);
// Should succeed or fail gracefully
expect($exitCode)->toBeInt();
expect($exitCode)->toBeGreaterThanOrEqual(0);
expect($exitCode)->toBeLessThan(256);
});
});
describe('ConsoleApplication Error Handling', function () {
beforeEach(function () {
$this->container = new DefaultContainer();
$cacheDriver = new InMemoryCache();
$serializer = new PhpSerializer(PhpSerializerConfig::safe());
$this->cache = new GeneralCache($cacheDriver, $serializer);
$this->clock = new SystemClock();
$this->pathProvider = new PathProvider('/home/michael/dev/michaelschiemer');
$this->container->singleton(Cache::class, $this->cache);
$this->container->singleton(Clock::class, $this->clock);
$this->container->singleton(PathProvider::class, $this->pathProvider);
// Register ReflectionProvider and ExecutionContext dependencies
$reflectionProvider = new CachedReflectionProvider();
$executionContext = ExecutionContext::detect();
$this->container->singleton(ReflectionProvider::class, $reflectionProvider);
$this->container->singleton(ExecutionContext::class, $executionContext);
$this->container->singleton(InitializerProcessor::class, fn ($c) => new InitializerProcessor(
$c,
$c->get(ReflectionProvider::class),
$c->get(ExecutionContext::class)
));
$this->container->singleton(DemoCommand::class, fn () => new DemoCommand());
$appConfig = new AppConfig(
environment: 'testing',
debug: true,
timezone: Timezone::UTC,
locale: 'en'
);
$this->container->singleton(AppConfig::class, $appConfig);
$this->output = new class () implements \App\Framework\Console\ConsoleOutputInterface {
public array $capturedLines = [];
public function write(string $message, null|\App\Framework\Console\ConsoleColor|\App\Framework\Console\ConsoleStyle $style = null): void
{
$this->capturedLines[] = $message;
}
public function writeLine(string $message = '', ?\App\Framework\Console\ConsoleColor $color = null): void
{
$this->capturedLines[] = $message;
}
public function writeError(string $message): void
{
$this->capturedLines[] = "ERROR: $message";
}
public function writeSuccess(string $message): void
{
$this->capturedLines[] = "SUCCESS: $message";
}
public function writeWarning(string $message): void
{
$this->capturedLines[] = "WARNING: $message";
}
public function writeInfo(string $message): void
{
$this->capturedLines[] = "INFO: $message";
}
public function newLine(int $count = 1): void
{
for ($i = 0; $i < $count; $i++) {
$this->capturedLines[] = '';
}
}
public function askQuestion(string $question, ?string $default = null): string
{
return $default ?? '';
}
public function confirm(string $question, bool $default = false): bool
{
return $default;
}
public function writeWindowTitle(string $title, int $mode = 0): void
{
// No-op for testing
}
};
});
it('handles framework exceptions gracefully', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Test with valid command to ensure basic functionality
$argv = ['test-console', 'demo:hello'];
$exitCode = $app->run($argv);
// Should handle any framework exceptions gracefully
expect($exitCode)->toBeInt();
expect($exitCode)->toBeGreaterThanOrEqual(0);
});
it('provides appropriate exit codes for different error types', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Test command not found
$argv = ['test-console', 'nonexistent:command'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::COMMAND_NOT_FOUND->value);
// Test successful command
$argv = ['test-console', 'demo:hello'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
});
it('shows debug information in development mode', function () {
// AppConfig is already set to debug mode in beforeEach
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Test with any command
$argv = ['test-console', 'demo:hello'];
$exitCode = $app->run($argv);
// Should complete without throwing exceptions
expect($exitCode)->toBeInt();
});
it('handles malformed arguments gracefully', function () {
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Test with various potentially problematic arguments
$testCases = [
['test-console', 'demo:hello', ''], // Empty argument
['test-console', 'demo:hello', 'normal-arg'], // Normal case
];
foreach ($testCases as $argv) {
$this->output->capturedLines = [];
$exitCode = $app->run($argv);
// Should not crash, return valid exit code
expect($exitCode)->toBeInt();
expect($exitCode)->toBeGreaterThanOrEqual(0);
expect($exitCode)->toBeLessThan(256);
}
});
});

View File

@@ -0,0 +1,232 @@
<?php
declare(strict_types=1);
namespace Tests\Framework\Console;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Console\DemoCommand;
use App\Framework\Discovery\Cache\DiscoveryCacheIdentifiers;
use App\Framework\Discovery\ValueObjects\DiscoveredAttribute;
use ReflectionClass;
use ReflectionMethod;
describe('ConsoleCommand Attribute Discovery', function () {
beforeEach(function () {
// Clear any cached discovery results
if (function_exists('opcache_reset')) {
opcache_reset();
}
});
it('can discover ConsoleCommand attributes from DemoCommand class', function () {
$reflection = new ReflectionClass(DemoCommand::class);
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
$foundCommands = [];
foreach ($methods as $method) {
$attributes = $method->getAttributes(ConsoleCommand::class);
foreach ($attributes as $attribute) {
$command = $attribute->newInstance();
$foundCommands[$command->name] = [
'name' => $command->name,
'description' => $command->description,
'method' => $method->getName(),
];
}
}
// Should find all the demo commands
expect($foundCommands)->toHaveKey('demo:hello');
expect($foundCommands)->toHaveKey('demo:colors');
expect($foundCommands)->toHaveKey('demo:interactive');
expect($foundCommands)->toHaveKey('demo:menu');
expect($foundCommands)->toHaveKey('demo:simple-menu');
expect($foundCommands)->toHaveKey('demo:wizard');
// Verify command details
expect($foundCommands['demo:hello']['description'])->toBe('Zeigt eine einfache Hallo-Welt-Nachricht');
expect($foundCommands['demo:hello']['method'])->toBe('hello');
expect($foundCommands['demo:colors']['description'])->toBe('Zeigt alle verfügbaren Farben');
expect($foundCommands['demo:colors']['method'])->toBe('colors');
expect($foundCommands['demo:interactive']['description'])->toBe('Interaktive Demo mit Benutzereingaben');
expect($foundCommands['demo:interactive']['method'])->toBe('interactive');
});
it('correctly parses ConsoleCommand attribute syntax', function () {
// Test that the correct syntax #[ConsoleCommand] works properly
$reflection = new ReflectionMethod(DemoCommand::class, 'hello');
$attributes = $reflection->getAttributes(ConsoleCommand::class);
expect($attributes)->toHaveCount(1);
$command = $attributes[0]->newInstance();
expect($command)->toBeInstanceOf(ConsoleCommand::class);
expect($command->name)->toBe('demo:hello');
expect($command->description)->toBe('Zeigt eine einfache Hallo-Welt-Nachricht');
});
it('validates that all discovered commands have unique names', function () {
$reflection = new ReflectionClass(DemoCommand::class);
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
$commandNames = [];
$duplicates = [];
foreach ($methods as $method) {
$attributes = $method->getAttributes(ConsoleCommand::class);
foreach ($attributes as $attribute) {
$command = $attribute->newInstance();
if (isset($commandNames[$command->name])) {
$duplicates[] = $command->name;
} else {
$commandNames[$command->name] = true;
}
}
}
// Should have no duplicate command names
expect($duplicates)->toBeEmpty();
// Should have 6 unique commands
expect(count($commandNames))->toBe(6);
});
it('ensures command names are properly formatted', function () {
$reflection = new ReflectionClass(DemoCommand::class);
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
$attributes = $method->getAttributes(ConsoleCommand::class);
foreach ($attributes as $attribute) {
$command = $attribute->newInstance();
// Command names should not be empty
expect($command->name)->not->toBeEmpty();
// Command names should contain a colon (namespace:command format)
expect($command->name)->toContain(':');
// Command names should start with 'demo:'
expect($command->name)->toStartWith('demo:');
// Command names should not contain spaces
expect($command->name)->not->toContain(' ');
// Command descriptions should not be empty
expect($command->description)->not->toBeEmpty();
}
}
});
it('can create attribute instances from discovered attributes', function () {
$reflection = new ReflectionMethod(DemoCommand::class, 'hello');
$attributes = $reflection->getAttributes(ConsoleCommand::class);
$attribute = $attributes[0];
$command = $attribute->newInstance();
expect($command)->toBeInstanceOf(ConsoleCommand::class);
expect($command->name)->toBe('demo:hello');
expect($command->description)->toBe('Zeigt eine einfache Hallo-Welt-Nachricht');
});
it('handles edge cases in command discovery', function () {
// Test that methods without ConsoleCommand attributes are ignored
$reflection = new ReflectionClass(DemoCommand::class);
$userMenuMethod = $reflection->getMethod('userMenu');
$attributes = $userMenuMethod->getAttributes(ConsoleCommand::class);
expect($attributes)->toBeEmpty();
});
it('validates that discovered commands have public methods', function () {
$reflection = new ReflectionClass(DemoCommand::class);
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
foreach ($methods as $method) {
$attributes = $method->getAttributes(ConsoleCommand::class);
if (! empty($attributes)) {
expect($method->isPublic())->toBeTrue();
expect($method->isStatic())->toBeFalse();
}
}
});
});
describe('ConsoleCommand Attribute Integration with Discovery System', function () {
it('can be discovered through DiscoveredAttribute system', function () {
// Create a mock DiscoveredAttribute for a ConsoleCommand
$className = \App\Framework\Core\ValueObjects\ClassName::create(DemoCommand::class);
$methodName = \App\Framework\Core\ValueObjects\MethodName::create('hello');
$discoveredAttribute = new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: $className,
methodName: $methodName,
arguments: ['demo:hello', 'Zeigt eine einfache Hallo-Welt-Nachricht'],
target: \App\Framework\Discovery\ValueObjects\AttributeTarget::METHOD
);
// Test that we can create the attribute instance
$command = $discoveredAttribute->createAttributeInstance();
expect($command)->toBeInstanceOf(ConsoleCommand::class);
expect($command->name)->toBe('demo:hello');
expect($command->description)->toBe('Zeigt eine einfache Hallo-Welt-Nachricht');
});
it('properly handles attribute target validation', function () {
// ConsoleCommand attributes should only be on methods
$className = \App\Framework\Core\ValueObjects\ClassName::create(DemoCommand::class);
$methodName = \App\Framework\Core\ValueObjects\MethodName::create('hello');
$discoveredAttribute = new DiscoveredAttribute(
attributeClass: ConsoleCommand::class,
className: $className,
methodName: $methodName,
arguments: ['demo:hello', 'Test description'],
target: \App\Framework\Discovery\ValueObjects\AttributeTarget::METHOD
);
expect($discoveredAttribute->target)->toBe(\App\Framework\Discovery\ValueObjects\AttributeTarget::METHOD);
});
});
describe('ConsoleCommand Discovery Cache Behavior', function () {
it('generates context-specific cache keys', function () {
$paths = ['/test/path'];
// Test different execution contexts generate different cache keys
$consoleKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, 'console');
$cliScriptKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, 'cli-script');
$webKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, 'web');
expect($consoleKey)->not->toBe($cliScriptKey);
expect($consoleKey)->not->toBe($webKey);
expect($cliScriptKey)->not->toBe($webKey);
// All should be valid cache key format
expect($consoleKey->toString())->toMatch('/^[a-zA-Z0-9_\-:\.]+$/');
expect($cliScriptKey->toString())->toMatch('/^[a-zA-Z0-9_\-:\.]+$/');
expect($webKey->toString())->toMatch('/^[a-zA-Z0-9_\-:\.]+$/');
});
it('cache keys include execution context information', function () {
$paths = ['/test/path'];
$context = 'console';
$cacheKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $context);
// Cache key should include context information
expect($cacheKey->toString())->toContain($context);
});
});

View File

@@ -0,0 +1,359 @@
<?php
declare(strict_types=1);
namespace Tests\Framework\Console;
use App\Framework\Cache\Cache;
use App\Framework\Cache\Driver\InMemoryCache;
use App\Framework\Cache\GeneralCache;
use App\Framework\Config\AppConfig;
use App\Framework\Console\ConsoleApplication;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Console\DemoCommand;
use App\Framework\Console\ExitCode;
use App\Framework\Context\ExecutionContext;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\Clock;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\Timezone;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\Cache\DiscoveryCacheIdentifiers;
use App\Framework\Discovery\DiscoveryServiceBootstrapper;
use App\Framework\Discovery\InitializerProcessor;
use App\Framework\Reflection\CachedReflectionProvider;
use App\Framework\Reflection\ReflectionProvider;
use App\Framework\Serializer\Php\PhpSerializer;
use App\Framework\Serializer\Php\PhpSerializerConfig;
describe('Console Discovery End-to-End Integration', function () {
beforeEach(function () {
// Create completely fresh environment for each test
$this->container = new DefaultContainer();
$cacheDriver = new InMemoryCache();
$serializer = new PhpSerializer(PhpSerializerConfig::safe());
$this->cache = new GeneralCache($cacheDriver, $serializer);
$this->clock = new SystemClock();
$this->pathProvider = new PathProvider('/home/michael/dev/michaelschiemer');
// Register all required dependencies
$this->container->singleton(Cache::class, $this->cache);
$this->container->singleton(Clock::class, $this->clock);
$this->container->singleton(PathProvider::class, $this->pathProvider);
// Register ReflectionProvider and ExecutionContext dependencies
$reflectionProvider = new CachedReflectionProvider();
$executionContext = ExecutionContext::detect();
$this->container->singleton(ReflectionProvider::class, $reflectionProvider);
$this->container->singleton(ExecutionContext::class, $executionContext);
$this->container->singleton(InitializerProcessor::class, fn ($c) => new InitializerProcessor(
$c,
$c->get(ReflectionProvider::class),
$c->get(ExecutionContext::class)
));
$this->container->singleton(DemoCommand::class, fn () => new DemoCommand());
// App config for testing
$appConfig = new AppConfig(
environment: 'testing',
debug: true,
timezone: Timezone::UTC,
locale: 'en'
);
$this->container->singleton(AppConfig::class, $appConfig);
// Clear all caches to ensure fresh discovery
$this->cache->clear();
// Clear opcache if available
if (function_exists('opcache_reset')) {
opcache_reset();
}
// Create test output
$this->output = new class () implements \App\Framework\Console\ConsoleOutputInterface {
public array $capturedLines = [];
public array $errorLines = [];
public function write(string $message, null|\App\Framework\Console\ConsoleColor|\App\Framework\Console\ConsoleStyle $style = null): void
{
$this->capturedLines[] = $message;
}
public function writeLine(string $message = '', ?\App\Framework\Console\ConsoleColor $color = null): void
{
$this->capturedLines[] = $message;
}
public function writeError(string $message): void
{
$this->errorLines[] = $message;
$this->capturedLines[] = "ERROR: $message";
}
public function writeSuccess(string $message): void
{
$this->capturedLines[] = "SUCCESS: $message";
}
public function writeWarning(string $message): void
{
$this->capturedLines[] = "WARNING: $message";
}
public function writeInfo(string $message): void
{
$this->capturedLines[] = "INFO: $message";
}
public function newLine(int $count = 1): void
{
for ($i = 0; $i < $count; $i++) {
$this->capturedLines[] = '';
}
}
public function askQuestion(string $question, ?string $default = null): string
{
return $default ?? '';
}
public function confirm(string $question, bool $default = false): bool
{
return $default;
}
public function writeWindowTitle(string $title, int $mode = 0): void
{
// No-op for testing
}
};
});
it('performs complete console command discovery and execution flow', function () {
// Step 1: Fresh discovery from scratch
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
$registry = $bootstrapper->bootstrap();
// Verify discovery found console commands
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
expect($consoleCommands)->not->toBeEmpty();
expect(count($consoleCommands))->toBeGreaterThanOrEqual(6); // At least the demo commands
// Step 2: Create console application
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Step 3: Verify command discovery worked
$argv = ['test-console', 'help'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('demo:hello');
expect($output)->toContain('demo:colors');
expect($output)->not->toContain('Keine Kommandos verfügbar');
// Step 4: Execute specific command
$this->output->capturedLines = []; // Clear output
$argv = ['test-console', 'demo:hello', 'EndToEndTest'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('Hallo, EndToEndTest!');
});
it('handles cache-miss scenario correctly', function () {
// Ensure cache is completely empty
$this->cache->clear();
// Verify cache is empty for discovery
$paths = [$this->pathProvider->getSourcePath()];
$context = ExecutionContext::detect();
$cacheKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $context->getType()->value);
$cachedItem = $this->cache->get($cacheKey);
expect($cachedItem->isHit)->toBeFalse();
// Create console application (should trigger fresh discovery)
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Should work despite cache miss
$argv = ['test-console', 'demo:hello'];
$exitCode = $app->run($argv);
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
// Verify cache is now populated
$cachedItem = $this->cache->get($cacheKey);
expect($cachedItem->isHit)->toBeTrue();
});
it('handles context-specific cache separation correctly', function () {
// Test that different execution contexts have separate caches
$paths = [$this->pathProvider->getSourcePath()];
$consoleKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, 'console');
$cliScriptKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, 'cli-script');
// Keys should be different
expect($consoleKey->toString())->not->toBe($cliScriptKey->toString());
// Perform discovery (will cache under current context)
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
$registry = $bootstrapper->bootstrap();
// One cache should be hit, the other should not
$consoleCached = $this->cache->get($consoleKey);
$cliScriptCached = $this->cache->get($cliScriptKey);
// At least one should be hit (depending on current context)
$anyCacheHit = $consoleCached->isHit || $cliScriptCached->isHit;
expect($anyCacheHit)->toBeTrue();
});
it('discovers minimum expected number of commands', function () {
// Fresh discovery to get actual count
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
$registry = $bootstrapper->bootstrap();
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
$commandCount = count($consoleCommands);
// Should find at least:
// - 6 demo commands (hello, colors, interactive, menu, simple-menu, wizard)
// - Various framework commands (db:migrate, db:status, etc.)
expect($commandCount)->toBeGreaterThanOrEqual(10);
// Verify specific demo commands are present
$commandNames = [];
foreach ($consoleCommands as $discovered) {
$command = $discovered->createAttributeInstance();
$commandNames[] = $command->name;
}
$expectedDemoCommands = [
'demo:hello',
'demo:colors',
'demo:interactive',
'demo:menu',
'demo:simple-menu',
'demo:wizard',
];
foreach ($expectedDemoCommands as $expectedCommand) {
expect($commandNames)->toContain($expectedCommand);
}
});
it('handles no duplicate command names', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
$registry = $bootstrapper->bootstrap();
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
$commandNames = [];
$duplicates = [];
foreach ($consoleCommands as $discovered) {
$command = $discovered->createAttributeInstance();
if (in_array($command->name, $commandNames)) {
$duplicates[] = $command->name;
} else {
$commandNames[] = $command->name;
}
}
// Should have no duplicate command names
expect($duplicates)->toBeEmpty("Found duplicate commands: " . implode(', ', $duplicates));
});
it('recovers gracefully when fallback discovery is triggered', function () {
// Simulate a scenario where initial discovery fails or finds no commands
// by registering an empty discovery registry first
$emptyRegistry = new \App\Framework\Discovery\Results\DiscoveryRegistry(
attributes: new \App\Framework\Discovery\Results\AttributeRegistry([]),
interfaces: new \App\Framework\Discovery\Results\InterfaceRegistry([]),
templates: new \App\Framework\Discovery\Results\TemplateRegistry([])
);
$this->container->singleton(\App\Framework\Discovery\Results\DiscoveryRegistry::class, $emptyRegistry);
// Create console application - should trigger fallback discovery
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Should still be able to execute commands after fallback
$argv = ['test-console', 'demo:hello'];
$exitCode = $app->run($argv);
// May succeed if fallback worked, or fail gracefully if no commands found
expect($exitCode)->toBeInt();
expect($exitCode)->toBeGreaterThanOrEqual(0);
});
it('validates ConsoleCommand attribute syntax is correct throughout codebase', function () {
// Perform discovery and verify all found commands have valid syntax
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
$registry = $bootstrapper->bootstrap();
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
foreach ($consoleCommands as $discovered) {
// Should be able to create attribute instance without errors
$command = $discovered->createAttributeInstance();
expect($command)->toBeInstanceOf(ConsoleCommand::class);
expect($command->name)->not->toBeEmpty();
expect($command->description)->not->toBeEmpty();
// Command name should follow expected format
expect($command->name)->toMatch('/^[a-z]+:[a-z\-]+$/');
}
});
it('completes full console session within reasonable time', function () {
$startTime = microtime(true);
// Complete flow: discovery -> application -> command execution
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
// Execute a command
$argv = ['test-console', 'demo:hello', 'PerformanceTest'];
$exitCode = $app->run($argv);
$endTime = microtime(true);
$duration = $endTime - $startTime;
// Should complete within reasonable time (20 seconds for CI)
expect($duration)->toBeLessThan(20.0);
// Should still succeed
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
$output = implode(' ', $this->output->capturedLines);
expect($output)->toContain('Hallo, PerformanceTest!');
});
it('maintains memory usage within bounds during discovery and execution', function () {
$initialMemory = memory_get_usage(true);
// Perform complete console flow
$app = new ConsoleApplication($this->container, 'test-console', 'Test Console', $this->output);
$argv = ['test-console', 'demo:colors'];
$exitCode = $app->run($argv);
$finalMemory = memory_get_usage(true);
$memoryIncrease = $finalMemory - $initialMemory;
// Memory increase should be reasonable (less than 100MB)
expect($memoryIncrease)->toBeLessThan(100 * 1024 * 1024);
expect($exitCode)->toBe(ExitCode::SUCCESS->value);
});
});

View File

@@ -0,0 +1,236 @@
<?php
declare(strict_types=1);
namespace Tests\Framework\Context;
use App\Framework\Context\ContextType;
use App\Framework\Context\ExecutionContext;
use App\Framework\Discovery\Cache\DiscoveryCacheIdentifiers;
describe('ExecutionContext Cache Key Generation', function () {
beforeEach(function () {
// Clear any environment variables that might affect detection
unset($_ENV['APP_ENV']);
unset($_SERVER['REQUEST_METHOD']);
unset($_SERVER['HTTP_HOST']);
unset($_SERVER['SERVER_NAME']);
});
it('generates different cache keys for different execution contexts', function () {
$paths = ['/test/src'];
// Create different execution contexts
$consoleContext = ExecutionContext::forConsole();
$webContext = ExecutionContext::forWeb();
$workerContext = ExecutionContext::forWorker();
$testContext = ExecutionContext::forTest();
// Generate cache keys for each context
$consoleKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $consoleContext->getType()->value);
$webKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $webContext->getType()->value);
$workerKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $workerContext->getType()->value);
$testKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $testContext->getType()->value);
// All keys should be different
$keys = [$consoleKey, $webKey, $workerKey, $testKey];
$uniqueKeys = array_unique(array_map(fn ($key) => $key->toString(), $keys));
expect(count($uniqueKeys))->toBe(4);
});
it('console and cli-script contexts have separate cache keys', function () {
$paths = ['/test/src'];
$consoleKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, 'console');
$cliScriptKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, 'cli-script');
expect($consoleKey->toString())->not->toBe($cliScriptKey->toString());
// Both should contain their respective context type
expect($consoleKey->toString())->toContain('console');
expect($cliScriptKey->toString())->toContain('cli-script');
});
it('cache keys are consistent for the same context', function () {
$paths = ['/test/src'];
$context = 'console';
$key1 = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $context);
$key2 = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $context);
expect($key1->toString())->toBe($key2->toString());
});
it('cache keys change with different paths', function () {
$paths1 = ['/test/src'];
$paths2 = ['/test/src', '/test/additional'];
$context = 'console';
$key1 = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths1, $context);
$key2 = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths2, $context);
expect($key1->toString())->not->toBe($key2->toString());
});
it('validates cache key format is safe for storage', function () {
$paths = ['/test/src'];
$contexts = ['console', 'cli-script', 'web', 'worker', 'test'];
foreach ($contexts as $context) {
$key = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $context);
// Cache key should only contain safe characters
expect($key->toString())->toMatch('/^[a-zA-Z0-9_\-:\.]+$/');
// Should not be empty
expect($key->toString())->not->toBeEmpty();
// Should have reasonable length (not too short or too long)
expect(strlen($key->toString()))->toBeGreaterThan(10);
expect(strlen($key->toString()))->toBeLessThan(200);
}
});
});
describe('ExecutionContext Detection and Metadata', function () {
it('can detect different execution contexts', function () {
// Test forced contexts
$consoleContext = ExecutionContext::forConsole();
expect($consoleContext->isConsole())->toBeTrue();
expect($consoleContext->isCli())->toBeTrue();
expect($consoleContext->isWeb())->toBeFalse();
$webContext = ExecutionContext::forWeb();
expect($webContext->isWeb())->toBeTrue();
expect($webContext->isCli())->toBeFalse();
expect($webContext->isConsole())->toBeFalse();
$workerContext = ExecutionContext::forWorker();
expect($workerContext->isWorker())->toBeTrue();
expect($workerContext->isCli())->toBeTrue();
expect($workerContext->isWeb())->toBeFalse();
$testContext = ExecutionContext::forTest();
expect($testContext->isTest())->toBeTrue();
expect($testContext->isCli())->toBeFalse(); // Test context is separate from CLI
});
it('provides consistent metadata for cache key generation', function () {
$context = ExecutionContext::forConsole();
$metadata1 = $context->getMetadata();
$metadata2 = $context->getMetadata();
// Metadata should be consistent
expect($metadata1['type'])->toBe($metadata2['type']);
expect($metadata1['sapi'])->toBe($metadata2['sapi']);
// Type should match context
expect($metadata1['type'])->toBe('console');
});
it('includes execution context type in metadata', function () {
$contexts = [
ExecutionContext::forConsole(),
ExecutionContext::forWeb(),
ExecutionContext::forWorker(),
ExecutionContext::forTest(),
];
foreach ($contexts as $context) {
$metadata = $context->getMetadata();
expect($metadata)->toHaveKey('type');
expect($metadata['type'])->toBe($context->getType()->value);
expect($metadata)->toHaveKey('sapi');
expect($metadata)->toHaveKey('pid');
}
});
});
describe('ExecutionContext ContextType Enum', function () {
it('has all required context types', function () {
$types = [
ContextType::CONSOLE,
ContextType::WEB,
ContextType::WORKER,
ContextType::TEST,
ContextType::CLI_SCRIPT,
];
foreach ($types as $type) {
expect($type)->toBeInstanceOf(ContextType::class);
expect($type->value)->toBeString();
expect($type->value)->not->toBeEmpty();
}
});
it('context type values are suitable for cache keys', function () {
$types = [
ContextType::CONSOLE,
ContextType::WEB,
ContextType::WORKER,
ContextType::TEST,
ContextType::CLI_SCRIPT,
];
foreach ($types as $type) {
// Values should be valid for cache keys (no spaces, special chars)
expect($type->value)->toMatch('/^[a-z\-]+$/');
expect($type->value)->not->toContain(' ');
expect($type->value)->not->toContain('/');
expect($type->value)->not->toContain('\\');
}
});
it('console and cli-script are different types', function () {
expect(ContextType::CONSOLE->value)->not->toBe(ContextType::CLI_SCRIPT->value);
expect(ContextType::CONSOLE->value)->toBe('console');
expect(ContextType::CLI_SCRIPT->value)->toBe('cli-script');
});
});
describe('Cache Key Collision Prevention', function () {
it('prevents cache collisions between console.php and other CLI scripts', function () {
$paths = ['/test/src'];
// Before the fix, console.php was detected as CLI_SCRIPT
// Now it should be handled separately as CONSOLE context
$consoleKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, ContextType::CONSOLE->value);
$cliScriptKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, ContextType::CLI_SCRIPT->value);
expect($consoleKey->toString())->not->toBe($cliScriptKey->toString());
// Verify the fix: different contexts should have distinct cache keys
$keyDifferences = array_diff_assoc(
str_split($consoleKey->toString()),
str_split($cliScriptKey->toString())
);
// There should be differences between the keys
expect(count($keyDifferences))->toBeGreaterThan(0);
});
it('ensures discovery cache separation between execution contexts', function () {
$paths = ['/test/src'];
// All different contexts should produce different cache keys
$contexts = ['console', 'cli-script', 'web', 'worker', 'test'];
$keys = [];
foreach ($contexts as $context) {
$key = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $context);
$keys[$context] = $key->toString();
}
// All keys should be unique
$uniqueKeys = array_unique($keys);
expect(count($uniqueKeys))->toBe(count($contexts));
// Each key should be identifiable by its context
foreach ($contexts as $context) {
expect($keys[$context])->toContain($context);
}
});
});

View File

@@ -0,0 +1,285 @@
<?php
declare(strict_types=1);
namespace Tests\Framework\Discovery;
use App\Framework\Cache\Cache;
use App\Framework\Cache\CacheItem;
use App\Framework\Cache\Driver\InMemoryCache;
use App\Framework\Cache\GeneralCache;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Context\ExecutionContext;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\Clock;
use App\Framework\DateTime\SystemClock;
use App\Framework\DI\Container;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\Cache\DiscoveryCacheIdentifiers;
use App\Framework\Discovery\DiscoveryServiceBootstrapper;
use App\Framework\Discovery\Results\DiscoveryRegistry;
use App\Framework\Serializer\Php\PhpSerializer;
use App\Framework\Serializer\Php\PhpSerializerConfig;
describe('DiscoveryService Integration with Console Commands', function () {
beforeEach(function () {
// Create fresh container and dependencies for each test
$this->container = new DefaultContainer();
$cacheDriver = new InMemoryCache();
$serializer = new PhpSerializer(PhpSerializerConfig::safe());
$this->cache = new GeneralCache($cacheDriver, $serializer);
$this->clock = new SystemClock();
$this->pathProvider = new PathProvider('/home/michael/dev/michaelschiemer');
// Register dependencies in container
$this->container->singleton(Cache::class, $this->cache);
$this->container->singleton(Clock::class, $this->clock);
$this->container->singleton(PathProvider::class, $this->pathProvider);
// Clear cache between tests
$this->cache->clear();
// Clear any opcache
if (function_exists('opcache_reset')) {
opcache_reset();
}
});
it('properly handles different execution contexts in discovery', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
// Test console context discovery
$consoleContext = ExecutionContext::forConsole();
$paths = [$this->pathProvider->getSourcePath()];
// Perform discovery
$registry = $bootstrapper->performBootstrap($this->pathProvider, $this->cache, null);
expect($registry)->toBeInstanceOf(DiscoveryRegistry::class);
expect($registry->isEmpty())->toBeFalse();
// Should have discovered ConsoleCommand attributes
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
expect($consoleCommands)->not->toBeEmpty();
});
it('finds the correct number of console commands after fresh discovery', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
// Perform fresh discovery
$registry = $bootstrapper->performBootstrap($this->pathProvider, $this->cache, null);
// Get console commands
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
// Should find a reasonable number of commands (at least the demo commands)
expect(count($consoleCommands))->toBeGreaterThanOrEqual(6); // Demo commands
expect(count($consoleCommands))->toBeLessThan(100); // Reasonable upper bound
// Should include demo commands
$commandNames = [];
foreach ($consoleCommands as $discovered) {
$command = $discovered->createAttributeInstance();
$commandNames[] = $command->name;
}
expect($commandNames)->toContain('demo:hello');
expect($commandNames)->toContain('demo:colors');
expect($commandNames)->toContain('demo:interactive');
});
it('caches discovery results properly', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->cache);
// First discovery should cache results
$registry1 = $bootstrapper->bootstrap();
// Check that cache contains discovery results
$paths = [$this->pathProvider->getSourcePath()];
$context = ExecutionContext::detect();
$cacheKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $context->getType()->value);
$cachedItem = $this->cache->get($cacheKey);
expect($cachedItem->isHit)->toBeTrue();
// Second discovery should use cache
$registry2 = $bootstrapper->bootstrap();
// Results should be equivalent
expect($registry1->isEmpty())->toBe($registry2->isEmpty());
$commands1 = $registry1->attributes->get(ConsoleCommand::class);
$commands2 = $registry2->attributes->get(ConsoleCommand::class);
expect(count($commands1))->toBe(count($commands2));
});
it('handles cache misses gracefully with fallback discovery', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
// Ensure cache is empty
$this->cache->clear();
// Bootstrap should perform fresh discovery when cache is empty
$registry = $bootstrapper->bootstrap();
expect($registry)->toBeInstanceOf(DiscoveryRegistry::class);
expect($registry->isEmpty())->toBeFalse();
// Should have found console commands even without cache
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
expect($consoleCommands)->not->toBeEmpty();
});
it('separates cache by execution context', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
$paths = [$this->pathProvider->getSourcePath()];
// Create cache keys for different contexts
$consoleKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, 'console');
$cliScriptKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, 'cli-script');
// Keys should be different
expect($consoleKey->toString())->not->toBe($cliScriptKey->toString());
// Cache data for console context
$consoleRegistry = $bootstrapper->performBootstrap($this->pathProvider, $this->cache, null);
// Check that only console cache is populated
$consoleCached = $this->cache->get($consoleKey);
$cliScriptCached = $this->cache->get($cliScriptKey);
expect($consoleCached->isHit)->toBeTrue();
expect($cliScriptCached->isHit)->toBeFalse();
});
it('handles invalid cached data gracefully', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
$paths = [$this->pathProvider->getSourcePath()];
$context = ExecutionContext::detect();
// Put invalid data in cache
$cacheKey = DiscoveryCacheIdentifiers::fullDiscoveryKey($paths, $context->getType()->value);
$invalidItem = CacheItem::forSetting($cacheKey, 'invalid_data');
$this->cache->set($invalidItem);
// Bootstrap should handle invalid cache gracefully and perform fresh discovery
$registry = $bootstrapper->bootstrap();
expect($registry)->toBeInstanceOf(DiscoveryRegistry::class);
expect($registry->isEmpty())->toBeFalse();
});
it('processes initializers after discovery', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
// Bootstrap should process initializers
$registry = $bootstrapper->bootstrap();
// Registry should be registered in container
expect($this->container->has(DiscoveryRegistry::class))->toBeTrue();
$containerRegistry = $this->container->get(DiscoveryRegistry::class);
expect($containerRegistry)->toBe($registry);
});
it('detects when discovery is required', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
// Initially should require discovery
expect($bootstrapper->isDiscoveryRequired())->toBeTrue();
// After bootstrap should still work
$bootstrapper->bootstrap();
// The method should still be callable
$isRequired = $bootstrapper->isDiscoveryRequired();
expect($isRequired)->toBeOfType('boolean');
});
it('performs incremental discovery when service is available', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
// First bootstrap
$registry1 = $bootstrapper->bootstrap();
// Incremental bootstrap should work
$registry2 = $bootstrapper->incrementalBootstrap();
expect($registry2)->toBeInstanceOf(DiscoveryRegistry::class);
// Should have registered new registry in container
expect($this->container->has(DiscoveryRegistry::class))->toBeTrue();
});
it('falls back to full bootstrap when incremental is not possible', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
// Incremental without previous bootstrap should fall back to full bootstrap
$registry = $bootstrapper->incrementalBootstrap();
expect($registry)->toBeInstanceOf(DiscoveryRegistry::class);
expect($registry->isEmpty())->toBeFalse();
});
});
describe('DiscoveryService Performance and Error Handling', function () {
beforeEach(function () {
$this->container = new DefaultContainer();
$cacheDriver = new InMemoryCache();
$serializer = new PhpSerializer(PhpSerializerConfig::safe());
$this->cache = new GeneralCache($cacheDriver, $serializer);
$this->clock = new SystemClock();
$this->pathProvider = new PathProvider('/home/michael/dev/michaelschiemer');
$this->container->singleton(Cache::class, $this->cache);
$this->container->singleton(Clock::class, $this->clock);
$this->container->singleton(PathProvider::class, $this->pathProvider);
});
it('completes discovery within reasonable time', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
$startTime = microtime(true);
$registry = $bootstrapper->bootstrap();
$endTime = microtime(true);
$duration = $endTime - $startTime;
// Discovery should complete within 30 seconds (generous for CI)
expect($duration)->toBeLessThan(30.0);
// Should still produce valid results
expect($registry)->toBeInstanceOf(DiscoveryRegistry::class);
expect($registry->isEmpty())->toBeFalse();
});
it('handles discovery errors gracefully', function () {
// Create bootstrapper with invalid path
$invalidPathProvider = new PathProvider('/nonexistent/path');
$this->container->instance(PathProvider::class, $invalidPathProvider);
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
// Should not throw exception, but may produce empty or partial results
$registry = $bootstrapper->bootstrap();
expect($registry)->toBeInstanceOf(DiscoveryRegistry::class);
// May be empty due to invalid path, but should not crash
});
it('maintains memory usage within reasonable bounds', function () {
$bootstrapper = new DiscoveryServiceBootstrapper($this->container, $this->clock);
$initialMemory = memory_get_usage(true);
$registry = $bootstrapper->bootstrap();
$finalMemory = memory_get_usage(true);
$memoryIncrease = $finalMemory - $initialMemory;
// Memory increase should be reasonable (less than 50MB)
expect($memoryIncrease)->toBeLessThan(50 * 1024 * 1024);
// Should still produce valid results
expect($registry)->toBeInstanceOf(DiscoveryRegistry::class);
});
});

View File

@@ -5,7 +5,6 @@ declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use App\Framework\Design\ComponentScanner;
use App\Framework\Filesystem\FilePath;
$scanner = new ComponentScanner();
@@ -50,9 +49,9 @@ file_put_contents($tempFile, $testCss);
try {
// Scan the test file
$registry = $scanner->scanComponents([$tempFile]);
echo "Total components found: " . $registry->getTotalComponents() . "\n\n";
$components = $registry->getAllComponents();
foreach ($components as $component) {
echo "Component: " . $component->name . "\n";
@@ -63,23 +62,23 @@ try {
echo " - Selector: " . $component->selector . "\n";
echo "\n";
}
// Now test with real CSS files
echo "\n--- Scanning Real CSS Files ---\n\n";
$cssFiles = [
__DIR__ . '/../../resources/css/components/card.css',
__DIR__ . '/../../resources/css/components/buttons.css',
__DIR__ . '/../../resources/css/components/sidebar.css',
];
$existingFiles = array_filter($cssFiles, 'file_exists');
if (!empty($existingFiles)) {
if (! empty($existingFiles)) {
$realRegistry = $scanner->scanComponents($existingFiles);
echo "Total components found in real files: " . $realRegistry->getTotalComponents() . "\n\n";
$realComponents = $realRegistry->getAllComponents();
foreach (array_slice($realComponents, 0, 10) as $component) {
echo "Component: " . $component->name . "\n";
@@ -89,7 +88,7 @@ try {
echo " - File: " . basename($component->filePath) . "\n";
echo "\n";
}
// Show category counts
echo "\nCategory Distribution:\n";
$categoryCounts = $realRegistry->getCategoryCounts();
@@ -101,11 +100,11 @@ try {
} else {
echo "No real CSS files found to scan.\n";
}
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
echo "Trace: " . $e->getTraceAsString() . "\n";
} finally {
// Clean up temp file
@unlink($tempFile);
}
}

View File

@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
echo "Testing console command file and attributes...\n\n";
// Test if we can find the ConsoleCommand attribute manually
$file = '/var/www/html/src/Framework/Mcp/Console/McpServerCommand.php';
$content = file_get_contents($file);
echo "File content (first 25 lines):\n";
echo implode("\n", array_slice(explode("\n", $content), 0, 25)) . "\n\n";
// Check if attribute is present
if (strpos($content, '#[ConsoleCommand') !== false) {
echo "✅ ConsoleCommand attribute found in file\n";
} else {
echo "❌ ConsoleCommand attribute NOT found in file\n";
}
// Try to use reflection on the class
try {
$class = new ReflectionClass('App\Framework\Mcp\Console\McpServerCommand');
echo "✅ Class loaded successfully\n";
echo "Methods: " . implode(', ', array_map(fn ($m) => $m->getName(), $class->getMethods())) . "\n";
$executeMethod = $class->getMethod('execute');
$attributes = $executeMethod->getAttributes();
echo "Attributes on execute method: " . count($attributes) . "\n";
foreach ($attributes as $attr) {
echo " - " . $attr->getName() . "\n";
$instance = $attr->newInstance();
if ($instance instanceof \App\Framework\Console\ConsoleCommand) {
echo " name: " . $instance->name . "\n";
echo " description: " . $instance->description . "\n";
}
}
} catch (Exception $e) {
echo "❌ Error loading class: " . $e->getMessage() . "\n";
echo "Stack trace: " . $e->getTraceAsString() . "\n";
}
echo "\nTesting if Discovery would find this file...\n";
// Test if file would be scanned by Discovery
$pathProvider = new \App\Framework\Core\PathProvider('/var/www/html');
$fileScanner = new \App\Framework\Filesystem\FileScanner();
$pattern = new \App\Framework\Filesystem\ValueObjects\FilePattern('*.php');
$basePath = \App\Framework\Filesystem\FilePath::fromString('/var/www/html/src');
$files = $fileScanner->scan($basePath, $pattern);
$mcpCommandFile = null;
foreach ($files as $file) {
if (strpos($file->getPath(), 'McpServerCommand.php') !== false) {
$mcpCommandFile = $file;
break;
}
}
if ($mcpCommandFile) {
echo "✅ McpServerCommand.php would be found by file scanner\n";
echo "File path: " . $mcpCommandFile->getPath() . "\n";
} else {
echo "❌ McpServerCommand.php would NOT be found by file scanner\n";
}

View File

@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
// Test Discovery service bootstrap with all required dependencies
$clock = new \App\Framework\DateTime\SystemClock();
$highResClock = new \App\Framework\DateTime\SystemHighResolutionClock();
$memoryMonitor = new \App\Framework\Performance\MemoryMonitor();
$collector = new \App\Framework\Performance\EnhancedPerformanceCollector(
$clock,
$highResClock,
$memoryMonitor,
enabled: false
);
$container = new \App\Framework\DI\DefaultContainer();
// Add all required dependencies like in the real bootstrap
$container->instance(\App\Framework\DateTime\Clock::class, $clock);
$container->instance(\App\Framework\Logging\Logger::class, new \App\Framework\Logging\DefaultLogger());
$container->instance(\App\Framework\Performance\Contracts\PerformanceCollectorInterface::class, $collector);
// Initialize cache properly
$cacheInitializer = new \App\Framework\Cache\CacheInitializer($collector, $container);
$cache = $cacheInitializer();
$container->instance(\App\Framework\Cache\Cache::class, $cache);
// Add PathProvider
$container->instance(\App\Framework\Core\PathProvider::class, new \App\Framework\Core\PathProvider('/var/www/html'));
// Add ExecutionContext properly - this is crucial!
$env = \App\Framework\Config\Environment::fromFile('/var/www/html/.env');
$executionContext = \App\Framework\Context\ExecutionContext::detect($env);
$container->instance(\App\Framework\Context\ExecutionContext::class, $executionContext);
try {
echo "Starting Discovery bootstrap test...\n";
$bootstrapper = new \App\Framework\Discovery\DiscoveryServiceBootstrapper($container, $clock);
echo "DiscoveryServiceBootstrapper created successfully\n";
$results = $bootstrapper->bootstrap();
echo "Discovery bootstrap completed successfully\n";
if ($results instanceof \App\Framework\Discovery\Results\DiscoveryRegistry) {
// Get the attributes from the registry
$attributes = $results->attributes;
// Check for routes
$routeAttributes = $attributes->get('App\\Framework\\Http\\Routing\\Route');
echo "Routes found: " . count($routeAttributes) . "\n";
// Check for console commands
$commandAttributes = $attributes->get('App\\Framework\\Console\\ConsoleCommand');
echo "Console commands found: " . count($commandAttributes) . "\n";
// Check for MCP tools
$mcpToolAttributes = $attributes->get('App\\Framework\\Mcp\\Attributes\\McpTool');
echo "MCP tools found: " . count($mcpToolAttributes) . "\n";
// Check for initializers
$initializerAttributes = $attributes->get('App\\Framework\\DI\\Initializer');
echo "Initializers found: " . count($initializerAttributes) . "\n";
// List all discovered attribute types
echo "All discovered attribute types:\n";
foreach ($attributes->getAllTypes() as $type) {
echo " - " . $type . " (" . $attributes->getCount($type) . ")\n";
}
// Show a few routes if any
if (! empty($routeAttributes)) {
echo "First few routes:\n";
foreach (array_slice($routeAttributes, 0, 3) as $route) {
echo " - " . ($route->getClassName()->getShortName()) . "::" . ($route->getMethodName()?->getName() ?? 'unknown') . "\n";
}
}
// Show console commands if any
if (! empty($commandAttributes)) {
echo "Console commands:\n";
foreach ($commandAttributes as $command) {
$args = $command->getArguments();
echo " - " . ($args['name'] ?? 'unknown') . " (" . $command->getClassName()->getShortName() . ")\n";
}
}
} else {
echo "No results returned or results not in expected format\n";
var_dump($results);
}
} catch (Exception $e) {
echo "Discovery bootstrap failed: " . $e->getMessage() . "\n";
echo "File: " . $e->getFile() . ":" . $e->getLine() . "\n";
echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
}

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use App\Framework\Console\ConsoleCommand;
use App\Framework\Context\ExecutionContext;
use App\Framework\Core\AppBootstrapper;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\SystemHighResolutionClock;
use App\Framework\Discovery\Results\DiscoveryRegistry;
use App\Framework\Performance\EnhancedPerformanceCollector;
use App\Framework\Performance\MemoryMonitor;
echo "=== Clean Discovery Test (with AppBootstrapper) ===\n\n";
// Create dependencies exactly like console.php
$clock = new SystemClock();
$highResClock = new SystemHighResolutionClock();
$memoryMonitor = new MemoryMonitor();
// Disable performance collection for CLI to prevent memory exhaustion during discovery
$collector = new EnhancedPerformanceCollector($clock, $highResClock, $memoryMonitor, enabled: false);
$bootstrapper = new AppBootstrapper('/home/michael/dev/michaelschiemer', $collector, $memoryMonitor);
echo "1. Environment:\n";
echo " - Context: " . ExecutionContext::detect()->getType()->value . "\n\n";
// Bootstrap console application exactly like console.php
echo "2. Bootstrapping Console Application:\n";
try {
echo " - Creating console application...\n";
$consoleApp = $bootstrapper->bootstrapConsole();
echo " - Console application created successfully\n";
// Now get the discovery registry from the container
$container = $consoleApp->getContainer();
if ($container->has(DiscoveryRegistry::class)) {
echo " - DiscoveryRegistry found in container\n";
$registry = $container->get(DiscoveryRegistry::class);
echo " - Registry empty: " . ($registry->isEmpty() ? 'YES' : 'NO') . "\n";
echo " - Total attribute types: " . count($registry->attributes->getAllTypes()) . "\n";
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
echo " - ConsoleCommand attributes found: " . count($consoleCommands) . "\n";
// List all types found
echo " - All discovered types:\n";
foreach ($registry->attributes->getAllTypes() as $type) {
$count = $registry->attributes->getCount($type);
echo " * $type: $count instances\n";
if ($type === ConsoleCommand::class && $count > 0) {
echo " Sample commands:\n";
$commands = $registry->attributes->get($type);
$sampleCount = 0;
foreach ($commands as $discovered) {
if ($sampleCount >= 3) {
break;
}
$command = $discovered->createAttributeInstance();
echo " - " . $command->name . "\n";
$sampleCount++;
}
}
}
} else {
echo " - ❌ DiscoveryRegistry not found in container\n";
}
} catch (\Throwable $e) {
echo " - ❌ Error: " . $e->getMessage() . "\n";
echo " - File: " . $e->getFile() . ":" . $e->getLine() . "\n";
echo " - Stack trace:\n" . $e->getTraceAsString() . "\n";
}
echo "\n=== End Test ===\n";

View File

@@ -0,0 +1,269 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use App\Framework\Cache\Cache;
use App\Framework\Cache\CacheIdentifier;
use App\Framework\Cache\CacheItem;
use App\Framework\Cache\CacheKey;
use App\Framework\Cache\CacheResult;
use App\Framework\Core\ValueObjects\Duration;
// Simple test cache implementation
class TestCache implements Cache
{
private array $cache = [];
public function get(CacheIdentifier ...$identifiers): CacheResult
{
$results = [];
foreach ($identifiers as $identifier) {
$key = $identifier->toString();
if (isset($this->cache[$key])) {
$results[$key] = CacheItem::hit($identifier, $this->cache[$key]);
} else {
$results[$key] = CacheItem::miss($identifier);
}
}
return CacheResult::fromItems(...array_values($results));
}
public function set(CacheItem ...$items): bool
{
foreach ($items as $item) {
$this->cache[$item->key->toString()] = $item->value;
}
return true;
}
public function forget(CacheIdentifier ...$identifiers): bool
{
foreach ($identifiers as $identifier) {
unset($this->cache[$identifier->toString()]);
}
return true;
}
public function clear(): bool
{
$this->cache = [];
return true;
}
public function remember(CacheKey $key, callable $callback, ?Duration $ttl = null): CacheItem
{
$keyStr = $key->toString();
if (isset($this->cache[$keyStr])) {
return CacheItem::hit($key, $this->cache[$keyStr]);
}
$value = $callback();
$this->cache[$keyStr] = $value;
return CacheItem::hit($key, $value);
}
public function has(CacheIdentifier ...$identifiers): array
{
$results = [];
foreach ($identifiers as $identifier) {
$results[$identifier->toString()] = isset($this->cache[$identifier->toString()]);
}
return $results;
}
}
use App\Framework\Config\Environment;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\Clock;
use App\Framework\DateTime\SystemClock;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\DiscoveryServiceBootstrapper;
use App\Framework\Discovery\InitializerProcessor;
use App\Framework\Logging\Logger;
use App\Framework\Logging\LogLevel;
// Simple array logger for testing
class TestArrayLogger implements Logger
{
private array $messages = [];
public function getMessages(): array
{
return $this->messages;
}
public function debug(string $message, array $context = []): void
{
$this->messages[] = ['level' => 'debug', 'message' => $message, 'context' => $context];
}
public function info(string $message, array $context = []): void
{
$this->messages[] = ['level' => 'info', 'message' => $message, 'context' => $context];
}
public function notice(string $message, array $context = []): void
{
$this->messages[] = ['level' => 'notice', 'message' => $message, 'context' => $context];
}
public function warning(string $message, array $context = []): void
{
$this->messages[] = ['level' => 'warning', 'message' => $message, 'context' => $context];
}
public function error(string $message, array $context = []): void
{
$this->messages[] = ['level' => 'error', 'message' => $message, 'context' => $context];
}
public function critical(string $message, array $context = []): void
{
$this->messages[] = ['level' => 'critical', 'message' => $message, 'context' => $context];
}
public function alert(string $message, array $context = []): void
{
$this->messages[] = ['level' => 'alert', 'message' => $message, 'context' => $context];
}
public function emergency(string $message, array $context = []): void
{
$this->messages[] = ['level' => 'emergency', 'message' => $message, 'context' => $context];
}
public function log(LogLevel $level, string $message, array $context = []): void
{
$this->messages[] = ['level' => $level->value, 'message' => $message, 'context' => $context];
}
}
use App\Framework\Context\ExecutionContext;
use App\Framework\Reflection\CachedReflectionProvider;
use App\Framework\Reflection\ReflectionProvider;
echo "=== Discovery System Debug Test ===\n";
// Setup basic services
$container = new DefaultContainer();
$logger = new TestArrayLogger();
$cache = new TestCache();
$clock = new SystemClock();
$pathProvider = new PathProvider(__DIR__ . '/../..');
$reflectionProvider = new CachedReflectionProvider();
$environment = new Environment();
$executionContext = ExecutionContext::detect();
// Register required services
$container->singleton(TestArrayLogger::class, $logger);
$container->singleton(Logger::class, $logger);
$container->singleton(TestCache::class, $cache);
$container->singleton(Cache::class, $cache);
$container->singleton(Clock::class, $clock);
$container->singleton(PathProvider::class, $pathProvider);
$container->singleton(ReflectionProvider::class, $reflectionProvider);
$container->singleton(Environment::class, $environment);
$container->singleton(ExecutionContext::class, $executionContext);
// Create InitializerProcessor
$initializerProcessor = new InitializerProcessor(
$container,
$reflectionProvider,
$executionContext
);
$container->singleton(InitializerProcessor::class, $initializerProcessor);
echo "Step 1: Creating Discovery Service Bootstrapper...\n";
// Create Discovery Service
$discoveryBootstrapper = new DiscoveryServiceBootstrapper($container, $clock);
echo "Step 2: Clearing cache to force fresh discovery...\n";
// Clear cache to force fresh discovery
$cache->clear();
echo "Step 3: Running Discovery bootstrap...\n";
try {
$registry = $discoveryBootstrapper->bootstrap();
echo "Step 4: Discovery completed successfully!\n";
echo "Registry contains " . count($registry) . " items\n";
// Check for Request interface binding
echo "\nStep 5: Checking container bindings...\n";
$requestInterface = 'App\Framework\Http\Request';
$hasRequestBinding = $container->has($requestInterface);
echo "Request interface binding exists: " . ($hasRequestBinding ? "YES" : "NO") . "\n";
if ($hasRequestBinding) {
try {
$requestInstance = $container->get($requestInterface);
echo "Request instance created: " . get_class($requestInstance) . "\n";
} catch (Throwable $e) {
echo "Error creating Request instance: " . $e->getMessage() . "\n";
}
}
// Show discovered attributes
echo "\nStep 6: Discovered attribute types:\n";
if (isset($registry->attributes)) {
// Use reflection to access the attribute data
$reflection = new \ReflectionObject($registry->attributes);
$property = $reflection->getProperty('mappings');
$property->setAccessible(true);
$mappings = $property->getValue($registry->attributes);
foreach ($mappings as $type => $attributes) {
$count = count($attributes);
echo "- {$type}: {$count} instances\n";
}
}
// Show discovered initializers specifically
echo "\nStep 7: Discovered Initializers:\n";
if (isset($registry->attributes)) {
$initializerResults = $registry->attributes->get(\App\Framework\DI\Initializer::class);
echo "Found " . count($initializerResults) . " initializers:\n";
foreach ($initializerResults as $idx => $discoveredAttribute) {
echo " {$idx}: {$discoveredAttribute->className}::{$discoveredAttribute->methodName}\n";
// Check additional data
if ($discoveredAttribute->additionalData) {
$returnType = $discoveredAttribute->additionalData['return'] ?? 'unknown';
echo " -> Returns: {$returnType}\n";
}
}
}
echo "\nStep 8: Discovery log messages:\n";
foreach ($logger->getMessages() as $message) {
echo "[{$message['level']}] {$message['message']}\n";
if (! empty($message['context'])) {
echo " Context: " . json_encode($message['context'], JSON_PRETTY_PRINT) . "\n";
}
}
} catch (Throwable $e) {
echo "Discovery failed with error: " . $e->getMessage() . "\n";
echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
echo "\nDebug log messages:\n";
foreach ($logger->getMessages() as $message) {
echo "[{$message['level']}] {$message['message']}\n";
if (! empty($message['context'])) {
echo " Context: " . json_encode($message['context'], JSON_PRETTY_PRINT) . "\n";
}
}
}
echo "\n=== Debug Test Complete ===\n";

View File

@@ -0,0 +1,149 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use App\Framework\Cache\Driver\InMemoryCache;
use App\Framework\Cache\GeneralCache;
use App\Framework\Config\AppConfig;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Context\ExecutionContext;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\Timezone;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\DiscoveryServiceBootstrapper;
use App\Framework\Discovery\InitializerProcessor;
use App\Framework\Reflection\CachedReflectionProvider;
use App\Framework\Serializer\Php\PhpSerializer;
use App\Framework\Serializer\Php\PhpSerializerConfig;
echo "=== Discovery Diagnosis Script ===\n\n";
// Create completely fresh container
$container = new DefaultContainer();
$cacheDriver = new InMemoryCache();
$serializer = new PhpSerializer(PhpSerializerConfig::safe());
$cache = new GeneralCache($cacheDriver, $serializer);
$clock = new SystemClock();
$pathProvider = new PathProvider('/home/michael/dev/michaelschiemer');
// Register all dependencies exactly like tests
$container->singleton(\App\Framework\Cache\Cache::class, $cache);
$container->singleton(\App\Framework\DateTime\Clock::class, $clock);
$container->singleton(PathProvider::class, $pathProvider);
$reflectionProvider = new CachedReflectionProvider();
$executionContext = ExecutionContext::detect();
$container->singleton(\App\Framework\Reflection\ReflectionProvider::class, $reflectionProvider);
$container->singleton(ExecutionContext::class, $executionContext);
$container->singleton(InitializerProcessor::class, fn ($c) => new InitializerProcessor(
$c,
$c->get(\App\Framework\Reflection\ReflectionProvider::class),
$c->get(ExecutionContext::class)
));
// App config for testing
$appConfig = new AppConfig(
environment: 'testing',
debug: true,
timezone: Timezone::UTC,
locale: 'en'
);
$container->singleton(AppConfig::class, $appConfig);
// Clear cache
$cache->clear();
echo "1. Environment Setup:\n";
echo " - Context: " . $executionContext->getType()->value . "\n";
echo " - Source Path: " . $pathProvider->getSourcePath() . "\n";
echo " - Cache cleared: ✓\n\n";
// Test basic reflection first
echo "2. Basic Reflection Test:\n";
$reflection = new ReflectionClass(\App\Framework\Console\DemoCommand::class);
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
$foundCommands = 0;
foreach ($methods as $method) {
$attributes = $method->getAttributes(ConsoleCommand::class);
$foundCommands += count($attributes);
foreach ($attributes as $attribute) {
$command = $attribute->newInstance();
echo " - Found: " . $command->name . "\n";
}
}
echo " - Total demo commands found via reflection: $foundCommands\n\n";
// Test discovery service
echo "3. Discovery Service Test:\n";
// Let's also check if the UnifiedDiscoveryService exists in container
if ($container->has(\App\Framework\Discovery\UnifiedDiscoveryService::class)) {
echo " - UnifiedDiscoveryService already in container\n";
} else {
echo " - UnifiedDiscoveryService not in container - will be created\n";
}
$bootstrapper = new DiscoveryServiceBootstrapper($container, $clock);
try {
echo " - Starting bootstrap process...\n";
// Let's use performBootstrap directly to avoid cached results
$registry = $bootstrapper->performBootstrap($pathProvider, $cache, null);
echo " - Bootstrap completed successfully\n";
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
$discoveredCount = count($consoleCommands);
echo " - Total commands found via discovery: $discoveredCount\n";
if ($discoveredCount === 0) {
echo " - ❌ No commands discovered - investigating...\n";
// Check registry contents
echo " - Registry empty: " . ($registry->isEmpty() ? 'YES' : 'NO') . "\n";
// Check what types are available
$allTypes = $registry->attributes->getAllTypes();
echo " - Total attribute types in registry: " . count($allTypes) . "\n";
foreach ($allTypes as $type) {
$count = $registry->attributes->getCount($type);
echo " - Found $type: $count instances\n";
}
// Let's also manually check if we have the specific type
$hasConsoleCommand = $registry->attributes->has(ConsoleCommand::class);
echo " - Has ConsoleCommand type: " . ($hasConsoleCommand ? 'YES' : 'NO') . "\n";
} else {
echo " - ✓ Discovery working!\n";
echo " - Sample commands:\n";
$count = 0;
foreach ($consoleCommands as $discovered) {
if ($count >= 5) {
echo " - ... and " . ($discoveredCount - 5) . " more\n";
break;
}
$command = $discovered->createAttributeInstance();
echo " - " . $command->name . "\n";
$count++;
}
}
} catch (\Throwable $e) {
echo " - ❌ Discovery failed with exception:\n";
echo " - Error: " . $e->getMessage() . "\n";
echo " - File: " . $e->getFile() . ":" . $e->getLine() . "\n";
}
echo "\n=== End Diagnosis ===\n";

View File

@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use App\Framework\Cache\Driver\InMemoryCache;
use App\Framework\Cache\GeneralCache;
use App\Framework\Config\AppConfig;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Context\ContextType;
use App\Framework\Context\ExecutionContext;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\Timezone;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\DiscoveryServiceBootstrapper;
use App\Framework\Discovery\InitializerProcessor;
use App\Framework\Reflection\CachedReflectionProvider;
use App\Framework\Serializer\Php\PhpSerializer;
use App\Framework\Serializer\Php\PhpSerializerConfig;
echo "=== Discovery Test Forcing CLI Context ===\n\n";
// Create container with minimal setup
$container = new DefaultContainer();
$cacheDriver = new InMemoryCache();
$serializer = new PhpSerializer(PhpSerializerConfig::safe());
$cache = new GeneralCache($cacheDriver, $serializer);
$clock = new SystemClock();
$pathProvider = new PathProvider('/home/michael/dev/michaelschiemer');
// Register dependencies
$container->singleton(\App\Framework\Cache\Cache::class, $cache);
$container->singleton(\App\Framework\DateTime\Clock::class, $clock);
$container->singleton(PathProvider::class, $pathProvider);
$reflectionProvider = new CachedReflectionProvider();
// Force create a cli-script execution context instead of auto-detecting
$forcedContext = new ExecutionContext(ContextType::CLI_SCRIPT, ['script' => __FILE__]);
$container->singleton(\App\Framework\Reflection\ReflectionProvider::class, $reflectionProvider);
$container->singleton(ExecutionContext::class, $forcedContext);
$container->singleton(InitializerProcessor::class, fn ($c) => new InitializerProcessor(
$c,
$c->get(\App\Framework\Reflection\ReflectionProvider::class),
$c->get(ExecutionContext::class)
));
// App config for testing - same as working console
$appConfig = new AppConfig(
environment: 'development', // Use development like console.php would
debug: true,
timezone: Timezone::UTC,
locale: 'en'
);
$container->singleton(AppConfig::class, $appConfig);
$cache->clear();
echo "1. Environment Setup:\n";
echo " - Forced Context: " . $forcedContext->getType()->value . "\n";
echo " - App Environment: development\n";
echo " - Source Path: " . $pathProvider->getSourcePath() . "\n\n";
echo "2. Discovery Test with Forced CLI Context:\n";
try {
$bootstrapper = new DiscoveryServiceBootstrapper($container, $clock);
echo " - Bootstrapper created\n";
$registry = $bootstrapper->performBootstrap($pathProvider, $cache, null);
echo " - Discovery completed\n";
echo " - Registry empty: " . ($registry->isEmpty() ? 'YES' : 'NO') . "\n";
echo " - Total attribute types: " . count($registry->attributes->getAllTypes()) . "\n";
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
echo " - ConsoleCommand attributes found: " . count($consoleCommands) . "\n";
if (count($consoleCommands) > 0) {
echo " - ✓ SUCCESS! Discovery is working with CLI context\n";
echo " - Sample commands:\n";
$sampleCount = 0;
foreach ($consoleCommands as $discovered) {
if ($sampleCount >= 5) {
break;
}
$command = $discovered->createAttributeInstance();
echo " * " . $command->name . " - " . $command->description . "\n";
$sampleCount++;
}
} else {
echo " - Still no commands found. All types:\n";
foreach ($registry->attributes->getAllTypes() as $type) {
$count = $registry->attributes->getCount($type);
echo " * $type: $count instances\n";
}
}
} catch (\Throwable $e) {
echo " - ❌ Error: " . $e->getMessage() . "\n";
echo " - File: " . $e->getFile() . ":" . $e->getLine() . "\n";
echo " - Stack trace:\n" . $e->getTraceAsString() . "\n";
}
echo "\n=== End Test ===\n";

View File

@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use App\Framework\Cache\Cache;
use App\Framework\Cache\CacheInitializer;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\Clock;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\SystemHighResolutionClock;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\DiscoveryServiceBootstrapper;
use App\Framework\Http\ResponseEmitter;
use App\Framework\Logging\DefaultLogger;
use App\Framework\Logging\Logger;
use App\Framework\Performance\Contracts\PerformanceCollectorInterface;
use App\Framework\Performance\EnhancedPerformanceCollector;
use App\Framework\Performance\MemoryMonitor;
echo "=== Discovery Test with Full Container Setup ===\n\n";
// Create container and setup exactly like ContainerBootstrapper does
$container = new DefaultContainer();
// Step 1: Add runtime instances exactly like ContainerBootstrapper->addRuntimeInstances()
$basePath = '/home/michael/dev/michaelschiemer';
// Create dependencies for performance collector (like console.php)
$clock = new SystemClock();
$highResClock = new SystemHighResolutionClock();
$memoryMonitor = new MemoryMonitor();
$collector = new EnhancedPerformanceCollector($clock, $highResClock, $memoryMonitor, enabled: false);
echo "1. Setting up runtime instances:\n";
$container->instance(Logger::class, new DefaultLogger());
$container->instance(PerformanceCollectorInterface::class, $collector);
// Get the cache instance to debug it
$cache = new CacheInitializer($collector, $container)();
$container->instance(Cache::class, $cache);
$pathProvider = new PathProvider($basePath);
$container->instance(PathProvider::class, $pathProvider);
$container->instance(ResponseEmitter::class, new ResponseEmitter());
$container->instance(Clock::class, $clock);
echo " - Runtime instances registered\n";
echo " - Cache type: " . get_class($cache) . "\n";
echo " - Base path: " . $basePath . "\n";
echo " - Source path: " . $pathProvider->getSourcePath() . "\n";
// Clear any existing cache
$cache->clear();
echo " - Cache cleared\n";
// Step 2: Now do discovery bootstrap exactly like ContainerBootstrapper->autowire()
echo "2. Running discovery bootstrap (like ContainerBootstrapper):\n";
try {
$discoveryBootstrapper = new DiscoveryServiceBootstrapper($container, $clock);
echo " - DiscoveryServiceBootstrapper created\n";
// Use the exact same call as ContainerBootstrapper->autowire()
$results = $discoveryBootstrapper->bootstrap();
echo " - Discovery bootstrap completed\n";
echo " - Registry empty: " . ($results->isEmpty() ? 'YES' : 'NO') . "\n";
echo " - Total attribute types: " . count($results->attributes->getAllTypes()) . "\n";
$consoleCommands = $results->attributes->get(ConsoleCommand::class);
echo " - ConsoleCommand attributes found: " . count($consoleCommands) . "\n";
if (count($consoleCommands) > 0) {
echo " - ✅ SUCCESS! Discovery is working with full setup\n";
echo " - Sample commands:\n";
$sampleCount = 0;
foreach ($consoleCommands as $discovered) {
if ($sampleCount >= 5) {
break;
}
$command = $discovered->createAttributeInstance();
echo " * " . $command->name . " - " . $command->description . "\n";
$sampleCount++;
}
} else {
echo " - ❌ Still no commands found. All discovered types:\n";
foreach ($results->attributes->getAllTypes() as $type) {
$count = $results->attributes->getCount($type);
echo " * $type: $count instances\n";
}
}
} catch (\Throwable $e) {
echo " - ❌ Error: " . $e->getMessage() . "\n";
echo " - File: " . $e->getFile() . ":" . $e->getLine() . "\n";
echo " - Stack trace:\n" . $e->getTraceAsString() . "\n";
}
echo "\n=== End Test ===\n";

View File

@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
// Replicate console.php exactly, but check discovery instead of running console
require_once __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../src/Framework/Debug/helpers.php';
use App\Framework\Console\ConsoleCommand;
use App\Framework\Core\AppBootstrapper;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\SystemHighResolutionClock;
use App\Framework\Discovery\Results\DiscoveryRegistry;
use App\Framework\Performance\EnhancedPerformanceCollector;
use App\Framework\Performance\MemoryMonitor;
echo "=== Discovery Test Exactly Like console.php ===\n\n";
// Create dependencies exactly like console.php
$clock = new SystemClock();
$highResClock = new SystemHighResolutionClock();
$memoryMonitor = new MemoryMonitor();
// Disable performance collection for CLI to prevent memory exhaustion during discovery
$collector = new EnhancedPerformanceCollector($clock, $highResClock, $memoryMonitor, enabled: false);
$bootstrapper = new AppBootstrapper(__DIR__ . '/../..', $collector, $memoryMonitor);
echo "1. Environment (like console.php):\n";
echo " - Base path: " . __DIR__ . '/../..' . "\n";
echo " - Working directory: " . getcwd() . "\n\n";
try {
echo "2. Bootstrap Console Application:\n";
$consoleApp = $bootstrapper->bootstrapConsole();
echo " - Console application created successfully\n";
// Get container and check discovery registry
$container = $consoleApp->getContainer();
if ($container->has(DiscoveryRegistry::class)) {
echo " - DiscoveryRegistry found in container\n";
$registry = $container->get(DiscoveryRegistry::class);
echo " - Registry empty: " . ($registry->isEmpty() ? 'YES' : 'NO') . "\n";
echo " - Total attribute types: " . count($registry->attributes->getAllTypes()) . "\n";
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
echo " - ConsoleCommand attributes found: " . count($consoleCommands) . "\n";
if (count($consoleCommands) > 0) {
echo " - ✅ SUCCESS! Discovery working exactly like console.php\n";
echo " - Sample commands:\n";
$sampleCount = 0;
foreach ($consoleCommands as $discovered) {
if ($sampleCount >= 10) {
break;
}
$command = $discovered->createAttributeInstance();
echo " * " . $command->name . " - " . $command->description . "\n";
$sampleCount++;
}
echo " ... and " . (count($consoleCommands) - 10) . " more\n";
} else {
echo " - ❌ Still no commands found\n";
echo " - All discovered attribute types:\n";
foreach ($registry->attributes->getAllTypes() as $type) {
$count = $registry->attributes->getCount($type);
echo " * $type: $count instances\n";
}
}
} else {
echo " - ❌ DiscoveryRegistry not found in container\n";
echo " - Available container bindings:\n";
// Can't easily list container bindings with DefaultContainer
}
} catch (\Throwable $e) {
echo " - ❌ Error: " . $e->getMessage() . "\n";
echo " - File: " . $e->getFile() . ":" . $e->getLine() . "\n";
echo " - Stack trace:\n" . $e->getTraceAsString() . "\n";
}
echo "\n=== End Test ===\n";

View File

@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
echo "Testing Discovery on single MCP command file...\n\n";
// Setup basic dependencies
$clock = new \App\Framework\DateTime\SystemClock();
$highResClock = new \App\Framework\DateTime\SystemHighResolutionClock();
$memoryMonitor = new \App\Framework\Performance\MemoryMonitor();
$collector = new \App\Framework\Performance\EnhancedPerformanceCollector(
$clock,
$highResClock,
$memoryMonitor,
enabled: false
);
$container = new \App\Framework\DI\DefaultContainer();
$cache = (new \App\Framework\Cache\CacheInitializer($collector, $container))();
// Setup required services
$pathProvider = new \App\Framework\Core\PathProvider('/var/www/html');
$reflectionProvider = new \App\Framework\Reflection\CachedReflectionProvider();
// Create discovery configuration
$config = \App\Framework\Discovery\ValueObjects\DiscoveryConfiguration::development();
// Add just the console command mapper
$consoleMapper = new \App\Framework\Console\ConsoleCommandMapper();
echo "Testing ConsoleCommandMapper directly on the MCP command file...\n";
// Test the mapper directly
$className = \App\Framework\Core\ValueObjects\ClassName::create('App\Framework\Mcp\Console\McpServerCommand');
$class = new \App\Framework\Reflection\WrappedReflectionClass($className);
$methods = $class->getMethods();
foreach ($methods as $method) {
$attributes = $method->getAttributes(\App\Framework\Console\ConsoleCommand::class);
echo "Method: " . $method->getName() . "\n";
echo "Attributes found: " . count($attributes) . "\n";
foreach ($attributes as $attribute) {
echo " Attribute: " . $attribute->getName() . "\n";
$instance = $attribute->newInstance();
echo " Instance: " . get_class($instance) . "\n";
// Test the mapper
$mappedResult = $consoleMapper->map($method, $instance);
if ($mappedResult) {
echo " ✅ Mapper result: " . json_encode($mappedResult, JSON_PRETTY_PRINT) . "\n";
} else {
echo " ❌ Mapper returned null\n";
}
}
echo "\n";
}
echo "Now testing full discovery on just this one file...\n";
// Create a minimal discovery service to test just this file
$discoveryService = new \App\Framework\Discovery\UnifiedDiscoveryService(
pathProvider: $pathProvider,
cache: $cache,
clock: $clock,
reflectionProvider: $reflectionProvider,
configuration: $config,
attributeMappers: [$consoleMapper],
targetInterfaces: []
);
// Test discovery on specific file
$filePath = \App\Framework\Filesystem\FilePath::fromString('/var/www/html/src/Framework/Mcp/Console/McpServerCommand.php');
try {
echo "Attempting to discover attributes in: " . $filePath->getPath() . "\n";
// Try to get the discovery to process this specific file
$result = $discoveryService->discover();
if ($result instanceof \App\Framework\Discovery\Results\DiscoveryRegistry) {
$consoleCommands = $result->attributes->get(\App\Framework\Console\ConsoleCommand::class);
echo "Console commands found: " . count($consoleCommands) . "\n";
foreach ($consoleCommands as $command) {
echo " - Command: " . json_encode($command->getArguments(), JSON_PRETTY_PRINT) . "\n";
}
}
} catch (Exception $e) {
echo "Discovery failed: " . $e->getMessage() . "\n";
echo "File: " . $e->getFile() . ":" . $e->getLine() . "\n";
}

View File

@@ -0,0 +1,114 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use App\Framework\Cache\Driver\InMemoryCache;
use App\Framework\Cache\GeneralCache;
use App\Framework\Config\AppConfig;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Context\ExecutionContext;
use App\Framework\Core\PathProvider;
use App\Framework\DateTime\SystemClock;
use App\Framework\DateTime\Timezone;
use App\Framework\DI\DefaultContainer;
use App\Framework\Discovery\DiscoveryServiceBootstrapper;
use App\Framework\Discovery\InitializerProcessor;
use App\Framework\Reflection\CachedReflectionProvider;
use App\Framework\Serializer\Php\PhpSerializer;
use App\Framework\Serializer\Php\PhpSerializerConfig;
echo "=== Discovery Test with Minimal Environment ===\n\n";
// Set minimal required environment variables for testing
$_ENV['DB_DATABASE'] = 'test_db';
$_ENV['DB_HOST'] = 'localhost';
$_ENV['DB_USER'] = 'test_user';
$_ENV['DB_PASS'] = 'test_pass';
$_ENV['APP_ENV'] = 'testing';
$_ENV['APP_DEBUG'] = 'true';
// Create container with minimal setup
$container = new DefaultContainer();
$cacheDriver = new InMemoryCache();
$serializer = new PhpSerializer(PhpSerializerConfig::safe());
$cache = new GeneralCache($cacheDriver, $serializer);
$clock = new SystemClock();
$pathProvider = new PathProvider('/home/michael/dev/michaelschiemer');
// Register dependencies
$container->singleton(\App\Framework\Cache\Cache::class, $cache);
$container->singleton(\App\Framework\DateTime\Clock::class, $clock);
$container->singleton(PathProvider::class, $pathProvider);
$reflectionProvider = new CachedReflectionProvider();
$executionContext = ExecutionContext::detect();
$container->singleton(\App\Framework\Reflection\ReflectionProvider::class, $reflectionProvider);
$container->singleton(ExecutionContext::class, $executionContext);
$container->singleton(InitializerProcessor::class, fn ($c) => new InitializerProcessor(
$c,
$c->get(\App\Framework\Reflection\ReflectionProvider::class),
$c->get(ExecutionContext::class)
));
// Simple app config for testing - don't use full Environment loading
$appConfig = new AppConfig(
environment: 'testing',
debug: true,
timezone: Timezone::UTC,
locale: 'en'
);
$container->singleton(AppConfig::class, $appConfig);
$cache->clear();
echo "1. Environment Setup:\n";
echo " - Context: " . $executionContext->getType()->value . "\n";
echo " - App Environment: testing\n";
echo " - Source Path: " . $pathProvider->getSourcePath() . "\n\n";
echo "2. Discovery Test:\n";
try {
$bootstrapper = new DiscoveryServiceBootstrapper($container, $clock);
echo " - Bootstrapper created\n";
$registry = $bootstrapper->performBootstrap($pathProvider, $cache, null);
echo " - Discovery completed\n";
echo " - Registry empty: " . ($registry->isEmpty() ? 'YES' : 'NO') . "\n";
echo " - Total attribute types: " . count($registry->attributes->getAllTypes()) . "\n";
$consoleCommands = $registry->attributes->get(ConsoleCommand::class);
echo " - ConsoleCommand attributes found: " . count($consoleCommands) . "\n";
if (count($consoleCommands) > 0) {
echo " - ✓ SUCCESS! Discovery is working\n";
echo " - Sample commands:\n";
$sampleCount = 0;
foreach ($consoleCommands as $discovered) {
if ($sampleCount >= 5) {
break;
}
$command = $discovered->createAttributeInstance();
echo " * " . $command->name . " - " . $command->description . "\n";
$sampleCount++;
}
} else {
echo " - Still no commands found. Checking all types:\n";
foreach ($registry->attributes->getAllTypes() as $type) {
$count = $registry->attributes->getCount($type);
echo " * $type: $count instances\n";
}
}
} catch (\Throwable $e) {
echo " - ❌ Error: " . $e->getMessage() . "\n";
echo " - File: " . $e->getFile() . ":" . $e->getLine() . "\n";
}
echo "\n=== End Test ===\n";