2016-08-03 18:26:08 +05:30
|
|
|
<?php
|
|
|
|
namespace codeception\api\unit\components\ReCaptcha;
|
|
|
|
|
|
|
|
use api\components\ReCaptcha\Validator;
|
2017-05-18 04:39:26 +05:30
|
|
|
use GuzzleHttp\ClientInterface;
|
2017-05-18 19:36:01 +05:30
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2016-08-03 18:26:08 +05:30
|
|
|
use GuzzleHttp\Psr7\Response;
|
2017-05-18 19:36:01 +05:30
|
|
|
use phpmock\mockery\PHPMockery;
|
|
|
|
use ReflectionClass;
|
2016-08-03 18:26:08 +05:30
|
|
|
use tests\codeception\api\unit\TestCase;
|
|
|
|
|
|
|
|
class ValidatorTest extends TestCase {
|
|
|
|
|
2017-05-18 19:36:01 +05:30
|
|
|
public function testValidateEmptyValue() {
|
2017-05-18 04:39:26 +05:30
|
|
|
$validator = new Validator(mock(ClientInterface::class));
|
|
|
|
$this->assertFalse($validator->validate('', $error));
|
|
|
|
$this->assertEquals('error.captcha_required', $error, 'Get error.captcha_required, if passed empty value');
|
2017-05-18 19:36:01 +05:30
|
|
|
}
|
2017-05-18 04:39:26 +05:30
|
|
|
|
2017-05-18 19:36:01 +05:30
|
|
|
public function testValidateInvalidValue() {
|
2017-05-18 04:39:26 +05:30
|
|
|
$mockClient = mock(ClientInterface::class);
|
|
|
|
$mockClient->shouldReceive('request')->andReturn(new Response(200, [], json_encode([
|
|
|
|
'success' => false,
|
|
|
|
'error-codes' => [
|
|
|
|
'invalid-input-response', // The response parameter is invalid or malformed.
|
|
|
|
],
|
|
|
|
])));
|
2017-05-18 19:36:01 +05:30
|
|
|
|
2017-05-18 04:39:26 +05:30
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->assertFalse($validator->validate('12341234', $error));
|
|
|
|
$this->assertEquals('error.captcha_invalid', $error, 'Get error.captcha_invalid, if passed wrong value');
|
2017-05-18 19:36:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateWithNetworkTroubles() {
|
|
|
|
$mockClient = mock(ClientInterface::class);
|
|
|
|
$mockClient->shouldReceive('request')->andThrow(mock(ConnectException::class))->once();
|
|
|
|
$mockClient->shouldReceive('request')->andReturn(new Response(200, [], json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'error-codes' => [
|
|
|
|
'invalid-input-response', // The response parameter is invalid or malformed.
|
|
|
|
],
|
|
|
|
])))->once();
|
|
|
|
PHPMockery::mock($this->getClassNamespace(Validator::class), 'sleep')->once();
|
|
|
|
|
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->assertTrue($validator->validate('12341234', $error));
|
|
|
|
$this->assertNull($error);
|
|
|
|
}
|
2017-05-18 04:39:26 +05:30
|
|
|
|
2017-05-18 19:36:01 +05:30
|
|
|
public function testValidateWithHugeNetworkTroubles() {
|
|
|
|
$mockClient = mock(ClientInterface::class);
|
|
|
|
$mockClient->shouldReceive('request')->andThrow(mock(ConnectException::class))->times(3);
|
|
|
|
PHPMockery::mock($this->getClassNamespace(Validator::class), 'sleep')->times(2);
|
|
|
|
|
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->expectException(ConnectException::class);
|
|
|
|
$validator->validate('12341234', $error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateValidValue() {
|
2017-05-18 04:39:26 +05:30
|
|
|
$mockClient = mock(ClientInterface::class);
|
|
|
|
$mockClient->shouldReceive('request')->andReturn(new Response(200, [], json_encode([
|
|
|
|
'success' => true,
|
|
|
|
])));
|
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->assertTrue($validator->validate('12341234', $error));
|
|
|
|
$this->assertNull($error);
|
2016-08-03 18:26:08 +05:30
|
|
|
}
|
|
|
|
|
2017-05-18 19:36:01 +05:30
|
|
|
private function getClassNamespace(string $className): string {
|
|
|
|
return (new ReflectionClass($className))->getNamespaceName();
|
|
|
|
}
|
|
|
|
|
2016-08-03 18:26:08 +05:30
|
|
|
}
|