8000 Improve testcoverage by pavog · Pull Request #12 · aternosorg/codex · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve testcoverage #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Analysis/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function setInsights(array $insights = [])
}

/**
* Add an insight
* Add an insight.
* If the insight already exists, we increase its counter.
*
* @param InsightInterface $insight
* @return $this
Expand Down
118 changes: 115 additions & 3 deletions test/tests/Analyser/PatternAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

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\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
{
Expand All @@ -18,7 +22,7 @@ class PatternAnalyserTest extends TestCase
*/
protected function getExpectedAnalysis(): Analysis
{
$analysis = (new Analysis())
return (new Analysis())
->addInsight((new TestPatternProblem())
->setCause("ABC")
->increaseCounter()
Expand Down Expand Up @@ -52,8 +56,6 @@ protected function getExpectedAnalysis(): Analysis
->addLine(new Line(8, "[01.01.1970 00:00:07] [Log/INFO] This log was generated by software v1.2.3"))
)
);

return $analysis;
}

public function testAnalyse(): void
Expand All @@ -65,4 +67,114 @@ public function testAnalyse(): void
$analysis = $log->analyse();
$this->assertEquals($this->getExpectedAnalysis()->getInsights(), $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->assertEquals($this->getExpectedAnalysis()->getInsights(), $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');
$possibleInsightClassesProperty->setAccessible(true);

$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');
$possibleInsightClassesProperty->setAccessible(true);

$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');
$possibleInsightClassesProperty->setAccessible(true);

$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');
$possibleInsightClassesProperty->setAccessible(true);

$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));
}
}
107 changes: 107 additions & 0 deletions test/tests/Analysis/AnalysisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Aternos\Codex\Analysis\Analysis;
use Aternos\Codex\Test\Src\Analysis\TestInformation;
use Aternos\Codex\Test\Src\Analysis\TestInsight;
use Aternos\Codex\Test\Src\Analysis\TestPatternProblem;
use Aternos\Codex\Test\Src\Analysis\TestProblem;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -49,4 +50,110 @@ public function testGetInformation(): void

$this->assertEquals([$information], $analysis->getInformation());
}

public function testKey(): void
{
$analysis = new Analysis();
$problem = new TestProblem();
$information = new TestInformation();

$analysis->addInsight($problem);
$this->assertEquals(0, $analysis->key());
$analysis->addInsight($information);
$this->assertEquals(1, $analysis->key());
}


public function testCount(): void
{
$analysis = new Analysis();
$problem = new TestProblem();
$information = new TestInformation();

$this->assertEquals(0, $analysis->count());
$analysis->addInsight($problem);
$this->assertEquals(1, $analysis->count());
$analysis->addInsight($information);
$this->assertEquals(2, $analysis->count());
}

public function testAddingTheSameInsightIncreasesInternalCounter(): void
{
// Adding the same insight to an analysis does not add it to the insights, and therefore it
// does not increase the counter of the analysis, but the internal counter of the insight.
// See Analysis->addInsight()

$analysis = new Analysis();
$problem = new TestPatternProblem();
$problem2 = new TestPatternProblem();

$analysis->addInsight($problem);
$this->assertEquals(1, $analysis->count());
$this->assertEquals(1, $problem->getCounterValue());

$analysis->addInsight($problem2);
$this->assertEquals(1, $analysis->count());
$this->assertEquals(2, $problem->getCounterValue());
}

public function testOffsetExists(): void
{
$analysis = new Analysis();
$information = new TestInformation();

$this->assertArrayNotHasKey(0, $analysis);
$this->assertEquals(0, $analysis->count());
$analysis->addInsight($information);
$this->assertArrayHasKey(0, $analysis);
$this->assertEquals($information, $analysis[0]);
}

public function testOffsetGet(): void
{
$analysis = new Analysis();
$information = new TestInformation();
$analysis->addInsight($information);

// Exists
$this->assertEquals($information, $analysis[0]);

// Does not exist -> "undefined array key" error
$this->expectError();
$this->assertEquals(null, $analysis[1]);
}

public function testOffsetSet(): void
{
$analysis = new Analysis();
$information = new TestInform 9E7A ation();

$this->assertArrayNotHasKey(0, $analysis);
$this->assertEquals(0, $analysis->count());
$analysis->addInsight($information);
$this->assertArrayHasKey(0, $analysis);
$this->assertEquals($information, $analysis[0]);

// Overwrite $information on $analysis[0] using the offsetSet
$problem = new TestProblem();
$analysis[0] = $problem;
$this->assertEquals($problem, $analysis[0]);
}

public function testOffsetUnset(): void
{
$analysis = new Analysis();
$information = new TestInformation();

$this->assertArrayNotHasKey(0, $analysis);
$this->assertEquals(0, $analysis->count());
$analysis->addInsight($information);
$this->assertArrayHasKey(0, $analysis);
$this->assertEquals($information, $analysis[0]);

// Unset $information on $analysis[0] using the offsetUnset
unset($analysis[0]);
$this->assertArrayNotHasKey(0, $analysis);
$this->expectError();
$this->assertEquals(null, $analysis[1]);
}
}
87 changes: 87 additions & 0 deletions test/tests/Analysis/ProblemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,91 @@ public function testAddSolutions(): void
$this->assertSame($problem, $problem->addSolution($solution));
$this->assertEquals([$solution], $problem->getSolutions());
}

public function testKey(): void
{
$problem = new TestProblem();
$solution = new TestSolution();

$problem->addSolution($solution);
foreach ($problem as $ignored) {
// do nothing
}
$this->assertEquals(1, $problem->key());
}


public function testCount(): void
{
$problem = new TestProblem();
$solution1 = new TestSolution();
$solution2 = new TestSolution();

$this->assertEquals(0, $problem->count());
$problem->addSolution($solution1);
$this->assertEquals(1, $problem->count());
$problem->addSolution($solution2);
$this->assertEquals(2, $problem->count());
}

public function testOffsetExists(): void
{
$problem = new TestProblem();
$solution = new TestSolution();

$this->assertArrayNotHasKey(0, $problem);
$this->assertEquals(0, $problem->count());
$problem->addSolution($solution);
$this->assertArrayHasKey(0, $problem);
$this->assertEquals($solution, $problem[0]);
}

public function testOffsetGet(): void
{
$problem = new TestProblem();
$solution = new TestSolution();
$problem->addSolution($solution);

// Exists
$this->assertEquals($solution, $problem[0]);

// Does not exist -> "undefined array key" error
$this->expectError();
$this->assertEquals(null, $problem[1]);
}

public function testOffsetSet(): void
{
$problem = new TestProblem();
$solution1 = new TestSolution();

$this->assertArrayNotHasKey(0, $problem);
$this->assertEquals(0, $problem->count());
$problem->addSolution($solution1);
$this->assertArrayHasKey(0, $problem);
$this->assertEquals($solution1, $problem[0]);

// Overwrite $solution1 on $problem[0] using the offsetSet
$TestSolution2 = new TestSolution();
$problem[0] = $TestSolution2;
$this->assertEquals($TestSolution2, $problem[0]);
}

public function testOffsetUnset(): void
{
$problem = new TestProblem();
$solution = new TestSolution();

$this->assertArrayNotHasKey(0, $problem);
$this->assertEquals(0, $problem->count());
$problem->addSolution($solution);
$this->assertArrayHasKey(0, $problem);
$this->assertEquals($solution, $problem[0]);

// Unset $solution on $problem[0] using the offsetUnset
unset($problem[0]);
$this->assertArrayNotHasKey(0, $problem);
$this->expectError();
$this->assertEquals(null, $problem[1]);
}
}
Loading
0