-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestPresenter.php
114 lines (88 loc) · 3.13 KB
/
TestPresenter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types = 1);
namespace NepadaTests\FileUploadControl\Fixtures;
use Latte;
use Nette;
use Nette\Application\Routers\SimpleRouter;
use Nette\Application\UI\Form;
use Nette\Application\UI\Template;
use ReflectionMethod;
final class TestPresenter extends Nette\Application\UI\Presenter
{
public ?Nette\Application\Response $response = null;
/**
* @var callable|null
*/
private $formConfigurator;
public static function create(?Nette\Http\IRequest $httpRequest = null, ?callable $formConfigurator = null): self
{
$presenter = new self();
$presenter->autoCanonicalize = false;
$presenter->formConfigurator = $formConfigurator;
$primaryDependencies = [];
$rc = new ReflectionMethod($presenter, 'injectPrimary');
foreach ($rc->getParameters() as $parameter) {
if ($parameter->isDefaultValueAvailable()) {
continue;
}
$primaryDependencies[$parameter->getName()] = null;
}
$primaryDependencies['httpRequest'] = $httpRequest ?? $presenter->createHttpRequest();
$primaryDependencies['httpResponse'] = $presenter->createHttpResponse();
$primaryDependencies['router'] = new SimpleRouter();
$primaryDependencies['templateFactory'] = $presenter->createTemplateFactory();
$presenter->injectPrimary(...$primaryDependencies);
$presenter->setParent(null, 'Test');
$presenter->changeAction('default');
return $presenter;
}
public function getForm(): Form
{
return $this->getComponent('form');
}
protected function createComponentForm(): Form
{
$translator = new DummyTranslator();
$form = new Form();
$form->setTranslator($translator);
if ($this->formConfigurator !== null) {
call_user_func($this->formConfigurator, $form);
}
$form->onSubmit[] = function (): void {
};
return $form;
}
public function actionDefault(): void
{
}
public function sendTemplate(?Template $template = null): void
{
$this->sendResponse(new Nette\Application\Responses\TextResponse(''));
}
public function sendResponse(Nette\Application\Response $response): void
{
$this->response ??= $response;
parent::sendResponse($response);
}
private function createTemplateFactory(): Nette\Bridges\ApplicationLatte\TemplateFactory
{
return new Nette\Bridges\ApplicationLatte\TemplateFactory($this->createLatteFactory());
}
private function createLatteFactory(): Nette\Bridges\ApplicationLatte\LatteFactory
{
return new class () implements Nette\Bridges\ApplicationLatte\LatteFactory {
public function create(): Latte\Engine
{
return new Latte\Engine();
}
};
}
private function createHttpRequest(): Nette\Http\IRequest
{
return new Nette\Http\Request(new Nette\Http\UrlScript('https://example.com'));
}
private function createHttpResponse(): Nette\Http\IResponse
{
return new Nette\Http\Response();
}
}