Реорганизованы зависимости для ReCaptcha\Validator

Новый способ отключения проверки капчи для функциональных тестов
This commit is contained in:
ErickSkrauch
2017-05-18 02:09:26 +03:00
parent b9df9ff2a7
commit 0cf68a6360
7 changed files with 113 additions and 147 deletions

View File

@ -2,64 +2,36 @@
namespace codeception\api\unit\components\ReCaptcha;
use api\components\ReCaptcha\Validator;
use Codeception\Specify;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response;
use tests\codeception\api\unit\TestCase;
class ValidatorTest extends TestCase {
use Specify;
public function testValidateValue() {
$this->specify('Get error.captcha_required, if passed empty value', function() {
$validator = new Validator();
expect($validator->validate('', $error))->false();
expect($error)->equals('error.captcha_required');
});
$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');
$this->specify('Get error.captcha_invalid, if passed wrong value', function() {
/** @var \PHPUnit_Framework_MockObject_MockObject|Validator $validator */
$validator = $this->getMockBuilder(Validator::class)
->setMethods(['createClient'])
->getMock();
$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.
],
])));
$validator = new Validator($mockClient);
$this->assertFalse($validator->validate('12341234', $error));
$this->assertEquals('error.captcha_invalid', $error, 'Get error.captcha_invalid, if passed wrong value');
unset($error);
$validator->expects($this->once())
->method('createClient')
->will($this->returnValue($this->createMockGuzzleClient([
'success' => false,
'error-codes' => [
'invalid-input-response', // The response parameter is invalid or malformed.
],
])));
expect($validator->validate('12341234', $error))->false();
expect($error)->equals('error.captcha_invalid');
});
$this->specify('Get error.captcha_invalid, if passed wrong value', function() {
/** @var \PHPUnit_Framework_MockObject_MockObject|Validator $validator */
$validator = $this->getMockBuilder(Validator::class)
->setMethods(['createClient'])
->getMock();
$validator->expects($this->once())
->method('createClient')
->will($this->returnValue($this->createMockGuzzleClient(['success' => true])));
expect($validator->validate('12341234', $error))->true();
expect($error)->null();
});
}
private function createMockGuzzleClient(array $response) {
$mock = new MockHandler([
new Response(200, [], json_encode($response)),
]);
$handler = HandlerStack::create($mock);
return new Client(['handler' => $handler]);
$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);
}
}