|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Nette\Application\UI\Presenter::link() |
| 5 | + * @phpVersion 7 |
| 6 | + */ |
| 7 | + |
| 8 | +use Nette\Http; |
| 9 | +use Nette\Application; |
| 10 | +use Tester\Assert; |
| 11 | + |
| 12 | + |
| 13 | +require __DIR__ . '/../bootstrap.php'; |
| 14 | + |
| 15 | + |
| 16 | +class TestPresenter extends Application\UI\Presenter |
| 17 | +{ |
| 18 | + /** @persistent */ |
| 19 | + public $var1 = 10; |
| 20 | + |
| 21 | + |
| 22 | + protected function createTemplate($class = NULL) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + protected function startup() |
| 28 | + { |
| 29 | + parent::startup(); |
| 30 | + $this->invalidLinkMode = self::INVALID_LINK_TEXTUAL; |
| 31 | + |
| 32 | + Assert::same('/index.php?action=default&do=hint&presenter=Test', $this->link('hint!', ['var1' => $this->var1])); |
| 33 | + Assert::same('/index.php?var1=20&action=default&do=hint&presenter=Test', $this->link('hint!', ['var1' => $this->var1 * 2])); |
| 34 | + Assert::same('/index.php?y=2&action=default&do=hint&presenter=Test', $this->link('hint!', 1, 2)); |
| 35 | + Assert::same('/index.php?y=2&bool=1&str=1&action=default&do=hint&presenter=Test', $this->link('hint!', '1', '2', TRUE, TRUE)); |
| 36 | + Assert::same('/index.php?y=2&str=0&action=default&do=hint&presenter=Test', $this->link('hint!', '1', '2', FALSE, FALSE)); |
| 37 | + Assert::same('/index.php?action=default&do=hint&presenter=Test', $this->link('hint!', [1])); |
| 38 | + Assert::same("#error: Invalid value for parameter 'x' in method TestPresenter::handlehint(), expected int.", $this->link('hint!', [1], (object) [1])); |
| 39 | + Assert::same('/index.php?y=2&action=default&do=hint&presenter=Test', $this->link('hint!', [1, 'y' => 2])); |
| 40 | + Assert::same('/index.php?y=2&action=default&do=hint&presenter=Test', $this->link('hint!', ['x' => 1, 'y' => 2, 'var1' => $this->var1])); |
| 41 | + Assert::same('#error: Signal must be non-empty string.', $this->link('!')); |
| 42 | + Assert::same('/index.php?action=default&presenter=Test', $this->link('this', ['var1' => $this->var1])); |
| 43 | + Assert::same('/index.php?action=default&presenter=Test', $this->link('this!', ['var1' => $this->var1])); |
| 44 | + Assert::same('/index.php?sort%5By%5D%5Basc%5D=1&action=default&presenter=Test', $this->link('this', ['sort' => ['y' => ['asc' => TRUE]]])); |
| 45 | + |
| 46 | + // Presenter & signal link type checking |
| 47 | + Assert::same("#error: Invalid value for parameter 'x' in method TestPresenter::handlehint(), expected int.", $this->link('hint!', 'x')); |
| 48 | + Assert::same("#error: Invalid value for parameter 'bool' in method TestPresenter::handlehint(), expected bool.", $this->link('hint!', 1, 2, 3)); |
| 49 | + Assert::same("#error: Invalid value for parameter 'x' in method TestPresenter::handlehint(), expected int.", $this->link('hint!', [[]])); |
| 50 | + Assert::same('/index.php?action=default&do=hint&presenter=Test', $this->link('hint!')); |
| 51 | + Assert::same("#error: Invalid value for parameter 'x' in method TestPresenter::handlehint(), expected int.", $this->link('hint!', [new stdClass])); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + public function handleHint(int $x = 1, int $y, bool $bool, string $str) |
| 56 | + { |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +class MockPresenterFactory extends Nette\Object implements Nette\Application\IPresenterFactory |
| 63 | +{ |
| 64 | + function getPresenterClass(& $name) |
| 65 | + { |
| 66 | + return str_replace(':', 'Module\\', $name) . 'Presenter'; |
| 67 | + } |
| 68 | + |
| 69 | + function createPresenter($name) |
| 70 | + {} |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +$url = new Http\UrlScript('http://localhost/index.php'); |
| 75 | +$url->setScriptPath('/index.php'); |
| 76 | + |
| 77 | +$presenter = new TestPresenter; |
| 78 | +$presenter->injectPrimary( |
| 79 | + NULL, |
| 80 | + new MockPresenterFactory, |
| 81 | + new Application\Routers\SimpleRouter, |
| 82 | + new Http\Request($url), |
| 83 | + new Http\Response |
| 84 | +); |
| 85 | + |
| 86 | +$presenter->invalidLinkMode = TestPresenter::INVALID_LINK_WARNING; |
| 87 | +$presenter->autoCanonicalize = FALSE; |
| 88 | + |
| 89 | +$request = new Application\Request('Test', Http\Request::GET, []); |
| 90 | +$presenter->run($request); |
0 commit comments