Skip to content

Commit

Permalink
Add phpunit test suite.
Browse files Browse the repository at this point in the history
  • Loading branch information
vielhuber committed Sep 15, 2021
1 parent f66c5ae commit 8d17d7e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.DS_Store
/vendor
/composer.lock
/.phpunit.result.cache
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}
20 changes: 20 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
stopOnFailure="false"
backupGlobals="false"
>
<testsuites>
<testsuite
name="testsuite"
>
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
</php>
</phpunit>
55 changes: 55 additions & 0 deletions tests/Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
class Test extends \PHPUnit\Framework\TestCase
{
protected $blacklist = '';

protected function setUp(): void
{
$filename = __DIR__.'/../blacklist.txt';
if(file_exists($filename)) {
$this->blacklist = file_get_contents($filename);
}
}

public function testFalsePositives()
{
$this->runTestInFolder('false_positives');
}

public function testFalseNegatives() {
$this->runTestInFolder('false_negatives');
}

private function runTestInFolder($folder) {
$files = $this->getFilesInFolder($folder);
foreach($files as $files__value) {
$file_content = file_get_contents(__DIR__ . '/' . $folder . '/' . $files__value);
foreach(explode("\n", $file_content) as $file_content__value) {
if( $file_content__value == '' ) { continue; }
$match = null;
foreach(explode("\n", $this->blacklist) as $blacklist__value) {
if( $blacklist__value == '' ) { continue; }
$pattern = sprintf('#%s#i', preg_quote($blacklist__value, '#'));
if( preg_match($pattern, $file_content__value) ) {
$match = ['blacklist' => $blacklist__value, 'string' => $file_content__value];
break;
}
}
if( $folder === 'false_positives' && $match !== null ) {
$this->assertEquals('"'.$match['string'].'" => "'.$match['blacklist']. '"', 'FALSELY DETECTED AS SPAM');
}
else if( $folder === 'false_negatives' && $match === null ) {
$this->assertEquals($file_content__value, 'FALSELY NOT DETECTED AS SPAM');
}
else {
$this->assertTrue(true);
}
}
}
}

private function getFilesInFolder($folder) {
if( !is_string($folder) || $folder == '' || !is_dir(__DIR__ . '/' . $folder) ) { return []; }
return array_diff(scandir(__DIR__ . '/' . $folder), ['.', '..']);
}
}
2 changes: 2 additions & 0 deletions tests/false_negatives/2021-09-15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
loli*ta gi*rl fu*ck c*p pt*hc
I do spy software your website.
12 changes: 12 additions & 0 deletions tests/false_positives/2021-09-15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
foobar@icloud.com
foobar@yahoo.com
foobar@1und1.de
foobar@ionos.de
foobar@gmail.com
foobar@freenet.de
foobar@aol.com
foobar@outlook.de
foobar@t-online.de
foobar@web.de
foobar@gmx.de
foobar@gmx.net

0 comments on commit 8d17d7e

Please sign in to comment.