test: add comprehensive tests for Discovery module components
- Add tests for Results registries (AttributeRegistry, InterfaceRegistry, TemplateRegistry) - Add tests for Processing components (ProcessingContext) - Add tests for Memory components (MemoryGuard) - Add tests for Value Objects (DiscoveryOptions, DiscoveryContext) All new tests pass and cover core functionality including: - Registry operations (add, get, has, serialize/deserialize, optimize) - Processing context (reflection caching, file context management) - Memory guard (memory checks, statistics, emergency cleanup) - Value objects (factory methods, scan types, cache keys, metrics)
This commit is contained in:
108
tests/Framework/Discovery/Processing/ProcessingContextTest.php
Normal file
108
tests/Framework/Discovery/Processing/ProcessingContextTest.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Framework\Discovery\Processing;
|
||||
|
||||
use App\Framework\Core\ValueObjects\ClassName;
|
||||
use App\Framework\Discovery\Processing\ProcessingContext;
|
||||
use App\Framework\Discovery\ValueObjects\FileContext;
|
||||
use App\Framework\Filesystem\File;
|
||||
use App\Framework\Filesystem\ValueObjects\FilePath;
|
||||
use App\Framework\ReflectionLegacy\CachedReflectionProvider;
|
||||
use App\Framework\ReflectionLegacy\ReflectionProvider;
|
||||
|
||||
describe('ProcessingContext', function () {
|
||||
beforeEach(function () {
|
||||
$this->reflectionProvider = new CachedReflectionProvider();
|
||||
$this->context = new ProcessingContext($this->reflectionProvider);
|
||||
});
|
||||
|
||||
it('can be instantiated', function () {
|
||||
expect($this->context)->toBeInstanceOf(ProcessingContext::class);
|
||||
});
|
||||
|
||||
it('can set current file context', function () {
|
||||
$file = File::fromPath(FilePath::create('/test/file.php'));
|
||||
$fileContext = FileContext::fromFile($file);
|
||||
|
||||
$this->context->setCurrentFile($fileContext);
|
||||
|
||||
$current = $this->context->getCurrentFileContext();
|
||||
expect($current)->toBeInstanceOf(FileContext::class);
|
||||
expect($current)->toBe($fileContext);
|
||||
});
|
||||
|
||||
it('can get reflection for a class', function () {
|
||||
$className = ClassName::create('stdClass');
|
||||
|
||||
$reflection = $this->context->getReflection($className);
|
||||
|
||||
// stdClass should be reflectable
|
||||
expect($reflection)->not->toBeNull();
|
||||
});
|
||||
|
||||
it('returns null for non-existent class', function () {
|
||||
$className = ClassName::create('NonExistent\\Class\\That\\Does\\Not\\Exist');
|
||||
|
||||
$reflection = $this->context->getReflection($className);
|
||||
|
||||
expect($reflection)->toBeNull();
|
||||
});
|
||||
|
||||
it('caches reflection for the same class', function () {
|
||||
$className = ClassName::create('stdClass');
|
||||
|
||||
$reflection1 = $this->context->getReflection($className);
|
||||
$reflection2 = $this->context->getReflection($className);
|
||||
|
||||
// Should return the same instance (cached)
|
||||
expect($reflection1)->toBe($reflection2);
|
||||
});
|
||||
|
||||
it('clears reflection cache when switching files', function () {
|
||||
$file1 = File::fromPath(FilePath::create('/test/file1.php'));
|
||||
$fileContext1 = FileContext::fromFile($file1);
|
||||
$this->context->setCurrentFile($fileContext1);
|
||||
|
||||
$className = ClassName::create('stdClass');
|
||||
$reflection1 = $this->context->getReflection($className);
|
||||
|
||||
// Switch to different file
|
||||
$file2 = File::fromPath(FilePath::create('/test/file2.php'));
|
||||
$fileContext2 = FileContext::fromFile($file2);
|
||||
$this->context->setCurrentFile($fileContext2);
|
||||
|
||||
// Reflection should be cleared
|
||||
$reflection2 = $this->context->getReflection($className);
|
||||
|
||||
// Should still work, but might be a new instance
|
||||
expect($reflection2)->not->toBeNull();
|
||||
});
|
||||
|
||||
it('can cleanup resources', function () {
|
||||
$file = File::fromPath(FilePath::create('/test/file.php'));
|
||||
$fileContext = FileContext::fromFile($file);
|
||||
$this->context->setCurrentFile($fileContext);
|
||||
|
||||
$className = ClassName::create('stdClass');
|
||||
$this->context->getReflection($className);
|
||||
|
||||
$this->context->cleanup();
|
||||
|
||||
// After cleanup, current file context should be null
|
||||
$current = $this->context->getCurrentFileContext();
|
||||
expect($current)->toBeNull();
|
||||
});
|
||||
|
||||
it('handles reflection errors gracefully', function () {
|
||||
// Use a class that might cause reflection errors
|
||||
$className = ClassName::create('NonExistent\\Class\\With\\Parse\\Error');
|
||||
|
||||
$reflection = $this->context->getReflection($className);
|
||||
|
||||
// Should return null instead of throwing
|
||||
expect($reflection)->toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user