diff --git a/.travis.yml b/.travis.yml index 40af0bd..f2dbf7d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: php php: - - 5.4 - - 5.5 - - 5.6 + - 7.4 + - 8.0 + - 8.1 - hhvm sudo: false diff --git a/Component.php b/Component.php index 93a15d2..89f3acc 100644 --- a/Component.php +++ b/Component.php @@ -1,8 +1,9 @@ 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; + } } \ No newline at end of file diff --git a/tests/MessageValidatorTest.php b/tests/MessageValidatorTest.php index 8fa1023..3ef0313 100644 --- a/tests/MessageValidatorTest.php +++ b/tests/MessageValidatorTest.php @@ -3,19 +3,23 @@ use cleantalk\antispam\Component as CleantalkComponent; use cleantalk\antispam\validators\MessageValidator; -use CleantalkResponse; -use PHPUnit_Framework_TestCase; +use Cleantalk\CleantalkResponse; +use PHPUnit\Framework\TestCase; use Yii; use yii\base\Model; +// phpunit complaning about session headers already sent ... +@session_start(); + /** - * @coversDefaultClass \cleantalk\antispam\Api\validators\MessageValidatorTest + * @coversDefaultClass \cleantalk\antispam\validators\MessageValidator */ -class MessageValidatorTest extends PHPUnit_Framework_TestCase +class MessageValidatorTest extends TestCase { protected function setResponse($allow, $message) { - $mock = $this->getMock(CleantalkComponent::className(), ['sendRequest'], [['apiKey' => CLEANTALK_TEST_API_KEY]]); + $mock = $this->createPartialMock(CleantalkComponent::class, ['sendRequest']); + $mock->apiKey = CLEANTALK_TEST_API_KEY; $mock->expects($this->once()) ->method('sendRequest') ->will( @@ -29,38 +33,38 @@ protected function setResponse($allow, $message) ) ); - Yii::$app->set('antispam', $mock); - } + Yii::$app->set('antispam', $mock); + } - public function testValidator() - { - $this->setResponse(0, 'Forbidden'); + public function testValidator() + { + $this->setResponse(0, 'Forbidden'); - $model = new FakeModel(); - $model->message = 'example1'; - $model->validate(); - $this->assertTrue($model->hasErrors('message')); - } + $model = new FakeModel(); + $model->message = 'example1'; + $model->validate(); + $this->assertTrue($model->hasErrors('message')); + } - public function testValidatorAllow() - { - $this->setResponse(1, ''); + public function testValidatorAllow() + { + $this->setResponse(1, ''); - $model = new FakeModel(); - $model->message = 'example1'; - $model->validate(); - $this->assertFalse($model->hasErrors('message')); - } + $model = new FakeModel(); + $model->message = 'example1'; + $model->validate(); + $this->assertFalse($model->hasErrors('message')); + } } class FakeModel extends Model { - public $message; + public $message; - public function rules() - { - return [ - ['message', MessageValidator::className()] - ]; - } + public function rules() + { + return [ + ['message', MessageValidator::className()] + ]; + } } \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 0a1e72f..aef5150 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,17 +1,19 @@ - - - + + + + ../ - - ../vendor - ../tests - - - + + + ../vendor + ../tests + + -