2016-08-03 15:56:08 +03:00
|
|
|
<?php
|
2019-12-14 00:16:05 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-03 15:56:08 +03:00
|
|
|
namespace codeception\api\unit\components\ReCaptcha;
|
|
|
|
|
|
|
|
use api\components\ReCaptcha\Validator;
|
2019-02-23 02:11:57 +03:00
|
|
|
use api\tests\unit\TestCase;
|
2017-05-18 02:09:26 +03:00
|
|
|
use GuzzleHttp\ClientInterface;
|
2017-05-18 17:06:01 +03:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2016-08-03 15:56:08 +03:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
|
|
|
|
class ValidatorTest extends TestCase {
|
|
|
|
|
2017-05-18 17:06:01 +03:00
|
|
|
public function testValidateEmptyValue() {
|
2019-12-14 00:16:05 +03:00
|
|
|
$validator = new Validator($this->createMock(ClientInterface::class));
|
2017-05-18 02:09:26 +03:00
|
|
|
$this->assertFalse($validator->validate('', $error));
|
2019-02-26 02:26:02 +03:00
|
|
|
$this->assertSame('error.captcha_required', $error, 'Get error.captcha_required, if passed empty value');
|
2017-05-18 17:06:01 +03:00
|
|
|
}
|
2017-05-18 02:09:26 +03:00
|
|
|
|
2017-05-18 17:06:01 +03:00
|
|
|
public function testValidateInvalidValue() {
|
2019-12-14 00:16:05 +03:00
|
|
|
$mockClient = $this->createMock(ClientInterface::class);
|
|
|
|
$mockClient->method('request')->willReturn(new Response(200, [], json_encode([
|
2017-05-18 02:09:26 +03:00
|
|
|
'success' => false,
|
|
|
|
'error-codes' => [
|
|
|
|
'invalid-input-response', // The response parameter is invalid or malformed.
|
|
|
|
],
|
|
|
|
])));
|
2017-05-18 17:06:01 +03:00
|
|
|
|
2017-05-18 02:09:26 +03:00
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->assertFalse($validator->validate('12341234', $error));
|
2019-02-26 02:26:02 +03:00
|
|
|
$this->assertSame('error.captcha_invalid', $error, 'Get error.captcha_invalid, if passed wrong value');
|
2017-05-18 17:06:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateWithNetworkTroubles() {
|
2019-12-14 00:16:05 +03:00
|
|
|
$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.
|
|
|
|
],
|
|
|
|
])))
|
|
|
|
);
|
2019-12-15 00:49:54 +03:00
|
|
|
$this->getFunctionMock(Validator::class, 'sleep')->expects($this->once());
|
2017-05-18 17:06:01 +03:00
|
|
|
|
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->assertTrue($validator->validate('12341234', $error));
|
|
|
|
$this->assertNull($error);
|
|
|
|
}
|
2017-05-18 02:09:26 +03:00
|
|
|
|
2017-05-18 17:06:01 +03:00
|
|
|
public function testValidateWithHugeNetworkTroubles() {
|
2019-12-14 00:16:05 +03:00
|
|
|
$mockClient = $this->createMock(ClientInterface::class);
|
|
|
|
$mockClient->expects($this->exactly(3))->method('request')->willThrowException($this->createMock(ConnectException::class));
|
2019-12-15 00:49:54 +03:00
|
|
|
$this->getFunctionMock(Validator::class, 'sleep')->expects($this->exactly(2));
|
2017-05-18 17:06:01 +03:00
|
|
|
|
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->expectException(ConnectException::class);
|
|
|
|
$validator->validate('12341234', $error);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateValidValue() {
|
2019-12-14 00:16:05 +03:00
|
|
|
$mockClient = $this->createMock(ClientInterface::class);
|
|
|
|
$mockClient->method('request')->willReturn(new Response(200, [], json_encode([
|
2017-05-18 02:09:26 +03:00
|
|
|
'success' => true,
|
|
|
|
])));
|
|
|
|
$validator = new Validator($mockClient);
|
|
|
|
$this->assertTrue($validator->validate('12341234', $error));
|
|
|
|
$this->assertNull($error);
|
2016-08-03 15:56:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|