|
| 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); |
0 commit comments