2016-08-03 18:26:08 +05:30
|
|
|
<?php
|
2019-12-14 02:46:05 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-03 18:26:08 +05:30
|
|
|
namespace codeception\api\unit\components\ReCaptcha;
|
|
|
|
|
|
|
|
use api\components\ReCaptcha\Validator;
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
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
|
|
|
|
|
|
|
class ValidatorTest extends TestCase {
|
|
|
|
|
2017-05-18 19:36:01 +05:30
|
|
|
public function testValidateEmptyValue() {
|
2019-12-14 02:46:05 +05:30
|
|
|
$validator = new Validator($this->createMock(ClientInterface::class));
|
2017-05-18 04:39:26 +05:30
|
|
|
$this->assertFalse($validator->validate('', $error));
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('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() {
|
2019-12-14 02:46:05 +05:30
|
|
|
$mockClient = $this->createMock(ClientInterface::class);
|
|
|
|
$mockClient->method('request')->willReturn(new Response(200, [], json_encode([
|
2017-05-18 04:39:26 +05:30
|
|
|
'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));
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertSame('error.captcha_invalid', $error, 'Get error.captcha_invalid, if passed wrong value');
|
2017-05-18 19:36:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateWithNetworkTroubles() {
|
2019-12-14 02:46:05 +05:30
|
|
|
$mockClient = $this->createMock(ClientInterface::class);
|
|
|
|
$mockClient->expects($this->exactly(2))->method('request')->willReturnOnConsecutiveCalls(
|
|
|
|
$this->throwException($this->createMock(ConnectException::class)),
|
|
|
|
$this->returnValue(new Response(200, [], json_encode([
|
|
|
|
'success' => true,
|
|
|
|
'error-codes' => [
|
|
|
|
'invalid-input-response', // The response parameter is invalid or malformed.
|
|
|
|
],
|
|
|
|
])))
|
|
|
|
);
|
2017-05-18 19:36:01 +05:30
|
|
|
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() {
|
2019-12-14 02:46:05 +05:30
|
|
|
$mockClient = $this->createMock(ClientInterface::class);
|
|
|
|
$mockClient->expects($this->exactly(3))->method('request')->willThrowException($this->createMock(ConnectException::class));
|
2017-05-18 19:36:01 +05:30
|
|
|
PHPMockery::mock($this->getClassNamespace(Validator::class), 'sleep')->times(2);
|
|
|
|
|
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->expectException(ConnectException::class);
|
|
|
|
$validator->validate('12341234', $error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateValidValue() {
|
2019-12-14 02:46:05 +05:30
|
|
|
$mockClient = $this->createMock(ClientInterface::class);
|
|
|
|
$mockClient->method('request')->willReturn(new Response(200, [], json_encode([
|
2017-05-18 04:39:26 +05:30
|
|
|
'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
|
|
|
}
|