Ivan - Backend - Add unit test for captcha validation [skip ci]

This commit is contained in:
ivan 2016-08-20 19:19:18 -03:00
parent 10d6eebc69
commit 8a87e8118d
7 changed files with 68 additions and 2 deletions

View File

@ -1,2 +1,3 @@
phpunit --colors tests/models
phpunit --colors tests/controllers
phpunit --colors tests/controllers
phpunit --colors tests/libs

View File

@ -1,6 +1,7 @@
<?php
include_once 'tests/__lib__/Mock.php';
class BeanMock implements ArrayAccess {
class BeanMock extends \Mock implements ArrayAccess {
private $properties;
public function __construct($array = []) {

View File

@ -0,0 +1,23 @@
<?php
namespace ReCaptcha {
include_once 'tests/__lib__/Mock.php';
class ReCaptcha extends \Mock {
public static $functionList = array();
public static $verify;
public static function initVerify($value = true) {
ReCaptcha::$verify = \Mock::stub()->returns(new \Mock([
'isSuccess' => \Mock::stub()->returns($value)
]));
}
public function __construct($privateKey) {
parent::__construct();
$this->privateKey = $privateKey;
$this->verify = ReCaptcha::$verify;
}
}
}

View File

@ -0,0 +1,5 @@
<?php
namespace Respect\Validation\Rules {
class AbstractRule {}
}

View File

@ -20,6 +20,7 @@ class Setting extends \Mock {
$mockUserInstance->name = 'MOCK_SETTING_NAME';
$mockUserInstance->value = 'MOCK_SETTING_VALUE';
$mockUserInstance->getValue = \Mock::stub()->returns('MOCK_SETTING_VALUE');
return $mockUserInstance;
}

View File

@ -0,0 +1,35 @@
<?php
include_once 'tests/__lib__/Mock.php';
include_once 'tests/__mocks__/RespectMock.php';
include_once 'tests/__mocks__/SettingMock.php';
include_once 'tests/__mocks__/ReCaptchaMock.php';
include_once 'libs/validations/captcha.php';
class CaptchaValidationTest extends PHPUnit_Framework_TestCase {
protected function setUp() {
Setting::initStubs();
\ReCaptcha\ReCaptcha::initVerify();
$_SERVER['REMOTE_ADDR'] = 'MOCK_REMOTE';
}
public function testShouldReturnCorrectValue() {
$captchaValidation = new \CustomValidations\Captcha();
$response = $captchaValidation->validate('MOCK_RESPONSE');
$this->assertTrue($response);
\ReCaptcha\ReCaptcha::initVerify(false);
$response = $captchaValidation->validate('MOCK_RESPONSE');
$this->assertFalse($response);
}
public function testShouldPassCorrectValuesToCaptcha() {
$captchaValidation = new \CustomValidations\Captcha();
$captchaValidation->validate('MOCK_RESPONSE');
$this->assertTrue(Setting::get('getSetting')->hasBeenCalledWithArgs('recaptcha-private'));
$this->assertTrue(\ReCaptcha\ReCaptcha::$verify->hasBeenCalledWithArgs('MOCK_RESPONSE', 'MOCK_REMOTE'));
}
}