Skip to content

Commit

Permalink
Tests: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 3, 2023
1 parent 38c37e8 commit f5a5508
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 52 deletions.
Expand Up @@ -2,21 +2,19 @@

namespace Tests\Cases\DI;

/**
* Test: ReCaptchaExtension
*/

use Contributte\ReCaptcha\DI\ReCaptchaExtension;
use Contributte\ReCaptcha\ReCaptchaProvider;
use Contributte\Tester\Environment;
use Contributte\Tester\Toolkit;
use Nette\DI\Compiler;
use Nette\DI\ContainerLoader;
use Nette\DI\InvalidConfigurationException;
use Tester\Assert;

require __DIR__ . '/../../bootstrap.php';

test(function () {
$loader = new ContainerLoader(TEMP_DIR);
Toolkit::test(function () {
$loader = new ContainerLoader(Environment::getTestDir());
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('captcha', new ReCaptchaExtension());

Expand All @@ -33,9 +31,9 @@ test(function () {
Assert::equal('foobar', $container->getByType(ReCaptchaProvider::class)->getSiteKey());
});

test(function () {
Toolkit::test(function () {
Assert::exception(function () {
$loader = new ContainerLoader(TEMP_DIR);
$loader = new ContainerLoader(Environment::getTestDir());
$loader->load(function (Compiler $compiler) {
$compiler->addExtension('captcha', new ReCaptchaExtension());
$compiler->addConfig([
Expand Down
Expand Up @@ -8,6 +8,7 @@ namespace Tests\Cases\Forms;

use Contributte\ReCaptcha\Forms\InvisibleReCaptchaField;
use Contributte\ReCaptcha\ReCaptchaProvider;
use Contributte\Tester\Toolkit;
use Nette\Forms\Controls\BaseControl;
use Nette\Forms\Form;
use Nette\Utils\Html;
Expand All @@ -28,7 +29,7 @@ final class FormMock extends Form

}

test(function () {
Toolkit::test(function () {
$field = new InvisibleReCaptchaField(new ReCaptchaProvider('foobar', 'secret'));
Assert::equal(['g-recaptcha' => true], $field->getControlPrototype()->getClass());

Expand All @@ -39,7 +40,7 @@ test(function () {
Assert::equal('foobar', $field->getControlPrototype()->getClass());
});

test(function () {
Toolkit::test(function () {
$form = new FormMock('form');

$fieldName = 'captcha';
Expand All @@ -50,7 +51,7 @@ test(function () {
Assert::equal(sprintf(BaseControl::$idMask, $form->getName() . '-' . $fieldName), $field->getHtmlId());
});

test(function () {
Toolkit::test(function () {
$form = new FormMock('form');

$fieldName = 'captcha';
Expand All @@ -61,7 +62,7 @@ test(function () {
Assert::equal($key, $field->getControl()->{'data-sitekey'});
});

test(function () {
Toolkit::test(function () {
$form = new FormMock('form');

$fieldName = 'captcha';
Expand Down
Expand Up @@ -2,19 +2,16 @@

namespace Tests\Cases\Forms;

/**
* Test: InvisibleReCaptchaBinding
*/

use Contributte\ReCaptcha\Forms\InvisibleReCaptchaBinding;
use Contributte\ReCaptcha\Forms\InvisibleReCaptchaField;
use Contributte\ReCaptcha\ReCaptchaProvider;
use Contributte\Tester\Toolkit;
use Nette\Forms\Form;
use Tester\Assert;

require __DIR__ . '/../../bootstrap.php';

test(function () {
Toolkit::test(function () {
$provider = new ReCaptchaProvider('foo', 'bar');
InvisibleReCaptchaBinding::bind($provider);

Expand All @@ -27,7 +24,7 @@ test(function () {
Assert::same('foo', $recaptcha->getControl()->{'data-sitekey'});
});

test(function () {
Toolkit::test(function () {
$provider = new ReCaptchaProvider('foo', 'bar');
InvisibleReCaptchaBinding::bind($provider);

Expand All @@ -36,8 +33,7 @@ test(function () {
Assert::false($recaptcha->isRequired());
});


test(function () {
Toolkit::test(function () {
$provider = new ReCaptchaProvider('foo', 'bar');
InvisibleReCaptchaBinding::bind($provider);

Expand Down
Expand Up @@ -2,12 +2,9 @@

namespace Tests\Cases\Forms;

/**
* Test: ReCaptchaField
*/

use Contributte\ReCaptcha\Forms\ReCaptchaField;
use Contributte\ReCaptcha\ReCaptchaProvider;
use Contributte\Tester\Toolkit;
use Nette\Forms\Controls\BaseControl;
use Nette\Forms\Form;
use Nette\Utils\Html;
Expand All @@ -28,7 +25,7 @@ final class FormMock extends Form

}

test(function () {
Toolkit::test(function () {
$field = new ReCaptchaField(new ReCaptchaProvider('foobar', 'secret'));
Assert::equal(['g-recaptcha' => true], $field->getControlPrototype()->getClass());

Expand All @@ -39,7 +36,7 @@ test(function () {
Assert::equal('foobar', $field->getControlPrototype()->getClass());
});

test(function () {
Toolkit::test(function () {
$form = new FormMock('form');

$fieldName = 'captcha';
Expand All @@ -51,7 +48,7 @@ test(function () {
Assert::equal(sprintf(BaseControl::$idMask, $form->getName() . '-' . $fieldName), $field->getHtmlId());
});

test(function () {
Toolkit::test(function () {
$form = new FormMock('form');

$fieldName = 'captcha';
Expand All @@ -62,7 +59,7 @@ test(function () {
Assert::equal($key, $field->getControl()->{'data-sitekey'});
});

test(function () {
Toolkit::test(function () {
$form = new FormMock('form');

$fieldName = 'captcha';
Expand Down
Expand Up @@ -9,12 +9,13 @@ namespace Tests\Cases\Forms;
use Contributte\ReCaptcha\Forms\ReCaptchaBinding;
use Contributte\ReCaptcha\Forms\ReCaptchaField;
use Contributte\ReCaptcha\ReCaptchaProvider;
use Contributte\Tester\Toolkit;
use Nette\Forms\Form;
use Tester\Assert;

require __DIR__ . '/../../bootstrap.php';

test(function () {
Toolkit::test(function () {
$provider = new ReCaptchaProvider('foo', 'bar');
ReCaptchaBinding::bind($provider);

Expand All @@ -28,7 +29,7 @@ test(function () {
Assert::same('foo', $recaptcha->getControl()->{'data-sitekey'});
});

test(function () {
Toolkit::test(function () {
$provider = new ReCaptchaProvider('foo', 'bar');
ReCaptchaBinding::bind($provider);

Expand All @@ -37,7 +38,7 @@ test(function () {
Assert::same('My label', $recaptcha->getLabel()->getText());
});

test(function () {
Toolkit::test(function () {
$provider = new ReCaptchaProvider('foo', 'bar');
ReCaptchaBinding::bind($provider);

Expand All @@ -46,8 +47,7 @@ test(function () {
Assert::false($recaptcha->isRequired());
});


test(function () {
Toolkit::test(function () {
$provider = new ReCaptchaProvider('foo', 'bar');
ReCaptchaBinding::bind($provider);

Expand Down
Expand Up @@ -2,26 +2,23 @@

namespace Tests\Cases;

/**
* Test: ReCaptchaResponse
*/

use Contributte\ReCaptcha\ReCaptchaResponse;
use Contributte\Tester\Toolkit;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

test(function () {
Toolkit::test(function () {
$response = new ReCaptchaResponse(true);
Assert::true($response->isSuccess());
});

test(function () {
Toolkit::test(function () {
$response = new ReCaptchaResponse(true);
Assert::equal('1', (string) $response);
});

test(function () {
Toolkit::test(function () {
$error = 'Some error';
$response = new ReCaptchaResponse(false, $error);
Assert::false($response->isSuccess());
Expand Down
Expand Up @@ -2,12 +2,9 @@

namespace Tests\Cases;

/**
* Test: ReCaptchaProvider
*/

use Contributte\ReCaptcha\ReCaptchaProvider;
use Contributte\ReCaptcha\ReCaptchaResponse;
use Contributte\Tester\Toolkit;
use Nette\Forms\Controls\BaseControl;
use Tester\Assert;

Expand All @@ -26,7 +23,7 @@ final class ControlMock extends BaseControl

}

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

Expand All @@ -37,14 +34,14 @@ test(function () {
Assert::notEqual(null, $response->getError());
});

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

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

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

Expand Down
7 changes: 2 additions & 5 deletions tests/bootstrap.php
@@ -1,13 +1,10 @@
<?php declare(strict_types = 1);

use Ninjify\Nunjuck\Environment;
use Contributte\Tester\Environment;

if (@!include __DIR__ . '/../vendor/autoload.php') {
echo 'Install Nette Tester using `composer update --dev`';
exit(1);
}

// Configure environment
Environment::setupTester();
Environment::setupTimezone();
Environment::setupVariables(__DIR__);
Environment::setup(__DIR__);

0 comments on commit f5a5508

Please sign in to comment.