From f1fdf03638f2f161602c600628963659910c732f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Sat, 13 Jan 2024 12:04:48 +0100 Subject: [PATCH] Add more tests for scoring [#65] --- composer.json | 3 +- tests/Cases/ReCaptchaProvider.phpt | 76 ++++++++++++++++++++++++++---- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index a9ced82..41dc4ba 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/tests/Cases/ReCaptchaProvider.phpt b/tests/Cases/ReCaptchaProvider.phpt index 148faba..f90d65c 100644 --- a/tests/Cases/ReCaptchaProvider.phpt +++ b/tests/Cases/ReCaptchaProvider.phpt @@ -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'; @@ -21,10 +23,9 @@ 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()); @@ -32,15 +33,72 @@ Toolkit::test(function (): void { }); 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()); });