Initial import from aternosorg/codex-minecraft
This commit is contained in:
122
test/tests/Detective/DetectiveTest.php
Normal file
122
test/tests/Detective/DetectiveTest.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Aternos\Codex\Test\Tests\Detective;
|
||||
|
||||
use Aternos\Codex\Detective\Detective;
|
||||
use Aternos\Codex\Detective\DetectorInterface;
|
||||
use Aternos\Codex\Log\DetectableLogInterface;
|
||||
use Aternos\Codex\Log\Log;
|
||||
use Aternos\Codex\Test\Src\Analysis\TestSolution;
|
||||
use Aternos\Codex\Test\Src\Log\TestAlwaysDetectableLog;
|
||||
use Aternos\Codex\Test\Src\Log\TestLessDetectableLog;
|
||||
use Aternos\Codex\Test\Src\Log\TestMoreDetectableLog;
|
||||
use Aternos\Codex\Test\Src\Log\TestNeverDetectableLog;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DetectiveTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param array $possibleLogClasses
|
||||
* @return Detective
|
||||
*/
|
||||
protected function getDetective(array $possibleLogClasses): Detective
|
||||
{
|
||||
$detective = new Detective();
|
||||
$detective->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'));
|
||||
$detective->setPossibleLogClasses($possibleLogClasses);
|
||||
return $detective;
|
||||
}
|
||||
|
||||
public function testDetect(): void
|
||||
{
|
||||
$this->assertEquals(TestAlwaysDetectableLog::class,
|
||||
get_class($this->getDetective([
|
||||
TestAlwaysDetectableLog::class,
|
||||
TestLessDetectableLog::class,
|
||||
TestMoreDetectableLog::class,
|
||||
TestNeverDetectableLog::class])->detect()));
|
||||
|
||||
$this->assertEquals(TestMoreDetectableLog::class,
|
||||
get_class($this->getDetective([
|
||||
TestLessDetectableLog::class,
|
||||
TestMoreDetectableLog::class,
|
||||
TestNeverDetectableLog::class])->detect()));
|
||||
|
||||
$this->assertEquals(TestLessDetectableLog::class,
|
||||
get_class($this->getDetective([
|
||||
TestLessDetectableLog::class,
|
||||
TestNeverDetectableLog::class])->detect()));
|
||||
|
||||
$this->assertEquals(Log::class,
|
||||
get_class($this->getDetective([
|
||||
TestNeverDetectableLog::class])->detect()));
|
||||
}
|
||||
|
||||
public function testAddPossibleLogClassThrowsExceptionIfPossibleClassDoesNotImplementDetectableLogInterface(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage("Class " . TestSolution::class . " does not implement " . DetectableLogInterface::class . ".");
|
||||
(new Detective())->addPossibleLogClass(TestSolution::class);
|
||||
}
|
||||
|
||||
public function testDetectThrowsExceptionIfDetectorClassDoesNotImplementDetectorInterface(): void
|
||||
{
|
||||
$invalidDetectorClass = new class {
|
||||
// Is empty and not a child class of DetectorInterface
|
||||
};
|
||||
|
||||
$customLogClass = new class() extends Log implements DetectableLogInterface {
|
||||
private static array $detectors = [];
|
||||
|
||||
public static function setDetectors($detectors): void
|
||||
{
|
||||
self::$detectors = $detectors;
|
||||
}
|
||||
|
||||
public static function getDetectors(): array
|
||||
{
|
||||
return self::$detectors;
|
||||
}
|
||||
};
|
||||
|
||||
$customLogClass::setDetectors([$invalidDetectorClass]);
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage("Class " . get_class($invalidDetectorClass) . " does not implement " . DetectorInterface::class . ".");
|
||||
$this->getDetective([get_class($customLogClass)])->detect();
|
||||
}
|
||||
|
||||
public function testGetPossibleLogClasses(): void
|
||||
{
|
||||
$possibleLogClasses = [
|
||||
TestAlwaysDetectableLog::class,
|
||||
TestLessDetectableLog::class,
|
||||
TestMoreDetectableLog::class,
|
||||
TestNeverDetectableLog::class
|
||||
];
|
||||
|
||||
$detective = $this->getDetective($possibleLogClasses);
|
||||
|
||||
$this->assertEquals($possibleLogClasses, $detective->getPossibleLogClasses());
|
||||
}
|
||||
|
||||
public function testAddDetective(): void
|
||||
{
|
||||
$possibleLogClasses1 = [
|
||||
TestAlwaysDetectableLog::class,
|
||||
TestLessDetectableLog::class
|
||||
];
|
||||
|
||||
$possibleLogClasses2 = [
|
||||
TestMoreDetectableLog::class,
|
||||
TestNeverDetectableLog::class
|
||||
];
|
||||
|
||||
$detective1 = $this->getDetective($possibleLogClasses1);
|
||||
$detective2 = $this->getDetective($possibleLogClasses2);
|
||||
|
||||
$detective1->addDetective($detective2);
|
||||
|
||||
$this->assertEquals(array_merge($possibleLogClasses1, $possibleLogClasses2), $detective1->getPossibleLogClasses());
|
||||
}
|
||||
}
|
||||
30
test/tests/Detective/LinePatternDetectorTest.php
Normal file
30
test/tests/Detective/LinePatternDetectorTest.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Aternos\Codex\Test\Tests\Detective;
|
||||
|
||||
use Aternos\Codex\Detective\LinePatternDetector;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LinePatternDetectorTest extends TestCase
|
||||
{
|
||||
public function testDetect(): void
|
||||
{
|
||||
$this->assertEquals(5 / 7, (new LinePatternDetector())
|
||||
->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'))
|
||||
->setPattern('/information/')
|
||||
->detect()
|
||||
);
|
||||
|
||||
$this->assertFalse((new LinePatternDetector())
|
||||
->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'))
|
||||
->setPattern('/missing/')
|
||||
->detect()
|
||||
);
|
||||
|
||||
$this->assertEquals(1, (new LinePatternDetector())
|
||||
->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'))
|
||||
->setPattern('/This/')
|
||||
->detect()
|
||||
);
|
||||
}
|
||||
}
|
||||
40
test/tests/Detective/MultiPatternDetectorTest.php
Normal file
40
test/tests/Detective/MultiPatternDetectorTest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Aternos\Codex\Test\Tests\Detective;
|
||||
|
||||
use Aternos\Codex\Detective\MultiPatternDetector;
|
||||
use Aternos\Codex\Log\File\StringLogFile;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MultiPatternDetectorTest extends TestCase
|
||||
{
|
||||
public function testDetectSinglePattern(): void
|
||||
{
|
||||
$this->assertTrue((new MultiPatternDetector())
|
||||
->setLogFile(new StringLogFile("You can detect this."))
|
||||
->addPattern('/detect/')
|
||||
->detect()
|
||||
);
|
||||
}
|
||||
|
||||
public function testDetectMultiplePatterns(): void
|
||||
{
|
||||
$this->assertTrue((new MultiPatternDetector())
|
||||
->setLogFile(new StringLogFile("You can detect this and this."))
|
||||
->addPattern('/detect/')
|
||||
->addPattern('/and this/')
|
||||
->detect()
|
||||
);
|
||||
}
|
||||
|
||||
public function testNotDetectMissingFromMultiplePatterns(): void
|
||||
{
|
||||
$this->assertFalse((new MultiPatternDetector())
|
||||
->setLogFile(new StringLogFile("You can detect this and this."))
|
||||
->addPattern('/detect/')
|
||||
->addPattern('/and this/')
|
||||
->addPattern('/but not this/')
|
||||
->detect()
|
||||
);
|
||||
}
|
||||
}
|
||||
25
test/tests/Detective/SinglePatternDetectorTest.php
Normal file
25
test/tests/Detective/SinglePatternDetectorTest.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Aternos\Codex\Test\Tests\Detective;
|
||||
|
||||
use Aternos\Codex\Detective\SinglePatternDetector;
|
||||
use Aternos\Codex\Log\File\StringLogFile;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SinglePatternDetectorTest extends TestCase
|
||||
{
|
||||
public function testDetect(): void
|
||||
{
|
||||
$this->assertTrue((new SinglePatternDetector())
|
||||
->setLogFile(new StringLogFile("You can detect this."))
|
||||
->setPattern('/detect/')
|
||||
->detect()
|
||||
);
|
||||
|
||||
$this->assertFalse((new SinglePatternDetector())
|
||||
->setLogFile(new StringLogFile("You cannot detect this."))
|
||||
->setPattern('/missing/')
|
||||
->detect()
|
||||
);
|
||||
}
|
||||
}
|
||||
40
test/tests/Detective/WeightedSinglePatternDetectorTest.php
Normal file
40
test/tests/Detective/WeightedSinglePatternDetectorTest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Aternos\Codex\Test\Tests\Detective;
|
||||
|
||||
use Aternos\Codex\Detective\WeightedSinglePatternDetector;
|
||||
use Aternos\Codex\Log\File\StringLogFile;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class WeightedSinglePatternDetectorTest extends TestCase
|
||||
{
|
||||
public function testDetect(): void
|
||||
{
|
||||
$this->assertEquals(1, (new WeightedSinglePatternDetector())
|
||||
->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'))
|
||||
->setPattern('/This/')
|
||||
->setWeight(1)
|
||||
->detect()
|
||||
);
|
||||
|
||||
$this->assertEquals(0.5, (new WeightedSinglePatternDetector())
|
||||
->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'))
|
||||
->setPattern('/This/')
|
||||
->setWeight(0.5)
|
||||
->detect()
|
||||
);
|
||||
|
||||
$this->assertEquals(0, (new WeightedSinglePatternDetector())
|
||||
->setLogFile(new \Aternos\Codex\Log\File\PathLogFile(__DIR__ . '/../../data/simple.log'))
|
||||
->setPattern('/This/')
|
||||
->setWeight(0)
|
||||
->detect()
|
||||
);
|
||||
|
||||
$this->assertFalse((new WeightedSinglePatternDetector())
|
||||
->setLogFile(new StringLogFile("You cannot detect this."))
|
||||
->setPattern('/missing/')
|
||||
->detect()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user