-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from pavog/multi-pattern-detector
Add MultiPatternDetector
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Aternos\Codex\Detective; | ||
|
||
/** | ||
* MultiPatternDetector can detect multiple patterns in a log and return true if all patterns are found | ||
*/ | ||
class MultiPatternDetector extends Detector | ||
{ | ||
protected array $patterns = []; | ||
|
||
/** | ||
* Add a pattern to the list of patterns to detect | ||
* | ||
* @param string $pattern | ||
* @return $this | ||
*/ | ||
public function addPattern(string $pattern): static | ||
{ | ||
$this->patterns[] = $pattern; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Detects if the log matches all patterns | ||
* | ||
* Returns true if all patterns are found, false otherwise | ||
* | ||
* @return bool|float | ||
*/ | ||
public function detect(): bool|float | ||
{ | ||
foreach ($this->patterns as $pattern) { | ||
if (preg_match($pattern, $this->getLogContent()) !== 1) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Aternos\Codex\Test\Tests\Detector; | ||
|
||
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() | ||
); | ||
} | ||
} |