Skip to content

Commit d5e4eb1

Browse files
committed
improved tests
1 parent d2aa4d5 commit d5e4eb1

8 files changed

+1619
-0
lines changed

tests/UI/Component.signals.phpt

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Application\UI\Component signal handling
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Application;
10+
use Nette\Http;
11+
use Tester\Assert;
12+
13+
14+
require __DIR__ . '/../bootstrap.php';
15+
16+
17+
class NestedControl extends Application\UI\Control
18+
{
19+
public bool $signalCalled = false;
20+
21+
22+
public function handleRefresh(): void
23+
{
24+
$this->signalCalled = true;
25+
}
26+
}
27+
28+
29+
class ParentControl extends Application\UI\Control
30+
{
31+
public bool $signalCalled = false;
32+
33+
34+
public function handleUpdate(): void
35+
{
36+
$this->signalCalled = true;
37+
}
38+
39+
40+
protected function createComponentNested(): NestedControl
41+
{
42+
return new NestedControl;
43+
}
44+
}
45+
46+
47+
test('signal routing to nested component', function () {
48+
$presenter = new class extends Application\UI\Presenter {
49+
protected function createComponentParent(): ParentControl
50+
{
51+
return new ParentControl;
52+
}
53+
54+
55+
public function renderDefault(): void
56+
{
57+
$this->terminate();
58+
}
59+
};
60+
61+
$presenter->injectPrimary(
62+
new Http\Request(new Http\UrlScript, cookies: [Http\Helpers::StrictCookieName => 1]),
63+
new Http\Response,
64+
new Application\PresenterFactory,
65+
new Application\Routers\SimpleRouter,
66+
);
67+
$presenter->autoCanonicalize = false;
68+
69+
$presenter->run(new Application\Request('Test', 'GET', [
70+
'action' => 'default',
71+
'do' => 'parent-nested-refresh',
72+
]));
73+
74+
$parent = $presenter['parent'];
75+
$nested = $parent['nested'];
76+
77+
Assert::type(ParentControl::class, $parent);
78+
Assert::type(NestedControl::class, $nested);
79+
Assert::false($parent->signalCalled);
80+
Assert::true($nested->signalCalled);
81+
});
82+
83+
84+
class ParameterControl extends Application\UI\Control
85+
{
86+
public ?int $receivedParam = null;
87+
88+
89+
public function handleClick(int $id): void
90+
{
91+
$this->receivedParam = $id;
92+
}
93+
}
94+
95+
96+
test('signal with parameters', function () {
97+
$presenter = new class extends Application\UI\Presenter {
98+
protected function createComponentParam(): ParameterControl
99+
{
100+
return new ParameterControl;
101+
}
102+
103+
104+
public function renderDefault(): void
105+
{
106+
$this->terminate();
107+
}
108+
};
109+
110+
$presenter->injectPrimary(
111+
new Http\Request(new Http\UrlScript, cookies: [Http\Helpers::StrictCookieName => 1]),
112+
new Http\Response,
113+
new Application\PresenterFactory,
114+
new Application\Routers\SimpleRouter,
115+
);
116+
$presenter->autoCanonicalize = false;
117+
118+
$presenter->run(new Application\Request('Test', 'GET', [
119+
'action' => 'default',
120+
'do' => 'param-click',
121+
'param-id' => '42',
122+
]));
123+
124+
$control = $presenter['param'];
125+
126+
Assert::type(ParameterControl::class, $control);
127+
Assert::same(42, $control->receivedParam);
128+
});
129+
130+
131+
testException('invalid signal name throws exception', function () {
132+
$presenter = new class extends Application\UI\Presenter {
133+
public function renderDefault(): void
134+
{
135+
$this->terminate();
136+
}
137+
};
138+
139+
$presenter->injectPrimary(
140+
new Http\Request(new Http\UrlScript, cookies: [Http\Helpers::StrictCookieName => 1]),
141+
new Http\Response,
142+
new Application\PresenterFactory,
143+
new Application\Routers\SimpleRouter,
144+
);
145+
$presenter->autoCanonicalize = false;
146+
147+
$presenter->run(new Application\Request('Test', 'GET', [
148+
'action' => 'default',
149+
'do' => 'nonexistent',
150+
]));
151+
}, Application\UI\BadSignalException::class);
152+
153+
154+
testException('signal to nonexistent component throws exception', function () {
155+
$presenter = new class extends Application\UI\Presenter {
156+
public function renderDefault(): void
157+
{
158+
$this->terminate();
159+
}
160+
};
161+
162+
$presenter->injectPrimary(
163+
new Http\Request(new Http\UrlScript, cookies: [Http\Helpers::StrictCookieName => 1]),
164+
new Http\Response,
165+
new Application\PresenterFactory,
166+
new Application\Routers\SimpleRouter,
167+
);
168+
$presenter->autoCanonicalize = false;
169+
170+
$presenter->run(new Application\Request('Test', 'GET', [
171+
'action' => 'default',
172+
'do' => 'nonexistent-click',
173+
]));
174+
}, Application\UI\BadSignalException::class);

tests/UI/Multiplier.phpt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Application\UI\Multiplier
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Application;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
class TestControl extends Application\UI\Control
17+
{
18+
public function __construct(
19+
public string $id,
20+
) {
21+
}
22+
23+
24+
public function handleClick(): void
25+
{
26+
}
27+
}
28+
29+
30+
test('factory creates components dynamically', function () {
31+
$calls = [];
32+
$multiplier = new Application\UI\Multiplier(function ($name) use (&$calls) {
33+
$calls[] = $name;
34+
return new TestControl($name);
35+
});
36+
37+
$component1 = $multiplier->getComponent('item1');
38+
$component2 = $multiplier->getComponent('item2');
39+
40+
Assert::type(TestControl::class, $component1);
41+
Assert::type(TestControl::class, $component2);
42+
Assert::same('item1', $component1->id);
43+
Assert::same('item2', $component2->id);
44+
Assert::same(['item1', 'item2'], $calls);
45+
});
46+
47+
48+
test('same name returns cached component', function () {
49+
$calls = 0;
50+
$multiplier = new Application\UI\Multiplier(function ($name) use (&$calls) {
51+
$calls++;
52+
return new TestControl($name);
53+
});
54+
55+
$component1 = $multiplier->getComponent('item1');
56+
$component2 = $multiplier->getComponent('item1');
57+
58+
Assert::same($component1, $component2);
59+
Assert::same(1, $calls);
60+
});
61+
62+
63+
test('factory receives component name and parent', function () {
64+
$receivedName = null;
65+
$receivedParent = null;
66+
67+
$multiplier = new Application\UI\Multiplier(function ($name, $parent) use (&$receivedName, &$receivedParent) {
68+
$receivedName = $name;
69+
$receivedParent = $parent;
70+
return new TestControl($name);
71+
});
72+
73+
$multiplier->getComponent('test');
74+
75+
Assert::same('test', $receivedName);
76+
Assert::same($multiplier, $receivedParent);
77+
});

0 commit comments

Comments
 (0)