Skip to content
Open
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
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- 7.4
- 8.0
- 8.1
- hhvm

sudo: false
Expand Down
11 changes: 6 additions & 5 deletions Component.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
namespace cleantalk\antispam;

use Cleantalk;
use CleantalkRequest;
use Cleantalk\Cleantalk;
use Cleantalk\CleantalkRequest;
use Cleantalk\CleantalkResponse;
use InvalidArgumentException;
use Yii;
use yii\base\Component as BaseComponent;
Expand Down Expand Up @@ -134,7 +135,7 @@ public function isJavascriptEnable()

/**
* Create request for CleanTalk API.
* @return \CleantalkRequest
* @return CleantalkRequest
*/
protected function createRequest()
{
Expand All @@ -155,9 +156,9 @@ protected function createRequest()
}

/**
* @param \CleantalkRequest $request
* @param CleantalkRequest $request
* @param string $method
* @return \CleantalkResponse CleanTalk API call result
* @return CleantalkResponse CleanTalk API call result
* @throws InvalidArgumentException
*/
protected function sendRequest($request, $method)
Expand Down
28 changes: 24 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,40 @@
],
"type": "yii2-extension",
"require": {
"cleantalk/php-antispam": "~2.0.0"
"php":"^7.4|^8.0",
"cleantalk/php-antispam": "~2.6.0"
},
"require-dev": {
"yiisoft/yii2": "~2.0.0",
"phpunit/phpunit": "~4.6.9"
"yiisoft/yii2": "^2.0.46",
"phpunit/phpunit": "^9.5"
},
"license": "GPL-3.0",
"authors": [
{
"name": "psrustik",
"email": "psrustik@gmail.com"
},
{
"name": "Dana Luther",
"email": "dana.luther@gmail.com"
}
],
"config": {
"platform": {
"php": "8.0.23",
"ext-mcrypt": "8.0"
},
"allow-plugins": {
"yiisoft/yii2-composer": true
}
},
"autoload": {
"psr-4": {"cleantalk\\antispam\\": ""}
}
},
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
}
265 changes: 127 additions & 138 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
@@ -1,150 +1,139 @@
<?php
namespace cleantalk\antispam\tests;

use cleantalk\antispam\Component as CleantalkComponent;
use CleantalkRequest;
use CleantalkResponse;
use cleantalk\antispam\Component;
use Cleantalk\CleantalkRequest;
use Cleantalk\CleantalkResponse;
use InvalidArgumentException;
use PHPUnit_Framework_TestCase;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Yii;
use yii\base\InvalidConfigException;

/**
* @coversDefaultClass \cleantalk\antispam\Component
*/
class ComponentTest extends PHPUnit_Framework_TestCase
class ComponentTest extends TestCase
{
/**
* @var \cleantalk\antispam\Component
*/
protected $component;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->component = Yii::createObject(['class' => CleantalkComponent::className(), 'apiKey' => CLEANTALK_TEST_API_KEY]);
}

/**
* @expectedException \yii\base\InvalidConfigException
*/
public function testInit()
{
Yii::createObject(['class' => CleantalkComponent::className(), 'apiKey' => null]);
}

public function testIsAllowUser()
{
$mock = $this->getSendRequestMock(
array(
'allow' => 1,
'comment' => ''
)
);
list($result, $comment) = $mock->isAllowUser('allow@email.ru', 'Ivan Petrov');
$this->assertTrue($result);

$mock = $this->getSendRequestMock(
array(
'allow' => 0,
'comment' => 'Mock deny'
)
);
list($result, $comment) = $mock->isAllowUser('deny@email.ru', 'Ivan Petrov');
$this->assertFalse($result);
$this->assertEquals('Mock deny', $comment);

$mock = $this->getSendRequestMock(
array(
'inactive' => 1,
'comment' => 'Mock deny'
)
);
list($result, $comment) = $mock->isAllowUser('deny@email.ru', 'Ivan Petrov');
$this->assertFalse($result);
}

public function testIsAllowMessage()
{
$mock = $this->getSendRequestMock(
array(
'allow' => 1,
'comment' => ''
)
);

list($result, $comment) = $mock->isAllowMessage('message1');
$this->assertTrue($result);

$mock = $this->getSendRequestMock(
array(
'allow' => 0,
'comment' => 'Mock deny'
)
);
list($result, $comment) = $mock->isAllowMessage('bad message');
$this->assertFalse($result);
$this->assertEquals('Mock deny', $comment);
}

public function testGetCheckJsCode()
{
$this->assertRegExp('#\w+#', $this->component->getCheckJsCode());
}


public function testStartFormSubmitTime()
{
$this->component->startFormSubmitTime('');
sleep(2);
$time = $this->component->calcFormSubmitTime('');
$this->assertGreaterThanOrEqual(1, $time);
}


public function testIsJavascriptEnable()
{
Yii::$app->request->setBodyParams(['ct_checkjs' => $this->component->getCheckJsCode()]);
$this->assertEquals(1, $this->component->isJavascriptEnable());

Yii::$app->request->setBodyParams([]);
$this->assertEquals(0, $this->component->isJavascriptEnable());
}

public function testCreateRequest()
{
$class = new ReflectionClass($this->component);
$method = $class->getMethod('createRequest');
$method->setAccessible(true);
$request = $method->invoke($this->component);
$this->assertInstanceOf('CleantalkRequest', $request);
}

/**
* @expectedException InvalidArgumentException
*/
public function testSendRequest()
{
$class = new ReflectionClass($this->component);
$method = $class->getMethod('sendRequest');
$method->setAccessible(true);
$method->invoke($this->component, new CleantalkRequest(), 'ololo');
}

protected function getSendRequestMock($response)
{
$mock = $this->getMock(CleantalkComponent::className(), ['sendRequest'], [['apiKey' => CLEANTALK_TEST_API_KEY]]);
$mock->expects($this->once())
->method('sendRequest')
->will(
$this->returnValue(
new CleantalkResponse(
$response
)
)
);
return $mock;
}
/**
* @var \cleantalk\antispam\Component
*/
protected $component;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->component = Yii::createObject(['class' => Component::class, 'apiKey' => CLEANTALK_TEST_API_KEY]);
}

public function testInit()
{
$this->expectException(InvalidConfigException::class);
Yii::createObject(['class' => Component::class, 'apiKey' => null]);
}

public function testIsAllowUser()
{
$mock = $this->getSendRequestMock([
'allow' => 1,
'comment' => ''
]);
[$result, $comment] = $mock->isAllowUser('allow@email.ru', 'Ivan Petrov');
$this->assertTrue($result);

$mock = $this->getSendRequestMock([
'allow' => 0,
'comment' => 'Mock deny'
]);
[$result, $comment] = $mock->isAllowUser('deny@email.ru', 'Ivan Petrov');
$this->assertFalse($result);
$this->assertEquals('Mock deny', $comment);

$mock = $this->getSendRequestMock([
'inactive' => 1,
'comment' => 'Mock deny'
]);
[$result, $comment] = $mock->isAllowUser('deny@email.ru', 'Ivan Petrov');
$this->assertFalse($result);
}

public function testIsAllowMessage()
{
$mock = $this->getSendRequestMock([
'allow' => 1,
'comment' => ''
]);

[$result, $comment] = $mock->isAllowMessage('message1');
$this->assertTrue($result);

$mock = $this->getSendRequestMock([
'allow' => 0,
'comment' => 'Mock deny'
]);
[$result, $comment] = $mock->isAllowMessage('bad message');
$this->assertFalse($result);
$this->assertEquals('Mock deny', $comment);
}

public function testGetCheckJsCode()
{
$this->assertMatchesRegularExpression('#\w+#', $this->component->getCheckJsCode());
}


public function testStartFormSubmitTime()
{
$this->component->startFormSubmitTime('');
sleep(2);
$time = $this->component->calcFormSubmitTime('');
$this->assertGreaterThanOrEqual(1, $time);
}


public function testIsJavascriptEnable()
{
Yii::$app->request->setBodyParams(['ct_checkjs' => $this->component->getCheckJsCode()]);
$this->assertEquals(1, $this->component->isJavascriptEnable());

Yii::$app->request->setBodyParams([]);
$this->assertEquals(0, $this->component->isJavascriptEnable());
}

public function testCreateRequest()
{
$class = new ReflectionClass($this->component);
$method = $class->getMethod('createRequest');
$method->setAccessible(true);
$request = $method->invoke($this->component);
$this->assertInstanceOf(CleantalkRequest::class, $request);
}

public function testSendRequest()
{
$this->expectException(InvalidArgumentException::class);
$class = new ReflectionClass($this->component);
$method = $class->getMethod('sendRequest');
$method->setAccessible(true);
$method->invoke($this->component, new CleantalkRequest(), 'ololo');
}

protected function getSendRequestMock($response)
{

$mock = $this->createPartialMock(Component::class, ['sendRequest']);
$mock->apiKey = CLEANTALK_TEST_API_KEY;
$mock->expects($this->once())
->method('sendRequest')
->will(
$this->returnValue(
new CleantalkResponse(
$response
)
)
);
return $mock;
}
}
Loading