Skip to content

Commit 6f87246

Browse files
committed
rozdělení fcí na renderování linku a renderování kontentu
1 parent 23901a9 commit 6f87246

File tree

4 files changed

+36
-84
lines changed

4 files changed

+36
-84
lines changed

phpstan.neon

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
- '#Cannot call method getParameter\(\) on Nette\\Application\\UI\\Presenter\|null\.#'
4-
- '#Cannot cast mixed to string#'
53
fileExtensions:
64
- phpt
75

src/UI/AsyncControlTrait.php

+16-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Pd\AsyncControl\UI;
44

55
use Nette\Application\UI\Control;
6-
use Nette\Application\UI\Presenter;
76
use Nette\Bridges\ApplicationLatte\Template;
87

98

@@ -27,7 +26,7 @@ public function handleAsyncLoad(): void
2726
ob_start(function () {
2827
});
2928
try {
30-
$this->renderAsync();
29+
$this->doRender();
3130
} catch (\Throwable $e) {
3231
ob_end_clean();
3332
throw $e;
@@ -43,27 +42,27 @@ public function handleAsyncLoad(): void
4342
*/
4443
public function renderAsync(string $linkMessage = NULL, array $linkAttributes = NULL): void
4544
{
46-
if (
47-
$this instanceof Control
48-
&& $this->getPresenter()->getParameter('_escaped_fragment_') === NULL
49-
&& strpos((string) $this->getPresenter()->getParameter(Presenter::SIGNAL_KEY), sprintf('%s-', $this->getUniqueId())) !== 0
50-
) {
51-
$template = $this->createTemplate();
52-
if ($template instanceof Template) {
53-
$template->add('link', new AsyncControlLink($linkMessage, $linkAttributes));
54-
}
55-
$template->setFile(__DIR__ . '/templates/asyncLoadLink.latte');
56-
$template->render();
57-
} elseif (is_callable($this->asyncRenderer)) {
58-
call_user_func($this->asyncRenderer);
59-
} else {
60-
$this->render();
45+
$template = $this->createTemplate();
46+
if ($template instanceof Template) {
47+
$template->add('link', new AsyncControlLink($linkMessage, $linkAttributes));
6148
}
49+
$template->setFile(__DIR__ . '/templates/asyncLoadLink.latte');
50+
$template->render();
6251
}
6352

6453

6554
public function setAsyncRenderer(callable $renderer): void
6655
{
6756
$this->asyncRenderer = $renderer;
6857
}
58+
59+
60+
protected function doRender(): void
61+
{
62+
if (is_callable($this->asyncRenderer)) {
63+
call_user_func($this->asyncRenderer);
64+
} else {
65+
$this->render();
66+
}
67+
}
6968
}

tests/UI/AsyncControlLinkTest.phpt

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use Tester\TestCase;
88

99
require_once __DIR__ . '/../../vendor/autoload.php';
1010

11-
11+
/**
12+
* @testCase
13+
*/
1214
final class AsyncControlLinkTest extends TestCase
1315
{
1416

tests/UI/AsyncControlTest.phpt

+17-64
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,36 @@
33
namespace Pd\AsyncControl\UI;
44

55
use Mockery;
6-
use Nette\Application\UI\ITemplate;
7-
use Nette\Application\UI\ITemplateFactory;
6+
7+
use Nette\Bridges\ApplicationLatte\TemplateFactory;
8+
use Nette\Bridges\ApplicationLatte\Template;
89
use Nette\Application\UI\Presenter;
910
use Tester\Assert;
1011
use Tester\TestCase;
1112

1213

1314
require_once __DIR__ . '/../../vendor/autoload.php';
1415

16+
\Tester\Environment::bypassFinals();
17+
1518
/**
1619
* @testCase
1720
*/
1821
final class AsyncControlTest extends TestCase
1922
{
2023

21-
const VALID_SIGNAL = 'control-form-submit';
22-
const FRAGMENT_PARAMETER = '_escaped_fragment_';
23-
2424

2525
public function testHandleAjax(): void
2626
{
2727
$presenter = Mockery::mock(Presenter::class);
2828
$presenter->shouldReceive('isAjax')->once()->andReturn(TRUE);
29-
$presenter->shouldReceive('getPayload')->andReturn($payload = new \stdClass);
29+
$presenter->shouldReceive('getPayload')->andReturn($payload = new \stdClass());
3030
$presenter->shouldReceive('sendPayload')->once();
31-
/**
32-
* @var AsyncControl|Mockery\Mock $control
33-
*/
34-
$control = Mockery::mock(AsyncControl::class)->makePartial();
31+
32+
$control = Mockery::mock(AsyncControl::class)->makePartial()->shouldAllowMockingProtectedMethods();
3533
$control->shouldReceive('getPresenter')->andReturn($presenter);
3634
$renderedContent = 'rendered content';
37-
$control->shouldReceive('renderAsync')->once()->andReturnUsing(function () use ($renderedContent) {
35+
$control->shouldReceive('doRender')->once()->andReturnUsing(function () use ($renderedContent) {
3836
echo $renderedContent;
3937
})
4038
;
@@ -51,9 +49,7 @@ final class AsyncControlTest extends TestCase
5149
$presenter->shouldReceive('isAjax')->once()->andReturn(FALSE);
5250
$presenter->shouldNotReceive('getPayload');
5351
$presenter->shouldNotReceive('sendPayload');
54-
/**
55-
* @var AsyncControl|Mockery\Mock $control
56-
*/
52+
5753
$control = Mockery::mock(AsyncControl::class)->makePartial();
5854
$control->shouldReceive('getPresenter')->andReturn($presenter);
5955
$control->shouldNotReceive('renderAsync');
@@ -62,77 +58,34 @@ final class AsyncControlTest extends TestCase
6258
}
6359

6460

65-
public function testRenderAsyncLoadLink(): void
61+
public function testRenderAsyncLoadsLink(): void
6662
{
67-
/**
68-
* @var AsyncControl|Mockery\Mock $control
69-
*/
7063
$control = Mockery::mock(AsyncControl::class)->makePartial();
7164

72-
$template = Mockery::mock(ITemplate::class);
65+
$template = Mockery::mock(Template::class);
66+
$template->shouldReceive('add')->once()->with('link', Mockery::type(AsyncControlLink::class));
7367
$template->shouldReceive('setFile')->once()->withAnyArgs();
7468
$template->shouldReceive('render')->once();
7569

76-
$templateFactory = Mockery::mock(ITemplateFactory::class);
70+
$templateFactory = Mockery::mock(TemplateFactory::class);
7771
$templateFactory->shouldReceive('createTemplate')->once()->with($control)->andReturn($template);
7872

7973
$presenter = Mockery::mock(Presenter::class);
80-
$presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn(NULL);
81-
$presenter->shouldReceive('getParameter')->once()->with(Presenter::SIGNAL_KEY)->andReturn(NULL);
8274
$presenter->shouldReceive('getTemplateFactory')->once()->andReturn($templateFactory);
8375

84-
$control->shouldReceive('getPresenter')->andReturn($presenter);
85-
$control->shouldReceive('getUniqueId')->once()->andReturn('control');
86-
$control->renderAsync();
87-
}
88-
89-
90-
public function testRenderWithSignal(): void
91-
{
92-
$presenter = Mockery::mock(Presenter::class);
93-
$presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn(NULL);
94-
$presenter->shouldReceive('getParameter')->once()->with(Presenter::SIGNAL_KEY)->andReturn(self::VALID_SIGNAL);
95-
/**
96-
* @var AsyncControl|Mockery\Mock $control
97-
*/
98-
$control = Mockery::mock(AsyncControl::class)->makePartial();
99-
$control->shouldReceive('getPresenter')->andReturn($presenter);
100-
$control->shouldReceive('getUniqueId')->once()->andReturn('control');
101-
$control->shouldReceive('render')->once();
102-
$control->renderAsync();
103-
}
104-
105-
106-
public function testRenderWithFragment(): void
107-
{
108-
$presenter = Mockery::mock(Presenter::class);
109-
$presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn('');
110-
/**
111-
* @var AsyncControl|Mockery\Mock $control
112-
*/
113-
$control = Mockery::mock(AsyncControl::class)->makePartial();
114-
$control->shouldReceive('getPresenter')->andReturn($presenter);
115-
$control->shouldReceive('render')->once();
76+
$control->shouldReceive('getPresenter')->once()->andReturn($presenter);
11677
$control->renderAsync();
11778
}
11879

11980

12081
public function testRenderAsyncRenderer(): void
12182
{
122-
$presenter = Mockery::mock(Presenter::class);
123-
$presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn(NULL);
124-
$presenter->shouldReceive('getParameter')->once()->with(Presenter::SIGNAL_KEY)->andReturn(self::VALID_SIGNAL);
125-
/**
126-
* @var AsyncControl|Mockery\Mock $control
127-
*/
128-
$control = Mockery::mock(AsyncControl::class)->makePartial();
129-
$control->shouldReceive('getPresenter')->andReturn($presenter);
130-
$control->shouldReceive('getUniqueId')->once()->andReturn('control');
83+
$control = Mockery::mock(AsyncControl::class)->makePartial()->shouldAllowMockingProtectedMethods();
13184
$asyncRendered = FALSE;
13285
$control->setAsyncRenderer(function () use (&$asyncRendered) {
13386
$asyncRendered = TRUE;
13487
});
135-
$control->renderAsync();
88+
$control->doRender();
13689
Assert::equal(TRUE, $asyncRendered);
13790
}
13891

0 commit comments

Comments
 (0)