Files
michaelschiemer/tests/debug/test-aggregator-internals.php

134 lines
4.9 KiB
PHP

<?php
declare(strict_types=1);
require __DIR__ . '/../../vendor/autoload.php';
use App\Framework\Core\ValueObjects\Timestamp;
use App\Framework\DateTime\Clock;
use App\Framework\ErrorAggregation\ErrorEvent;
use App\Framework\ErrorAggregation\ErrorPattern;
use App\Framework\ErrorAggregation\Storage\InMemoryErrorStorage;
use App\Framework\Exception\Core\DatabaseErrorCode;
use App\Framework\Exception\ErrorHandlerContext;
use App\Framework\Exception\ExceptionContext;
use App\Framework\Exception\FrameworkException;
use App\Framework\Exception\RequestContext;
use App\Framework\Exception\SystemContext;
// Create test clock
$clock = new class implements Clock {
public function now(): \DateTimeImmutable { return new \DateTimeImmutable(); }
public function fromTimestamp(Timestamp $timestamp): \DateTimeImmutable { return $timestamp->toDateTime(); }
public function fromString(string $dateTime, ?string $format = null): \DateTimeImmutable { return new \DateTimeImmutable($dateTime); }
public function today(): \DateTimeImmutable { return new \DateTimeImmutable('today'); }
public function yesterday(): \DateTimeImmutable { return new \DateTimeImmutable('yesterday'); }
public function tomorrow(): \DateTimeImmutable { return new \DateTimeImmutable('tomorrow'); }
public function time(): Timestamp { return Timestamp::now(); }
};
echo "Test 1: Direct ErrorPattern creation\n";
echo "=====================================\n";
// Create ErrorEvent manually
$exception = FrameworkException::create(
DatabaseErrorCode::QUERY_FAILED,
'Test database query failed'
);
$exceptionContext = ExceptionContext::empty()
->withOperation('test_operation', 'TestComponent')
->withData([
'original_exception' => $exception,
'exception_message' => $exception->getMessage()
]);
$requestContext = RequestContext::fromGlobals();
$systemContext = SystemContext::current();
$errorHandlerContext = ErrorHandlerContext::create(
$exceptionContext,
$requestContext,
$systemContext,
['http_status' => 500]
);
$errorEvent = ErrorEvent::fromErrorHandlerContext($errorHandlerContext, $clock);
echo "ErrorEvent created:\n";
echo " Component: {$errorEvent->component}\n";
echo " Operation: {$errorEvent->operation}\n";
echo " Message: {$errorEvent->errorMessage}\n";
echo " Severity: {$errorEvent->severity->value}\n";
echo " Error Code: {$errorEvent->errorCode->value}\n";
echo "\nAttempting to create ErrorPattern from ErrorEvent...\n";
try {
$pattern = ErrorPattern::fromErrorEvent($errorEvent, $clock);
echo "SUCCESS: Pattern created!\n";
echo " Pattern ID: " . (string) $pattern->id . "\n";
echo " Fingerprint: {$pattern->fingerprint}\n";
echo " Component: {$pattern->component}\n";
echo " Occurrences: {$pattern->occurrenceCount}\n";
echo " Is Active: " . ($pattern->isActive ? 'true' : 'false') . "\n";
echo " Severity: {$pattern->severity->value}\n";
} catch (\Throwable $e) {
echo "ERROR: Failed to create pattern: " . $e->getMessage() . "\n";
echo " Exception class: " . get_class($e) . "\n";
echo " Stack trace:\n";
echo $e->getTraceAsString() . "\n";
}
echo "\n\nTest 2: Direct storage test\n";
echo "============================\n";
$storage = new InMemoryErrorStorage();
echo "Storing event...\n";
try {
$storage->storeEvent($errorEvent);
echo "Event stored successfully\n";
} catch (\Throwable $e) {
echo "ERROR storing event: " . $e->getMessage() . "\n";
}
if (isset($pattern)) {
echo "\nStoring pattern...\n";
try {
$storage->storePattern($pattern);
echo "Pattern stored successfully\n";
} catch (\Throwable $e) {
echo "ERROR storing pattern: " . $e->getMessage() . "\n";
echo " Exception class: " . get_class($e) . "\n";
echo " Stack trace:\n";
echo $e->getTraceAsString() . "\n";
}
echo "\nRetrieving patterns from storage...\n";
$patterns = $storage->getActivePatterns(10);
echo "Active patterns count: " . count($patterns) . "\n";
if (count($patterns) === 0) {
echo "ERROR: Pattern was stored but getActivePatterns() returns 0\n";
// Direct check
$reflectionClass = new ReflectionClass($storage);
$patternsProperty = $reflectionClass->getProperty('patterns');
$patternsArray = $patternsProperty->getValue($storage);
echo "Raw patterns array count: " . count($patternsArray) . "\n";
if (count($patternsArray) > 0) {
echo "Patterns exist! Checking filter conditions...\n";
foreach ($patternsArray as $patternId => $storedPattern) {
echo " Pattern {$patternId}:\n";
echo " isActive: " . ($storedPattern->isActive ? 'true' : 'false') . "\n";
echo " Occurrences: {$storedPattern->occurrenceCount}\n";
}
}
} else {
echo "SUCCESS: Pattern retrieved from storage\n";
}
}