178 lines
7.6 KiB
PHP
178 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace Aternos\Codex\Test\Tests\Analyser;
|
|
|
|
use Aternos\Codex\Analyser\PatternAnalyser;
|
|
use Aternos\Codex\Analysis\Analysis;
|
|
use Aternos\Codex\Analysis\PatternInsightInterface;
|
|
use Aternos\Codex\Log\Entry;
|
|
use Aternos\Codex\Log\File\PathLogFile;
|
|
use Aternos\Codex\Log\Level;
|
|
use Aternos\Codex\Log\Line;
|
|
use Aternos\Codex\Test\Src\Analysis\TestPatternInformation;
|
|
use Aternos\Codex\Test\Src\Analysis\TestPatternProblem;
|
|
use Aternos\Codex\Test\Src\Analysis\TestSolution;
|
|
use Aternos\Codex\Test\Src\Log\TestPatternLog;
|
|
use PHPUnit\Framework\TestCase;
|
|
use ReflectionClass;
|
|
|
|
class PatternAnalyserTest extends TestCase
|
|
{
|
|
/**
|
|
* @return Analysis
|
|
*/
|
|
protected function getExpectedAnalysis(): Analysis
|
|
{
|
|
return (new Analysis())
|
|
->addInsight((new TestPatternProblem())
|
|
->setCause("ABC")
|
|
->increaseCounter()
|
|
->setEntry((new Entry())->setTime(2)->setLevel(Level::ERROR)->setPrefix("[01.01.1970 00:00:02] [Log/ERROR]")
|
|
->addLine(new Line(2, "[01.01.1970 00:00:02] [Log/ERROR] I have a problem with ABC"))
|
|
)
|
|
)
|
|
->addInsight((new TestPatternProblem())
|
|
->setCause("XYZ")
|
|
->setEntry((new Entry())->setTime(4)->setLevel(Level::ERROR)->setPrefix("[01.01.1970 00:00:04] [Log/ERROR]")
|
|
->addLine(new Line(4, "[01.01.1970 00:00:04] [Log/ERROR] I have a problem with XYZ"))
|
|
)
|
|
)
|
|
->addInsight((new TestPatternProblem())
|
|
->setCause("DEF")
|
|
->setEntry((new Entry())->setTime(6)->setLevel(Level::ERROR)->setPrefix("[01.01.1970 00:00:06] [Log/ERROR]")
|
|
->addLine(new Line(6, "[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF"))
|
|
->addLine(new Line(7, "I have a problem with GHI"))
|
|
)
|
|
)
|
|
->addInsight((new TestPatternProblem())
|
|
->setCause("GHI")
|
|
->setEntry((new Entry())->setTime(6)->setLevel(Level::ERROR)->setPrefix("[01.01.1970 00:00:06] [Log/ERROR]")
|
|
->addLine(new Line(6, "[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF"))
|
|
->addLine(new Line(7, "I have a problem with GHI"))
|
|
)
|
|
)
|
|
->addInsight((new TestPatternInformation())
|
|
->setValue("v1.2.3")
|
|
->setEntry((new Entry())->setTime(7)->setLevel(Level::INFO)->setPrefix("[01.01.1970 00:00:07] [Log/INFO]")
|
|
->addLine(new Line(8, "[01.01.1970 00:00:07] [Log/INFO] This log was generated by software v1.2.3"))
|
|
)
|
|
);
|
|
}
|
|
|
|
public function testAnalyse(): void
|
|
{
|
|
$logFile = new PathLogFile(__DIR__ . '/../../data/problem.log');
|
|
$log = (new TestPatternLog())->setLogFile($logFile);
|
|
$log->parse();
|
|
|
|
$analysis = $log->analyse();
|
|
$this->assertJsonStringEqualsJsonString(json_encode($this->getExpectedAnalysis()->getInsights()), json_encode($analysis->getInsights()));
|
|
}
|
|
|
|
public function testAnalyseWithPossibleInsightClasses(): void
|
|
{
|
|
$logFile = new PathLogFile(__DIR__ . '/../../data/problem.log');
|
|
$log = (new TestPatternLog())->setLogFile($logFile);
|
|
$log->parse();
|
|
|
|
$analyser = (new PatternAnalyser())
|
|
->setPossibleInsightClasses([
|
|
TestPatternInformation::class,
|
|
TestPatternProblem::class
|
|
]);
|
|
|
|
$analysis = $log->analyse($analyser);
|
|
$this->assertJsonStringEqualsJsonString(json_encode($this->getExpectedAnalysis()->getInsights()), json_encode($analysis->getInsights()));
|
|
}
|
|
|
|
public function testAddPossibleInsightClassThrowsExceptionIfPossibleInsightClassDoesNotImplementPatternInsightInterface(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->expectExceptionMessage("Class " . TestSolution::class . " does not implement " . PatternInsightInterface::class . ".");
|
|
(new PatternAnalyser())->addPossibleInsightClass(TestSolution::class);
|
|
}
|
|
|
|
public function testRemovePossibleInsightClass(): void
|
|
{
|
|
$analyser = (new PatternAnalyser())
|
|
->setPossibleInsightClasses([
|
|
TestPatternInformation::class,
|
|
TestPatternProblem::class
|
|
]);
|
|
|
|
$reflector = new ReflectionClass(PatternAnalyser::class);
|
|
$possibleInsightClassesProperty = $reflector->getProperty('possibleInsightClasses');
|
|
|
|
$this->assertEquals([TestPatternInformation::class, TestPatternProblem::class], $possibleInsightClassesProperty->getValue($analyser));
|
|
|
|
$analyser->removePossibleInsightClass(TestPatternProblem::class);
|
|
|
|
$this->assertEquals([TestPatternInformation::class], $possibleInsightClassesProperty->getValue($analyser));
|
|
}
|
|
|
|
public function testRemovePossibleInsightClassThrowsExceptionIfPossibleInsightClassIsNotAdded(): void
|
|
{
|
|
// Set TestPatternProblem class
|
|
$analyser = (new PatternAnalyser())
|
|
->setPossibleInsightClasses([
|
|
TestPatternProblem::class
|
|
]);
|
|
|
|
$reflector = new ReflectionClass(PatternAnalyser::class);
|
|
$possibleInsightClassesProperty = $reflector->getProperty('possibleInsightClasses');
|
|
|
|
$this->assertEquals([TestPatternProblem::class], $possibleInsightClassesProperty->getValue($analyser));
|
|
|
|
// Remove TestPatternInformation class -> not found
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->expectExceptionMessage("Class " . TestPatternInformation::class . " not found in possible insight classes.");
|
|
$analyser->removePossibleInsightClass(TestPatternInformation::class);
|
|
|
|
$this->assertEquals([TestPatternInformation::class], $possibleInsightClassesProperty->getValue($analyser));
|
|
}
|
|
|
|
public function testOverridePossibleInsightClass(): void
|
|
{
|
|
$analyser = (new PatternAnalyser())
|
|
->setPossibleInsightClasses([
|
|
TestPatternProblem::class
|
|
]);
|
|
|
|
$reflector = new ReflectionClass(PatternAnalyser::class);
|
|
$possibleInsightClassesProperty = $reflector->getProperty('possibleInsightClasses');
|
|
|
|
$this->assertEquals([TestPatternProblem::class], $possibleInsightClassesProperty->getValue($analyser));
|
|
|
|
$childInsightClass = new class extends TestPatternProblem {
|
|
// Is empty child class
|
|
};
|
|
|
|
$analyser->overridePossibleInsightClass(TestPatternProblem::class, get_class($childInsightClass));
|
|
|
|
$this->assertEquals([get_class($childInsightClass)], $possibleInsightClassesProperty->getValue($analyser));
|
|
}
|
|
|
|
public function testOverridePossibleInsightClassThrowsExceptionIfClassDoesNotExtendParent(): void
|
|
{
|
|
$analyser = (new PatternAnalyser())
|
|
->setPossibleInsightClasses([
|
|
TestPatternProblem::class
|
|
]);
|
|
|
|
$reflector = new ReflectionClass(PatternAnalyser::class);
|
|
$possibleInsightClassesProperty = $reflector->getProperty('possibleInsightClasses');
|
|
|
|
$this->assertEquals([TestPatternProblem::class], $possibleInsightClassesProperty->getValue($analyser));
|
|
|
|
$childInsightClass = new class {
|
|
// Is empty and not a child class
|
|
};
|
|
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
$this->expectExceptionMessage("Class " . get_class($childInsightClass) . " does not extend " . TestPatternProblem::class . ".");
|
|
$analyser->overridePossibleInsightClass(TestPatternProblem::class, get_class($childInsightClass));
|
|
|
|
$this->assertEquals([TestPatternProblem::class], $possibleInsightClassesProperty->getValue($analyser));
|
|
}
|
|
}
|