2016-08-03 18:26:08 +05:30
|
|
|
<?php
|
2019-12-14 02:46:05 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
namespace api\tests\unit\components\ReCaptcha;
|
2016-08-03 18:26:08 +05:30
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
class ValidatorTest extends TestCase {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateEmptyValue(): void {
|
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
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateInvalidValue(): void {
|
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
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateWithNetworkTroubles(): void {
|
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.
|
|
|
|
],
|
2024-12-02 15:40:55 +05:30
|
|
|
]))),
|
2019-12-14 02:46:05 +05:30
|
|
|
);
|
2019-12-15 03:19:54 +05:30
|
|
|
$this->getFunctionMock(Validator::class, 'sleep')->expects($this->once());
|
2017-05-18 19:36:01 +05:30
|
|
|
|
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->assertTrue($validator->validate('12341234', $error));
|
|
|
|
$this->assertNull($error);
|
|
|
|
}
|
2017-05-18 04:39:26 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateWithHugeNetworkTroubles(): void {
|
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));
|
2019-12-15 03:19:54 +05:30
|
|
|
$this->getFunctionMock(Validator::class, 'sleep')->expects($this->exactly(2));
|
2017-05-18 19:36:01 +05:30
|
|
|
|
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->expectException(ConnectException::class);
|
|
|
|
$validator->validate('12341234', $error);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testValidateValidValue(): void {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|