Skip to content

Commit 6a29dac

Browse files
committed
tests: improved $onSuccess & $onClick etc. tests
1 parent 62d018a commit 6a29dac

File tree

3 files changed

+153
-84
lines changed

3 files changed

+153
-84
lines changed

tests/Forms/Forms.callbackParameters.phpt

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,6 @@ $form->addSubmit('submit');
2121

2222
$types = [];
2323

24-
class TestFormCallbackParameters
25-
{
26-
public static $results = [];
27-
28-
public static function doSomething($form, $values)
29-
{
30-
static::$results[] = $form instanceof Form;
31-
static::$results[] = $values instanceof ArrayHash;
32-
}
33-
34-
public static function doSomethingWithArray($form, array $values)
35-
{
36-
static::$results[] = $form instanceof Form;
37-
static::$results[] = is_array($values);
38-
}
39-
}
40-
41-
// Test the method second parameter to be an ArrayHash instance by default
42-
$m1 = ['TestFormCallbackParameters', 'doSomething'];
43-
44-
// Test the method second parameter to be an array by type-hint
45-
$m2 = ['TestFormCallbackParameters', 'doSomethingWithArray'];
46-
47-
// Test the method second parameter to be an ArrayHash instance by default again
48-
$m3 = ['TestFormCallbackParameters', 'doSomething'];
49-
5024
// Test the closure second parameter to be an ArrayHash instance by default
5125
$f1 = function ($form, $values) use (&$types) {
5226
$types[] = $form instanceof Form;
@@ -56,13 +30,7 @@ $f1 = function ($form, $values) use (&$types) {
5630
// Test the closure second parameter to be an array by type-hint
5731
$f2 = function ($form, array $values) use (&$types) {
5832
$types[] = $form instanceof Form;
59-
$types [] = is_array($values);
60-
};
61-
62-
// Test the closure second parameter to be an ArrayHash instance by default again
63-
$f3 = function ($form, $values) use (&$types) {
64-
$types[] = $form instanceof Form;
65-
$types[] = $values instanceof ArrayHash;
33+
$types[] = is_array($values);
6634
};
6735

6836
// Test the second parameter in ArrayHash form to be immutable
@@ -74,14 +42,9 @@ $f5 = function ($form, $values) use (&$arrayHashIsImmutable) {
7442
$arrayHashIsImmutable[] = $values->text === 'a';
7543
};
7644

77-
foreach ([$m1, $m2, $m3, $f1, $f2, $f3, $f4, $f5] as $f) {
78-
$form->onSuccess[] = $f;
79-
}
80-
foreach ([$m1, $m2, $m3, $f1, $f2, $f3, $f4, $f5] as $f) {
81-
$form->onValidate[] = $f;
82-
}
45+
$form->onSuccess = [$f1, $f2, $f1, $f4, $f5];
46+
$form->onValidate = [$f1, $f2, $f1, $f4, $f5];
8347
$form->fireEvents();
8448

85-
Assert::same(TestFormCallbackParameters::$results, [TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE]);
86-
Assert::same($types, [TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE]);
87-
Assert::same($arrayHashIsImmutable, [TRUE, TRUE]);
49+
Assert::same([TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE], $types);
50+
Assert::same([TRUE, TRUE], $arrayHashIsImmutable);

tests/Forms/Forms.onClick.phpt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\Forms\Form;
6+
use Nette\Forms\Controls\SubmitButton;
7+
use Tester\Assert;
8+
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
test(function () { // valid
14+
$_SERVER['REQUEST_METHOD'] = 'POST';
15+
$_POST = ['btn' => ''];
16+
17+
$called = [];
18+
$form = new Form;
19+
$form->addText('name');
20+
$button = $form->addSubmit('btn');
21+
22+
$button->onClick[] = function (SubmitButton $button) use (&$called) {
23+
$called[] = 'click';
24+
};
25+
$button->onInvalidClick[] = function (SubmitButton $button) use (&$called) {
26+
$called[] = 'invalidClick';
27+
};
28+
$form->onSuccess[] = function (Form $form) use (&$called) {
29+
$called[] = 'success';
30+
};
31+
$form->onError[] = function (Form $form) use (&$called) {
32+
$called[] = 'error';
33+
};
34+
$form->onSubmit[] = function (Form $form) use (&$called) {
35+
$called[] = 'submit';
36+
};
37+
$form->fireEvents();
38+
Assert::same(['click', 'success', 'submit'], $called);
39+
});
40+
41+
42+
test(function () { // invalid
43+
$_SERVER['REQUEST_METHOD'] = 'POST';
44+
$_POST = ['btn' => ''];
45+
46+
$called = [];
47+
$form = new Form;
48+
$form->addText('name')
49+
->setRequired();
50+
$button = $form->addSubmit('btn');
51+
52+
$button->onClick[] = function (SubmitButton $button) use (&$called) {
53+
$called[] = 'click';
54+
};
55+
$button->onInvalidClick[] = function (SubmitButton $button) use (&$called) {
56+
$called[] = 'invalidClick';
57+
};
58+
$form->onSuccess[] = function (Form $form) use (&$called) {
59+
$called[] = 'success';
60+
};
61+
$form->onError[] = function (Form $form) use (&$called) {
62+
$called[] = 'error';
63+
};
64+
$form->onSubmit[] = function (Form $form) use (&$called) {
65+
$called[] = 'submit';
66+
};
67+
$form->fireEvents();
68+
Assert::same(['invalidClick', 'error', 'submit'], $called);
69+
});
70+
71+
72+
Assert::exception(function () {
73+
$form = new Form;
74+
$form->addSubmit('btn')->onClick = TRUE;
75+
$form->fireEvents();
76+
}, Nette\UnexpectedValueException::class, 'Property Nette\Forms\Controls\SubmitButton::$onClick must be array or NULL, boolean given.');

tests/Forms/Forms.onSuccess.phpt

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,78 @@ use Tester\Assert;
1313
require __DIR__ . '/../bootstrap.php';
1414

1515

16-
$_SERVER['REQUEST_METHOD'] = 'POST';
17-
18-
$called = [];
19-
$form = new Form;
20-
$form->addText('name');
21-
$form->addSubmit('submit');
22-
$form->onSuccess[] = function () use (&$called) {
23-
$called[] = 1;
24-
};
25-
$form->onSuccess[] = function ($form) use (&$called) {
26-
$called[] = 2;
27-
$form['name']->addError('error');
28-
};
29-
$form->onSuccess[] = function () use (&$called) {
30-
$called[] = 3;
31-
};
32-
$form->onSuccess[] = function () use (&$called) {
33-
$called[] = 4;
34-
};
35-
$form->onError[] = function () use (&$called) {
36-
$called[] = 'err';
37-
};
38-
$form->fireEvents();
39-
Assert::same([1, 2, 'err'], $called);
40-
41-
42-
$called = [];
43-
$form = new Form;
44-
$form->addText('name');
45-
$form->addSubmit('submit');
46-
$form->onSuccess[] = function () use (&$called) {
47-
$called[] = 1;
48-
};
49-
$form->onSuccess[] = function ($form) use (&$called) {
50-
$called[] = 2;
51-
$form['name']->addError('error');
52-
};
53-
$form->onError[] = function () use (&$called) {
54-
$called[] = 'err';
55-
};
56-
$form->fireEvents();
57-
Assert::same([1, 2, 'err'], $called);
16+
test(function () { // valid
17+
$_SERVER['REQUEST_METHOD'] = 'POST';
18+
19+
$called = [];
20+
$form = new Form;
21+
$form->addText('name');
22+
$form->addSubmit('submit');
23+
24+
$form->onSuccess[] = function (Form $form) use (&$called) {
25+
$called[] = 'success';
26+
};
27+
$form->onError[] = function (Form $form) use (&$called) {
28+
$called[] = 'error';
29+
};
30+
$form->onSubmit[] = function (Form $form) use (&$called) {
31+
$called[] = 'submit';
32+
};
33+
$form->fireEvents();
34+
Assert::same(['success', 'submit'], $called);
35+
});
36+
37+
38+
test(function () { // valid -> invalid
39+
$_SERVER['REQUEST_METHOD'] = 'POST';
40+
41+
$called = [];
42+
$form = new Form;
43+
$form->addText('name');
44+
$form->addSubmit('submit');
45+
46+
$form->onSuccess[] = function (Form $form) use (&$called) {
47+
$called[] = 'success1';
48+
};
49+
$form->onSuccess[] = function (Form $form) use (&$called) {
50+
$called[] = 'success2';
51+
$form['name']->addError('error');
52+
};
53+
$form->onSuccess[] = function (Form $form) use (&$called) {
54+
$called[] = 'success3';
55+
};
56+
$form->onError[] = function (Form $form) use (&$called) {
57+
$called[] = 'error';
58+
};
59+
$form->onSubmit[] = function (Form $form) use (&$called) {
60+
$called[] = 'submit';
61+
};
62+
$form->fireEvents();
63+
Assert::same(['success1', 'success2', 'error', 'submit'], $called);
64+
});
65+
66+
67+
test(function () { // invalid
68+
$_SERVER['REQUEST_METHOD'] = 'POST';
69+
70+
$called = [];
71+
$form = new Form;
72+
$form->addText('name')
73+
->setRequired();
74+
$form->addSubmit('submit');
75+
76+
$form->onSuccess[] = function (Form $form) use (&$called) {
77+
$called[] = 'success';
78+
};
79+
$form->onError[] = function (Form $form) use (&$called) {
80+
$called[] = 'error';
81+
};
82+
$form->onSubmit[] = function (Form $form) use (&$called) {
83+
$called[] = 'submit';
84+
};
85+
$form->fireEvents();
86+
Assert::same(['error', 'submit'], $called);
87+
});
5888

5989

6090
Assert::exception(function () {

0 commit comments

Comments
 (0)