Skip to content

Commit

Permalink
rozdělení fcí na renderování linku a renderování kontentu
Browse files Browse the repository at this point in the history
  • Loading branch information
krejzyeu committed Dec 21, 2023
1 parent 0776863 commit b2f004a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 82 deletions.
33 changes: 16 additions & 17 deletions src/UI/AsyncControlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Pd\AsyncControl\UI;

use Nette\Application\UI\Control;
use Nette\Application\UI\Presenter;
use Nette\Bridges\ApplicationLatte\Template;


Expand All @@ -27,7 +26,7 @@ public function handleAsyncLoad(): void
ob_start(function () {
});
try {
$this->renderAsync();
$this->doRender();
} catch (\Throwable $e) {
ob_end_clean();
throw $e;
Expand All @@ -40,27 +39,27 @@ public function handleAsyncLoad(): void

public function renderAsync(string $linkMessage = NULL, array $linkAttributes = NULL): void
{
if (
$this instanceof Control
&& $this->getPresenter()->getParameter('_escaped_fragment_') === NULL
&& strpos((string) $this->getPresenter()->getParameter(Presenter::SIGNAL_KEY), sprintf('%s-', $this->getUniqueId())) !== 0
) {
$template = $this->createTemplate();
if ($template instanceof Template) {
$template->add('link', new AsyncControlLink($linkMessage, $linkAttributes));
}
$template->setFile(__DIR__ . '/templates/asyncLoadLink.latte');
$template->render();
} elseif (is_callable($this->asyncRenderer)) {
call_user_func($this->asyncRenderer);
} else {
$this->render();
$template = $this->createTemplate();
if ($template instanceof Template) {
$template->add('link', new AsyncControlLink($linkMessage, $linkAttributes));
}
$template->setFile(__DIR__ . '/templates/asyncLoadLink.latte');
$template->render();
}


public function setAsyncRenderer(callable $renderer): void
{
$this->asyncRenderer = $renderer;
}


protected function doRender(): void
{
if (is_callable($this->asyncRenderer)) {
call_user_func($this->asyncRenderer);
} else {
$this->render();
}
}
}
4 changes: 3 additions & 1 deletion tests/UI/AsyncControlLinkTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use Tester\TestCase;

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


/**
* @testCase
*/
final class AsyncControlLinkTest extends TestCase
{

Expand Down
81 changes: 17 additions & 64 deletions tests/UI/AsyncControlTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,36 @@
namespace Pd\AsyncControl\UI;

use Mockery;
use Nette\Application\UI\ITemplate;
use Nette\Application\UI\ITemplateFactory;

use Nette\Bridges\ApplicationLatte\TemplateFactory;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\Application\UI\Presenter;
use Tester\Assert;
use Tester\TestCase;


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

\Tester\Environment::bypassFinals();

/**
* @testCase
*/
final class AsyncControlTest extends TestCase
{

const VALID_SIGNAL = 'control-form-submit';
const FRAGMENT_PARAMETER = '_escaped_fragment_';


public function testHandleAjax(): void
{
$presenter = Mockery::mock(Presenter::class);
$presenter->shouldReceive('isAjax')->once()->andReturn(TRUE);
$presenter->shouldReceive('getPayload')->andReturn($payload = new \stdClass);
$presenter->shouldReceive('getPayload')->andReturn($payload = new \stdClass());
$presenter->shouldReceive('sendPayload')->once();
/**
* @var AsyncControl|Mockery\Mock $control
*/
$control = Mockery::mock(AsyncControl::class)->makePartial();

$control = Mockery::mock(AsyncControl::class)->makePartial()->shouldAllowMockingProtectedMethods();
$control->shouldReceive('getPresenter')->andReturn($presenter);
$renderedContent = 'rendered content';
$control->shouldReceive('renderAsync')->once()->andReturnUsing(function () use ($renderedContent) {
$control->shouldReceive('doRender')->once()->andReturnUsing(function () use ($renderedContent) {
echo $renderedContent;
})
;
Expand All @@ -51,9 +49,7 @@ final class AsyncControlTest extends TestCase
$presenter->shouldReceive('isAjax')->once()->andReturn(FALSE);
$presenter->shouldNotReceive('getPayload');
$presenter->shouldNotReceive('sendPayload');
/**
* @var AsyncControl|Mockery\Mock $control
*/

$control = Mockery::mock(AsyncControl::class)->makePartial();
$control->shouldReceive('getPresenter')->andReturn($presenter);
$control->shouldNotReceive('renderAsync');
Expand All @@ -62,77 +58,34 @@ final class AsyncControlTest extends TestCase
}


public function testRenderAsyncLoadLink(): void
public function testRenderAsyncLoadsLink(): void
{
/**
* @var AsyncControl|Mockery\Mock $control
*/
$control = Mockery::mock(AsyncControl::class)->makePartial();

$template = Mockery::mock(ITemplate::class);
$template = Mockery::mock(Template::class);
$template->shouldReceive('add')->once()->with('link', Mockery::type(AsyncControlLink::class));
$template->shouldReceive('setFile')->once()->withAnyArgs();
$template->shouldReceive('render')->once();

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

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

$control->shouldReceive('getPresenter')->andReturn($presenter);
$control->shouldReceive('getUniqueId')->once()->andReturn('control');
$control->renderAsync();
}


public function testRenderWithSignal(): void
{
$presenter = Mockery::mock(Presenter::class);
$presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn(NULL);
$presenter->shouldReceive('getParameter')->once()->with(Presenter::SIGNAL_KEY)->andReturn(self::VALID_SIGNAL);
/**
* @var AsyncControl|Mockery\Mock $control
*/
$control = Mockery::mock(AsyncControl::class)->makePartial();
$control->shouldReceive('getPresenter')->andReturn($presenter);
$control->shouldReceive('getUniqueId')->once()->andReturn('control');
$control->shouldReceive('render')->once();
$control->renderAsync();
}


public function testRenderWithFragment(): void
{
$presenter = Mockery::mock(Presenter::class);
$presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn('');
/**
* @var AsyncControl|Mockery\Mock $control
*/
$control = Mockery::mock(AsyncControl::class)->makePartial();
$control->shouldReceive('getPresenter')->andReturn($presenter);
$control->shouldReceive('render')->once();
$control->shouldReceive('getPresenter')->once()->andReturn($presenter);
$control->renderAsync();
}


public function testRenderAsyncRenderer(): void
{
$presenter = Mockery::mock(Presenter::class);
$presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn(NULL);
$presenter->shouldReceive('getParameter')->once()->with(Presenter::SIGNAL_KEY)->andReturn(self::VALID_SIGNAL);
/**
* @var AsyncControl|Mockery\Mock $control
*/
$control = Mockery::mock(AsyncControl::class)->makePartial();
$control->shouldReceive('getPresenter')->andReturn($presenter);
$control->shouldReceive('getUniqueId')->once()->andReturn('control');
$control = Mockery::mock(AsyncControl::class)->makePartial()->shouldAllowMockingProtectedMethods();
$asyncRendered = FALSE;
$control->setAsyncRenderer(function () use (&$asyncRendered) {
$asyncRendered = TRUE;
});
$control->renderAsync();
$control->doRender();
Assert::equal(TRUE, $asyncRendered);
}

Expand Down

0 comments on commit b2f004a

Please sign in to comment.