Skip to content

Commit

Permalink
Add more tests for scoring [#65]
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jan 13, 2024
1 parent 2267776 commit f1fdf03
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"require-dev": {
"contributte/qa": "^0.4",
"contributte/tester": "^0.3",
"contributte/phpstan": "^0.1"
"contributte/phpstan": "^0.1" ,
"mockery/mockery": "^1.6.7"
},
"autoload": {
"psr-4": {
Expand Down
76 changes: 67 additions & 9 deletions tests/Cases/ReCaptchaProvider.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace Tests\Cases;
use Contributte\ReCaptcha\ReCaptchaProvider;
use Contributte\ReCaptcha\ReCaptchaResponse;
use Contributte\Tester\Toolkit;
use Mockery;
use Nette\Forms\Controls\BaseControl;
use Nette\Utils\Json;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';
Expand All @@ -21,26 +23,82 @@ final class ControlMock extends BaseControl
}

Toolkit::test(function (): void {
$key = 'key';
$validator = new ReCaptchaProvider($key, 'secret');
$provider = new ReCaptchaProvider('key', 'secret');

$response = $validator->validate('test');
$response = $provider->validate('test');
Assert::type(ReCaptchaResponse::class, $response);

Assert::false($response->isSuccess());
Assert::notEqual(null, $response->getError());
});

Toolkit::test(function (): void {
$key = 'key';
$validator = new ReCaptchaProvider($key, 'secret');
$provider = new ReCaptchaProvider('key', 'secret');

Assert::false($validator->validateControl(new ControlMock()));
Assert::false($provider->validateControl(new ControlMock()));
});

// makeRequest returns null
Toolkit::test(function (): void {
$key = 'key';
$validator = new ReCaptchaProvider($key, 'secret');
$provider = Mockery::mock(ReCaptchaProvider::class)
->shouldAllowMockingProtectedMethods()
->makePartial();

Assert::false($validator->validateControl(new ControlMock()));
$provider->shouldReceive('makeRequest')
->once()
->andReturn(null);

Assert::null($provider->validate('test'));
});

// makeRequest returns success false
Toolkit::test(function (): void {
$provider = Mockery::mock(ReCaptchaProvider::class)
->shouldAllowMockingProtectedMethods()
->makePartial();

$provider->shouldReceive('makeRequest')
->once()
->andReturn(Json::encode([
'success' => false,
'error-codes' => ['test'],
]));

Assert::false($provider->validate('test')->isSuccess());
});

// scoring
Toolkit::test(function (): void {
$provider = Mockery::mock(ReCaptchaProvider::class)
->shouldAllowMockingProtectedMethods()
->makePartial();

$provider->shouldReceive('makeRequest')
->once()
->andReturn(Json::encode([
'success' => true,
'score' => 0.2,
]));

$provider->setMinimalScore(0.5);
Assert::false($provider->validate('test')->isSuccess());

$provider->setMinimalScore(0.1);
Assert::true($provider->validate('test')->isSuccess());
});

// score is missing
Toolkit::test(function (): void {
$provider = Mockery::mock(ReCaptchaProvider::class)
->shouldAllowMockingProtectedMethods()
->makePartial();

$provider->shouldReceive('makeRequest')
->once()
->andReturn(Json::encode([
'success' => true,
]));

$provider->setMinimalScore(0.5);
Assert::true($provider->validate('test')->isSuccess());
});

0 comments on commit f1fdf03

Please sign in to comment.