|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Pd\AsyncControl\UI; |
| 4 | + |
| 5 | +use Mockery; |
| 6 | +use Nette\Application\UI\ITemplate; |
| 7 | +use Nette\Application\UI\ITemplateFactory; |
| 8 | +use Nette\Application\UI\Presenter; |
| 9 | +use Tester\Assert; |
| 10 | +use Tester\TestCase; |
| 11 | + |
| 12 | + |
| 13 | +require_once __DIR__ . '/../../vendor/autoload.php'; |
| 14 | + |
| 15 | + |
| 16 | +final class AsyncControlTest extends TestCase |
| 17 | +{ |
| 18 | + |
| 19 | + const VALID_SIGNAL = 'control-form-submit'; |
| 20 | + const FRAGMENT_PARAMETER = '_escaped_fragment_'; |
| 21 | + |
| 22 | + |
| 23 | + public function testHandleAjax() |
| 24 | + { |
| 25 | + $presenter = Mockery::mock(Presenter::class); |
| 26 | + $presenter->shouldReceive('isAjax')->once()->andReturn(TRUE); |
| 27 | + $presenter->shouldReceive('getPayload')->andReturn($payload = new \stdClass); |
| 28 | + $presenter->shouldReceive('sendPayload')->once(); |
| 29 | + /** |
| 30 | + * @var AsyncControl|Mockery\Mock $control |
| 31 | + */ |
| 32 | + $control = Mockery::mock(AsyncControl::class)->makePartial(); |
| 33 | + $control->shouldReceive('getPresenter')->andReturn($presenter); |
| 34 | + $renderedContent = 'rendered content'; |
| 35 | + $control->shouldReceive('renderAsync')->once()->andReturnUsing(function () use ($renderedContent) { |
| 36 | + echo $renderedContent; |
| 37 | + }) |
| 38 | + ; |
| 39 | + $control->shouldReceive('getSnippetId')->with('async')->andReturn($snippetId = 'snippet-control-async'); |
| 40 | + $control->handleAsyncLoad(); |
| 41 | + |
| 42 | + Assert::equal(['snippets' => [$snippetId => $renderedContent]], (array) $payload); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + public function testHandleNoAjax() |
| 47 | + { |
| 48 | + $presenter = Mockery::mock(Presenter::class); |
| 49 | + $presenter->shouldReceive('isAjax')->once()->andReturn(FALSE); |
| 50 | + $presenter->shouldNotReceive('getPayload'); |
| 51 | + $presenter->shouldNotReceive('sendPayload'); |
| 52 | + /** |
| 53 | + * @var AsyncControl|Mockery\Mock $control |
| 54 | + */ |
| 55 | + $control = Mockery::mock(AsyncControl::class)->makePartial(); |
| 56 | + $control->shouldReceive('getPresenter')->andReturn($presenter); |
| 57 | + $control->shouldNotReceive('renderAsync'); |
| 58 | + $control->shouldNotReceive('getSnippetId'); |
| 59 | + $control->handleAsyncLoad(); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public function testRenderAsyncLoadLink() |
| 64 | + { |
| 65 | + /** |
| 66 | + * @var AsyncControl|Mockery\Mock $control |
| 67 | + */ |
| 68 | + $control = Mockery::mock(AsyncControl::class)->makePartial(); |
| 69 | + |
| 70 | + $template = Mockery::mock(ITemplate::class); |
| 71 | + $template->shouldReceive('setFile')->once()->withAnyArgs(); |
| 72 | + $template->shouldReceive('render')->once(); |
| 73 | + |
| 74 | + $templateFactory = Mockery::mock(ITemplateFactory::class); |
| 75 | + $templateFactory->shouldReceive('createTemplate')->once()->with($control)->andReturn($template); |
| 76 | + |
| 77 | + $presenter = Mockery::mock(Presenter::class); |
| 78 | + $presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn(NULL); |
| 79 | + $presenter->shouldReceive('getParameter')->once()->with(Presenter::SIGNAL_KEY)->andReturn(NULL); |
| 80 | + $presenter->shouldReceive('getTemplateFactory')->once()->andReturn($templateFactory); |
| 81 | + |
| 82 | + $control->shouldReceive('getPresenter')->andReturn($presenter); |
| 83 | + $control->shouldReceive('getUniqueId')->once()->andReturn('control'); |
| 84 | + $control->renderAsync(); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + public function testRenderWithSignal() |
| 89 | + { |
| 90 | + $presenter = Mockery::mock(Presenter::class); |
| 91 | + $presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn(NULL); |
| 92 | + $presenter->shouldReceive('getParameter')->once()->with(Presenter::SIGNAL_KEY)->andReturn(self::VALID_SIGNAL); |
| 93 | + /** |
| 94 | + * @var AsyncControl|Mockery\Mock $control |
| 95 | + */ |
| 96 | + $control = Mockery::mock(AsyncControl::class)->makePartial(); |
| 97 | + $control->shouldReceive('getPresenter')->andReturn($presenter); |
| 98 | + $control->shouldReceive('getUniqueId')->once()->andReturn('control'); |
| 99 | + $control->shouldReceive('render')->once(); |
| 100 | + $control->renderAsync(); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + public function testRenderWithFragment() |
| 105 | + { |
| 106 | + $presenter = Mockery::mock(Presenter::class); |
| 107 | + $presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn(''); |
| 108 | + /** |
| 109 | + * @var AsyncControl|Mockery\Mock $control |
| 110 | + */ |
| 111 | + $control = Mockery::mock(AsyncControl::class)->makePartial(); |
| 112 | + $control->shouldReceive('getPresenter')->andReturn($presenter); |
| 113 | + $control->shouldReceive('render')->once(); |
| 114 | + $control->renderAsync(); |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + public function testRenderAsyncRenderer() |
| 119 | + { |
| 120 | + $presenter = Mockery::mock(Presenter::class); |
| 121 | + $presenter->shouldReceive('getParameter')->once()->with(self::FRAGMENT_PARAMETER)->andReturn(NULL); |
| 122 | + $presenter->shouldReceive('getParameter')->once()->with(Presenter::SIGNAL_KEY)->andReturn(self::VALID_SIGNAL); |
| 123 | + /** |
| 124 | + * @var AsyncControl|Mockery\Mock $control |
| 125 | + */ |
| 126 | + $control = Mockery::mock(AsyncControl::class)->makePartial(); |
| 127 | + $control->shouldReceive('getPresenter')->andReturn($presenter); |
| 128 | + $control->shouldReceive('getUniqueId')->once()->andReturn('control'); |
| 129 | + $asyncRendered = FALSE; |
| 130 | + $control->setAsyncRenderer(function () use (&$asyncRendered) { |
| 131 | + $asyncRendered = TRUE; |
| 132 | + }); |
| 133 | + $control->renderAsync(); |
| 134 | + Assert::equal(TRUE, $asyncRendered); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + protected function tearDown() |
| 139 | + { |
| 140 | + parent::tearDown(); |
| 141 | + Mockery::close(); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +(new AsyncControlTest)->run(); |
0 commit comments