Skip to content

Commit fd8627c

Browse files
MartinMystikJonasf3l1x
authored andcommitted
Fixed and added tests
1 parent a11e1ef commit fd8627c

17 files changed

+384
-17
lines changed

.github/workflows/main.yaml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323

2424
strategy:
2525
matrix:
26-
php-version: [ "7.4" ]
26+
php-version: [ "7.4", "8.1" ]
2727
operating-system: [ "ubuntu-latest" ]
2828
fail-fast: false
2929

@@ -82,7 +82,7 @@ jobs:
8282

8383
strategy:
8484
matrix:
85-
php-version: [ "7.4" ]
85+
php-version: [ "7.4", "8.1" ]
8686
operating-system: [ "ubuntu-latest" ]
8787
fail-fast: false
8888

@@ -138,7 +138,7 @@ jobs:
138138

139139
strategy:
140140
matrix:
141-
php-version: [ "7.2", "7.3", "7.4" ]
141+
php-version: [ "7.2", "7.3", "7.4", "8.0", "8.1" ]
142142
operating-system: [ "ubuntu-latest" ]
143143
composer-args: [ "" ]
144144
include:
@@ -147,11 +147,9 @@ jobs:
147147
composer-args: "--prefer-lowest"
148148
- php-version: "8.0"
149149
operating-system: "ubuntu-latest"
150-
composer-args: ""
150+
composer-args: "--prefer-lowest"
151151
fail-fast: false
152152

153-
continue-on-error: "${{ matrix.php-version == '8.0' }}"
154-
155153
steps:
156154
- name: "Checkout"
157155
uses: "actions/checkout@v2"
@@ -207,7 +205,7 @@ jobs:
207205

208206
strategy:
209207
matrix:
210-
php-version: [ "7.4" ]
208+
php-version: [ "8.1" ]
211209
operating-system: [ "ubuntu-latest" ]
212210
fail-fast: false
213211

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
},
2626
"require-dev": {
2727
"nette/application": "^3.0.0",
28-
"nette/di": "^3.0.0",
28+
"nette/di": "^3.1.0",
2929
"ninjify/nunjuck": "^0.4",
3030
"ninjify/qa": "^0.12",
31-
"phpstan/phpstan": "^0.12",
32-
"phpstan/phpstan-deprecation-rules": "^0.12",
33-
"phpstan/phpstan-nette": "^0.12",
34-
"phpstan/phpstan-strict-rules": "^0.12"
31+
"phpstan/phpstan": "^1.0",
32+
"phpstan/phpstan-deprecation-rules": "^1.0",
33+
"phpstan/phpstan-nette": "^1.0",
34+
"phpstan/phpstan-strict-rules": "^1.1"
3535
},
3636
"suggest": {
3737
"nette/di": "to use FormFactoryExtension[CompilerExtension]"
@@ -45,7 +45,10 @@
4545
}
4646
},
4747
"config": {
48-
"sort-packages": true
48+
"sort-packages": true,
49+
"allow-plugins": {
50+
"dealerdirect/phpcodesniffer-composer-installer": true
51+
}
4952
},
5053
"minimum-stability": "dev",
5154
"prefer-stable": true,

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ includes:
55
- vendor/phpstan/phpstan-strict-rules/rules.neon
66

77
parameters:
8+
bootstrapFiles:
9+
- vendor/nette/forms/src/compatibility.php
810
ignoreErrors:
911
# It is contravariant, original typehint is not recognized properly
1012
- '#^Parameter \#1 \$value \(string\) of method Contributte\\Forms\\Controls\\ProtectionFastInput\:\:setValue\(\) should be contravariant with parameter \$value \(mixed\) of method Nette\\Forms\\.+\:\:setValue\(\)$#'
13+
- '#^Call to deprecated method getSeparatorPrototype\(\) of class Nette\\Forms\\Controls\\Checkbox#'

src/Controls/ProtectionFastInput.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public function getControl(): Html
4646

4747
public function validateInput(ProtectionFastInput $control): bool
4848
{
49-
$value = (string) $control->getValue();
49+
/** @var string|null $value */
50+
$value = $control->getValue();
5051

5152
$d1 = new DateTime('@' . $value);
5253
$d1->modify($this->diff);

tests/cases/Controls/ProtectionFastInput.phpt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77
use Contributte\Forms\Controls\ProtectionFastInput;
88
use Nette\Forms\Form;
99
use Tester\Assert;
10+
use Tester\Environment;
1011

1112
require_once __DIR__ . '/../../bootstrap.php';
1213

14+
if (!method_exists(Form::class, 'initialize')) {
15+
Environment::skip('This test requires nette/forms >= 3.1');
16+
}
17+
1318
// OK
1419
test(function (): void {
1520
$_SERVER['REQUEST_METHOD'] = 'POST';
1621
$_POST = ['btn' => '', 'fast' => (string) (time() - 10)];
1722

1823
$form = new Form();
19-
$form->addSubmit('btn');
24+
$form->allowCrossOrigin();
25+
$form->addSubmit('btn')->onClick[] = function () {
26+
};
2027

2128
$form['fast'] = $input = new ProtectionFastInput('+5 seconds');
29+
Form::initialize(true);
2230
$form->fireEvents();
2331

2432
Assert::equal([], $form->getErrors());
@@ -30,9 +38,12 @@ test(function (): void {
3038
$_POST = ['btn' => '', 'fast' => (string) (time() - 3)];
3139

3240
$form = new Form();
33-
$form->addSubmit('btn');
41+
$form->allowCrossOrigin();
42+
$form->addSubmit('btn')->onClick[] = function () {
43+
};
3444

3545
$form['fast'] = $input = new ProtectionFastInput('+5 seconds');
46+
Form::initialize(true);
3647
$form->fireEvents();
3748

3849
Assert::equal(['Form was submitted too fast. Are you robot?'], $form->getErrors());
@@ -44,9 +55,12 @@ test(function (): void {
4455
$_POST = ['btn' => '', 'fast' => (string) (time() - 3)];
4556

4657
$form = new Form();
47-
$form->addSubmit('btn');
58+
$form->allowCrossOrigin();
59+
$form->addSubmit('btn')->onClick[] = function () {
60+
};
4861

4962
$form['fast'] = $input = new ProtectionFastInput('+5 seconds', 'Bot? Bot?');
63+
Form::initialize(true);
5064
$form->fireEvents();
5165

5266
Assert::equal(['Bot? Bot?'], $form->getErrors());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* Test: Rendering/Bootstrap3HorizontalRenderer
5+
*/
6+
7+
use Contributte\Forms\Rendering\Bootstrap3HorizontalRenderer;
8+
use Nette\Forms\Form;
9+
use Tester\Assert;
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
$form = new Form();
14+
$form->addText('text1', 'Text 1');
15+
$form->addText('text2', 'Text 2');
16+
$form->addSelect('select', 'Select', ['1' => 'Option 1', '2' => 'Option 2']);
17+
$form->addCheckbox('checkbox', 'Checkbox');
18+
$form->addSubmit('button', 'Button');
19+
20+
test(function () use ($form): void {
21+
$renderer = new Bootstrap3HorizontalRenderer();
22+
Assert::matchFile(__DIR__ . '/expected/bootstrap3horizontal.html', $renderer->render($form));
23+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* Test: Rendering/Bootstrap3InlineRenderer
5+
*/
6+
7+
use Contributte\Forms\Rendering\Bootstrap3InlineRenderer;
8+
use Nette\Forms\Form;
9+
use Tester\Assert;
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
$form = new Form();
14+
$form->addText('text1', 'Text 1');
15+
$form->addText('text2', 'Text 2');
16+
$form->addSelect('select', 'Select', ['1' => 'Option 1', '2' => 'Option 2']);
17+
$form->addCheckbox('checkbox', 'Checkbox');
18+
$form->addSubmit('button', 'Button');
19+
20+
test(function () use ($form): void {
21+
$renderer = new Bootstrap3InlineRenderer();
22+
Assert::matchFile(__DIR__ . '/expected/bootstrap3inline.html', $renderer->render($form));
23+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* Test: Rendering/Bootstrap3VerticalRenderer
5+
*/
6+
7+
use Contributte\Forms\Rendering\Bootstrap3VerticalRenderer;
8+
use Nette\Forms\Form;
9+
use Tester\Assert;
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
$form = new Form();
14+
$form->addText('text1', 'Text 1');
15+
$form->addText('text2', 'Text 2');
16+
$form->addSelect('select', 'Select', ['1' => 'Option 1', '2' => 'Option 2']);
17+
$form->addCheckbox('checkbox', 'Checkbox');
18+
$form->addSubmit('button', 'Button');
19+
20+
test(function () use ($form): void {
21+
$renderer = new Bootstrap3VerticalRenderer();
22+
Assert::matchFile(__DIR__ . '/expected/bootstrap3vertical.html', $renderer->render($form));
23+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* Test: Rendering/Bootstrap4HorizontalRenderer
5+
*/
6+
7+
use Contributte\Forms\Rendering\Bootstrap4HorizontalRenderer;
8+
use Nette\Forms\Form;
9+
use Tester\Assert;
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
$form = new Form();
14+
$form->addText('text1', 'Text 1');
15+
$form->addText('text2', 'Text 2');
16+
$form->addSelect('select', 'Select', ['1' => 'Option 1', '2' => 'Option 2']);
17+
$form->addCheckbox('checkbox', 'Checkbox');
18+
$form->addSubmit('button', 'Button');
19+
20+
test(function () use ($form): void {
21+
$renderer = new Bootstrap4HorizontalRenderer();
22+
Assert::matchFile(__DIR__ . '/expected/bootstrap4horizontal.html', $renderer->render($form));
23+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* Test: Rendering/Bootstrap4InlineRenderer
5+
*/
6+
7+
use Contributte\Forms\Rendering\Bootstrap4InlineRenderer;
8+
use Nette\Forms\Form;
9+
use Tester\Assert;
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
$form = new Form();
14+
$form->addText('text1', 'Text 1');
15+
$form->addText('text2', 'Text 2');
16+
$form->addSelect('select', 'Select', ['1' => 'Option 1', '2' => 'Option 2']);
17+
$form->addCheckbox('checkbox', 'Checkbox');
18+
$form->addSubmit('button', 'Button');
19+
20+
test(function () use ($form): void {
21+
$renderer = new Bootstrap4InlineRenderer();
22+
Assert::matchFile(__DIR__ . '/expected/bootstrap4inline.html', $renderer->render($form));
23+
});

0 commit comments

Comments
 (0)