Skip to content

Commit

Permalink
Merge pull request #18 from mirko-pagliai/develop
Browse files Browse the repository at this point in the history
fixed
  • Loading branch information
mirko-pagliai authored Oct 15, 2022
2 parents 2b250b3 + 327cca1 commit f93b1bd
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.directory
.idea
.phpunit.result.cache
*_old
*.old.*
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# 1.x branch
## 1.1 branch
### 1.1.7
* updated for `php-tools` 1.7.0;
* little code fixes.

### 1.1.6
* added tests for PHP 8.1.
* added tests for PHP 8.1;
* little fixes for `phpstan`, `psalm` and for the `composer.json` file.

### 1.1.5
* updated for `php-tools` 1.5.8.
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parameters:
path: tests/TestCase/SpamDetectorTest.php

-
message: "#^Ternary operator condition is always true\\.$#"
message: "#^Negated boolean expression is always false\\.$#"
count: 1
path: tests/TestCase/SpamDetectorTest.php

25 changes: 8 additions & 17 deletions src/SpamDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
*/
namespace StopSpam;

use BadMethodCallException;
use Cake\Cache\Cache;
use Cake\Core\InstanceConfigTrait;
use Cake\Http\Client;
use Cake\Utility\Hash;
use Exception;
use ErrorException;
use Tools\Exceptionist;

/**
* A spam detector
* @method \StopSpam\SpamDetector email(string ...$email) Sets an email address to verify
* @method \StopSpam\SpamDetector ip(string ...$ip) Sets an IP address to verify
* @method \StopSpam\SpamDetector username(string ...$username) Sets an username to verify
* @method \StopSpam\SpamDetector username(string ...$username) Sets a username to verify
*/
class SpamDetector
{
Expand Down Expand Up @@ -61,7 +60,6 @@ class SpamDetector
/**
* Construct
* @param \Cake\Http\Client|null $Client A Client instance
* @uses $Client
*/
public function __construct(?Client $Client = null)
{
Expand All @@ -73,14 +71,13 @@ public function __construct(?Client $Client = null)
* @param string $name Method name
* @param array $arguments Method arguments
* @return $this
* @throws \BadMethodCallException
* @uses $data
* @throws \Tools\Exception\NotInArrayException|\ErrorException
*/
public function __call(string $name, array $arguments)
{
$methodName = sprintf('%s::%s', get_class($this), $name);
Exceptionist::inArray($name, ['email', 'ip', 'username'], __d('stop-spam', 'Method `{0}()` does not exist', $methodName), BadMethodCallException::class);
Exceptionist::isTrue($arguments, __d('stop-spam', 'At least 1 argument required for `{0}()` method', $methodName), BadMethodCallException::class);
Exceptionist::inArray($name, ['email', 'ip', 'username'], __d('stop-spam', 'Method `{0}()` does not exist', $methodName));
Exceptionist::isTrue($arguments, __d('stop-spam', 'At least 1 argument required for `{0}()` method', $methodName));

$this->data[$name] = array_merge($this->data[$name] ?? [], $arguments);

Expand All @@ -91,7 +88,6 @@ public function __call(string $name, array $arguments)
* Performs a single GET request and returns result
* @param array<string, array> $data The query data you want to send
* @return array Result
* @uses $Client
*/
protected function _getResponse(array $data): array
{
Expand All @@ -105,7 +101,6 @@ protected function _getResponse(array $data): array
/**
* Returns results of the last verification
* @return array
* @uses $result
*/
public function getResult(): array
{
Expand All @@ -114,20 +109,16 @@ public function getResult(): array

/**
* Verifies, based on the set data, if it's a spammer
* @return bool Returns `false` if certainly at least one of the parameters
* has been reported as a spammer, otherwise returns `true`
* @throws \Exception
* @uses _getResponse()
* @uses $data
* @uses $result
* @return bool Returns `false` if certainly at least one of the parameters has been reported as a spammer
* @throws \ErrorException
*/
public function verify(): bool
{
Exceptionist::isTrue($this->data, __d('stop-spam', 'Method `{0}()` was called without data to verify', __METHOD__));
$this->result = $this->_getResponse($this->data);

if (array_key_exists('error', $this->result)) {
throw new Exception(__d('stop-spam', 'Error from server: `{0}`', $this->result['error']));
throw new ErrorException(__d('stop-spam', 'Error from server: `{0}`', $this->result['error']));
}
$this->data = [];

Expand Down
31 changes: 16 additions & 15 deletions tests/TestCase/SpamDetectorTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);

/**
Expand All @@ -14,7 +15,6 @@
*/
namespace StopSpam\Test\TestCase;

use BadMethodCallException;
use Cake\Cache\Cache;
use Cake\Http\Client;
use Cake\Http\Client\Response;
Expand All @@ -41,26 +41,28 @@ public function setUp(): void
parent::setUp();

$Client = $this->getMockBuilder(Client::class)
->setMethods(['get'])
->onlyMethods(['get'])
->getMock();

$Client->expects($this->any())
->method('get')
->will($this->returnCallback(function (string $url, $data = []): Response {
->willReturnCallback(function (string $url, array $data = []): Response {
//Gets the `Response` instance already saved in the test files
$file = TESTS . DS . 'responses' . DS . md5(serialize($data));
if (!file_exists($file)) {
echo PHP_EOL . 'Creating file `' . $file . '`...' . PHP_EOL;
$response = (new Client())->get($url, $data);
file_put_contents($file, $response->getStringBody());

return $response;
if (file_exists($file)) {
return new Response([], file_get_contents($file) ?: '');
}

return new Response([], file_get_contents($file) ?: '');
}));
echo PHP_EOL . 'Creating file `' . $file . '`...' . PHP_EOL;
$response = (new Client())->get($url, $data);
file_put_contents($file, $response->getStringBody());

return $response;
});

$this->SpamDetector = $this->SpamDetector ?: new SpamDetector($Client);
if (!$this->SpamDetector) {
$this->SpamDetector = new SpamDetector($Client);
}
}

/**
Expand All @@ -80,8 +82,8 @@ public function tearDown(): void
*/
public function testCallMagicMethodNoExistingMethod(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Method `StopSpam\SpamDetector::noExisting()` does not exist');
/** @noinspection PhpUndefinedMethodInspection */
(new SpamDetector())->noExisting();
}

Expand All @@ -91,7 +93,6 @@ public function testCallMagicMethodNoExistingMethod(): void
*/
public function testCallMagicMethodMissingArguments(): void
{
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('At least 1 argument required for `StopSpam\SpamDetector::username()` method');
(new SpamDetector())->username();
}
Expand Down Expand Up @@ -186,7 +187,7 @@ public function testVerify(): void
public function testVerifyWithErrorFromServer(): void
{
$SpamDetector = @$this->getMockBuilder(SpamDetector::class)
->setMethods(['_getResponse'])
->onlyMethods(['_getResponse'])
->getMock();
$SpamDetector->method('_getResponse')->willReturn([
'success' => 0,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/TestApp/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function bootstrap(): void
$this->addPlugin(StopSpam::class);
}

public function middleware(MiddlewareQueue $middleware)
public function middleware(MiddlewareQueue $middleware): MiddlewareQueue
{
return $middleware->add(new RoutingMiddleware($this));
}
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.6
1.1.7

0 comments on commit f93b1bd

Please sign in to comment.