|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Location of #[Requires] |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +use Nette\Application; |
| 10 | +use Nette\Application\Attributes\Requires; |
| 11 | +use Nette\Http; |
| 12 | +use Tester\Assert; |
| 13 | + |
| 14 | +require __DIR__ . '/../bootstrap.php'; |
| 15 | +require __DIR__ . '/functions.php'; |
| 16 | + |
| 17 | + |
| 18 | +#[Requires(forward: true)] |
| 19 | +class TestClassPresenter extends Nette\Application\UI\Presenter |
| 20 | +{ |
| 21 | + public function actionDefault(): never |
| 22 | + { |
| 23 | + $this->terminate(); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class TestMethodActionPresenter extends Nette\Application\UI\Presenter |
| 28 | +{ |
| 29 | + #[Requires(forward: true)] |
| 30 | + public function actionDefault(): never |
| 31 | + { |
| 32 | + $this->terminate(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class TestMethodRenderPresenter extends Nette\Application\UI\Presenter |
| 37 | +{ |
| 38 | + #[Requires(forward: true)] |
| 39 | + public function renderDefault(): never |
| 40 | + { |
| 41 | + $this->terminate(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class TestMethodHandlePresenter extends Nette\Application\UI\Presenter |
| 46 | +{ |
| 47 | + #[Requires(forward: true)] |
| 48 | + public function handleFoo(): never |
| 49 | + { |
| 50 | + $this->terminate(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +// class-level attribute |
| 56 | +$presenter = createPresenter(TestClassPresenter::class); |
| 57 | +Assert::noError( |
| 58 | + fn() => $presenter->run(new Application\Request('', Application\Request::FORWARD)), |
| 59 | +); |
| 60 | + |
| 61 | +Assert::exception( |
| 62 | + fn() => $presenter->run(new Application\Request('', Http\Request::Get)), |
| 63 | + Application\BadRequestException::class, |
| 64 | + 'Forwarded request is required by TestClassPresenter', |
| 65 | +); |
| 66 | + |
| 67 | + |
| 68 | +// method action<name>() |
| 69 | +$presenter = createPresenter(TestMethodActionPresenter::class); |
| 70 | +Assert::noError( |
| 71 | + fn() => $presenter->run(new Application\Request('', Application\Request::FORWARD)), |
| 72 | +); |
| 73 | + |
| 74 | +Assert::exception( |
| 75 | + fn() => $presenter->run(new Application\Request('', Http\Request::Get)), |
| 76 | + Application\BadRequestException::class, |
| 77 | + 'Forwarded request is required by TestMethodActionPresenter::actionDefault()', |
| 78 | +); |
| 79 | + |
| 80 | + |
| 81 | +// method render<name>() |
| 82 | +$presenter = createPresenter(TestMethodRenderPresenter::class); |
| 83 | +Assert::noError( |
| 84 | + fn() => $presenter->run(new Application\Request('', Application\Request::FORWARD)), |
| 85 | +); |
| 86 | + |
| 87 | +Assert::exception( |
| 88 | + fn() => $presenter->run(new Application\Request('', Http\Request::Get)), |
| 89 | + Application\BadRequestException::class, |
| 90 | + 'Forwarded request is required by TestMethodRenderPresenter::renderDefault()', |
| 91 | +); |
| 92 | + |
| 93 | + |
| 94 | +// method handle<name>() |
| 95 | +$presenter = createPresenter(TestMethodHandlePresenter::class); |
| 96 | +Assert::noError( |
| 97 | + fn() => $presenter->run(new Application\Request('', Application\Request::FORWARD, ['do' => 'foo'])), |
| 98 | +); |
| 99 | + |
| 100 | +Assert::exception( |
| 101 | + fn() => $presenter->run(new Application\Request('', Http\Request::Get, ['do' => 'foo'])), |
| 102 | + Application\BadRequestException::class, |
| 103 | + 'Forwarded request is required by TestMethodHandlePresenter::handleFoo()', |
| 104 | +); |
0 commit comments