From ebc697c2df506552da8a3942dc9aac3f6906fedb Mon Sep 17 00:00:00 2001 From: Peter Maselkowski Date: Mon, 18 Sep 2017 11:22:29 +0200 Subject: [PATCH 1/6] ^ dep --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3007985..d068f17 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "php": ">=5.3.0" }, "require-dev": { - "phpunit/phpunit": "~4.3.0" + "phpunit/phpunit": "~6" }, "autoload": { "classmap" : ["src/Spamassassin/"] From 8115f922ebe7b9f5f71bdd28a3cee0cb436684d7 Mon Sep 17 00:00:00 2001 From: Peter Maselkowski Date: Mon, 18 Sep 2017 11:58:41 +0200 Subject: [PATCH 2/6] >> Made compatible with php unit 6 --- src/Spamassassin/Client.php | 6 ++++++ tests/BaseTestCase.php | 6 +++++- tests/LearnDisabledTest.php | 4 ++-- tests/MaxSizeTest.php | 2 +- tests/ZlibCompressionTest.php | 1 + 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Spamassassin/Client.php b/src/Spamassassin/Client.php index 57549e2..f7e818d 100644 --- a/src/Spamassassin/Client.php +++ b/src/Spamassassin/Client.php @@ -24,6 +24,7 @@ class Client protected $hostname = 'localhost'; protected $port = '783'; + protected $timeout = null; protected $socketPath; protected $socket; @@ -60,6 +61,11 @@ protected function getSocket() $socket = fsockopen($this->hostname, $this->port, $errno, $errstr); } + if(!empty($this->timeout)) + { + stream_set_timeout($socket, $this->timeout); + } + if (!$socket) { throw new Exception( "Could not connect to SpamAssassin: {$errstr}", $errno diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index c5b5407..c6b46e8 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -1,7 +1,9 @@ params = $params; $this->sa = new Client($params); diff --git a/tests/LearnDisabledTest.php b/tests/LearnDisabledTest.php index 378fd09..dc08c5c 100644 --- a/tests/LearnDisabledTest.php +++ b/tests/LearnDisabledTest.php @@ -36,14 +36,14 @@ public function setUp() public function testShouldThrowExceptionIfLearningIsDisabled() { $message = $this->getMessage('Spam_GTUBE.txt'); - $this->setExpectedException('Spamassassin\Client\Exception'); + $this->expectException('Spamassassin\Client\Exception'); $this->sa->learn($message, Client::LEARN_SPAM); } public function testShouldThrowExceptionWhenForgettingIfLearningIsDisabled() { $message = $this->getMessage('Spam_GTUBE.txt'); - $this->setExpectedException('Spamassassin\Client\Exception'); + $this->expectException('Spamassassin\Client\Exception'); $this->sa->learn($message, Client::LEARN_FORGET); } diff --git a/tests/MaxSizeTest.php b/tests/MaxSizeTest.php index 8e49369..7865419 100644 --- a/tests/MaxSizeTest.php +++ b/tests/MaxSizeTest.php @@ -6,7 +6,7 @@ class MaxSizeTest extends BaseTestCase { public function testShouldThrowExceptionIfMessageExceedsMaxSize() { - $this->setExpectedException('Spamassassin\Client\Exception'); + $this->expectException('Spamassassin\Client\Exception'); $params = $this->params; diff --git a/tests/ZlibCompressionTest.php b/tests/ZlibCompressionTest.php index a1beff2..11bc18a 100644 --- a/tests/ZlibCompressionTest.php +++ b/tests/ZlibCompressionTest.php @@ -5,6 +5,7 @@ class ZlibCompressionTest extends BaseTestCase { public function testShouldZlibCompressionWhenAvailable() { + return; $params = $this->params; $params['enableZlib'] = true; From b421f54a382c595cb768598051e140e7e15e00e8 Mon Sep 17 00:00:00 2001 From: Peter Maselkowski Date: Mon, 18 Sep 2017 12:03:33 +0200 Subject: [PATCH 3/6] * Made tests OS language independent --- tests/SpamReportTest.php | 2 +- tests/ZlibCompressionTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/SpamReportTest.php b/tests/SpamReportTest.php index 60dc26c..9e0acf6 100644 --- a/tests/SpamReportTest.php +++ b/tests/SpamReportTest.php @@ -5,7 +5,7 @@ public function testShouldReturnReportIfMessageIsSpam() { $report = $this->sa->getSpamReport($this->gtube); - $this->assertContains("Content preview:", $report->message); + $this->assertContains("1000.0", $report->message); $this->assertContains("1000 GTUBE", $report->message); $this->assertTrue($report->isSpam); } diff --git a/tests/ZlibCompressionTest.php b/tests/ZlibCompressionTest.php index 11bc18a..a1beff2 100644 --- a/tests/ZlibCompressionTest.php +++ b/tests/ZlibCompressionTest.php @@ -5,7 +5,6 @@ class ZlibCompressionTest extends BaseTestCase { public function testShouldZlibCompressionWhenAvailable() { - return; $params = $this->params; $params['enableZlib'] = true; From 99d49c1c8ec1d6bd011f2da7e5d748cd10b72ef4 Mon Sep 17 00:00:00 2001 From: Peter Maselkowski Date: Mon, 18 Sep 2017 12:22:37 +0200 Subject: [PATCH 4/6] >> Renamed typo while keeping it BC +~ Some basic docs --- docs/index.mkd | 29 +++++++++++++++++++++++++++++ src/Spamassassin/Client.php | 2 ++ src/Spamassassin/Client/Result.php | 10 ++++++++-- tests/CheckTest.php | 2 +- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 docs/index.mkd diff --git a/docs/index.mkd b/docs/index.mkd new file mode 100644 index 0000000..7e1bfb9 --- /dev/null +++ b/docs/index.mkd @@ -0,0 +1,29 @@ +# Using spamassasin PHP client + +PHP spamassasin client will connect to existing +spam detection daemon and verify if message is a unwanted +spam or if it's a legit message the end user want to +receive. + +To create instance of spamassasin PHP client, create +new instance of `Client` class - this will use default +params which should connect to local spamassasin: + +```php +use Spamassassin\Client; + +$client = new Client(); +``` + +Then validate if message is market by spam by checking +`$result` variable of type `Spamassassin\Result`: + +```php +$result = $client->process($message); +``` + +The most important properties here are: + +* `isSpam` - whether spamassasin marked message as unwanted +* `score` - received score +* `threshold` - required score to be marked as spam \ No newline at end of file diff --git a/src/Spamassassin/Client.php b/src/Spamassassin/Client.php index f7e818d..56259c8 100644 --- a/src/Spamassassin/Client.php +++ b/src/Spamassassin/Client.php @@ -221,6 +221,7 @@ protected function parseOutput($header, $message) $result->score = (float) $matches[2]; $result->thresold = (float) $matches[3]; + $result->threshold = (float) $matches[3]; } else { /** @@ -240,6 +241,7 @@ protected function parseOutput($header, $message) $result->score = (float) $matches[2]; $result->thresold = (float) $matches[3]; + $result->threshold = (float) $matches[3]; } } diff --git a/src/Spamassassin/Client/Result.php b/src/Spamassassin/Client/Result.php index 4780ce8..838f0bb 100644 --- a/src/Spamassassin/Client/Result.php +++ b/src/Spamassassin/Client/Result.php @@ -49,14 +49,20 @@ class Result /** * How many points the message must score to be considered spam - * + * * @var float */ + public $threshold; + + /** + * Typo kept for backward compat. + * @see $threshold + */ public $thresold; /** * Is it spam or not? - * + * * @var boolean */ public $isSpam; diff --git a/tests/CheckTest.php b/tests/CheckTest.php index d63ff04..f3eb83f 100644 --- a/tests/CheckTest.php +++ b/tests/CheckTest.php @@ -8,7 +8,7 @@ public function testShouldBeAbleToCheckSpamMessage() $return = $this->sa->check($message); $this->assertTrue($return->isSpam); - $this->assertEquals(5.0, $return->thresold); + $this->assertEquals(5.0, $return->threshold); $this->assertTrue($return->score >= 1000); } From 27ee0d6cdc596c3e80bdab30425300f5d943ddb7 Mon Sep 17 00:00:00 2001 From: Peter Maselkowski Date: Mon, 18 Sep 2017 12:24:44 +0200 Subject: [PATCH 5/6] + Link to docs --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index ef8cd01..2f9fcb2 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ Add the following lines to your `composer.json` file and then run `php composer. } ``` +[Basic usage](docs/) + ## License Apache License 2.0. See [LICENSE](LICENSE) for details. From 27f39e2b30e490911a7da4f7708b8f1eae1941a9 Mon Sep 17 00:00:00 2001 From: Peter Maselkowski Date: Mon, 25 Sep 2017 15:23:01 +0200 Subject: [PATCH 6/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2f9fcb2..cd89009 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add the following lines to your `composer.json` file and then run `php composer. } ``` -[Basic usage](docs/) +[Basic usage](docs/index.mkd) ## License