33namespace Pd \AsyncControl \UI ;
44
55use 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 ;
89use Nette \Application \UI \Presenter ;
910use Tester \Assert ;
1011use Tester \TestCase ;
1112
1213
1314require_once __DIR__ . '/../../vendor/autoload.php ' ;
1415
16+ \Tester \Environment::bypassFinals ();
1517
18+ /**
19+ * @testCase
20+ */
1621final class AsyncControlTest extends TestCase
1722{
1823
19- const VALID_SIGNAL = 'control-form-submit ' ;
20- const FRAGMENT_PARAMETER = '_escaped_fragment_ ' ;
21-
2224
23- public function testHandleAjax ()
25+ public function testHandleAjax (): void
2426 {
2527 $ presenter = Mockery::mock (Presenter::class);
2628 $ presenter ->shouldReceive ('isAjax ' )->once ()->andReturn (TRUE );
27- $ presenter ->shouldReceive ('getPayload ' )->andReturn ($ payload = new \stdClass );
29+ $ presenter ->shouldReceive ('getPayload ' )->andReturn ($ payload = new \stdClass () );
2830 $ presenter ->shouldReceive ('sendPayload ' )->once ();
29- /**
30- * @var AsyncControl|Mockery\Mock $control
31- */
32- $ control = Mockery::mock (AsyncControl::class)->makePartial ();
31+
32+ $ control = Mockery::mock (AsyncControl::class)->makePartial ()->shouldAllowMockingProtectedMethods ();
3333 $ control ->shouldReceive ('getPresenter ' )->andReturn ($ presenter );
3434 $ renderedContent = 'rendered content ' ;
35- $ control ->shouldReceive ('renderAsync ' )->once ()->andReturnUsing (function () use ($ renderedContent ) {
35+ $ control ->shouldReceive ('doRender ' )->once ()->andReturnUsing (function () use ($ renderedContent ) {
3636 echo $ renderedContent ;
3737 })
3838 ;
@@ -43,15 +43,13 @@ final class AsyncControlTest extends TestCase
4343 }
4444
4545
46- public function testHandleNoAjax ()
46+ public function testHandleNoAjax (): void
4747 {
4848 $ presenter = Mockery::mock (Presenter::class);
4949 $ presenter ->shouldReceive ('isAjax ' )->once ()->andReturn (FALSE );
5050 $ presenter ->shouldNotReceive ('getPayload ' );
5151 $ presenter ->shouldNotReceive ('sendPayload ' );
52- /**
53- * @var AsyncControl|Mockery\Mock $control
54- */
52+
5553 $ control = Mockery::mock (AsyncControl::class)->makePartial ();
5654 $ control ->shouldReceive ('getPresenter ' )->andReturn ($ presenter );
5755 $ control ->shouldNotReceive ('renderAsync ' );
@@ -60,82 +58,39 @@ final class AsyncControlTest extends TestCase
6058 }
6159
6260
63- public function testRenderAsyncLoadLink ()
61+ public function testRenderAsyncLoadsLink (): void
6462 {
65- /**
66- * @var AsyncControl|Mockery\Mock $control
67- */
6863 $ control = Mockery::mock (AsyncControl::class)->makePartial ();
6964
70- $ template = Mockery::mock (ITemplate::class);
65+ $ template = Mockery::mock (Template::class);
66+ $ template ->shouldReceive ('add ' )->once ()->with ('link ' , Mockery::type (AsyncControlLink::class));
7167 $ template ->shouldReceive ('setFile ' )->once ()->withAnyArgs ();
7268 $ template ->shouldReceive ('render ' )->once ();
7369
74- $ templateFactory = Mockery::mock (ITemplateFactory ::class);
70+ $ templateFactory = Mockery::mock (TemplateFactory ::class);
7571 $ templateFactory ->shouldReceive ('createTemplate ' )->once ()->with ($ control )->andReturn ($ template );
7672
7773 $ 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 );
8074 $ presenter ->shouldReceive ('getTemplateFactory ' )->once ()->andReturn ($ templateFactory );
8175
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 ();
76+ $ control ->shouldReceive ('getPresenter ' )->once ()->andReturn ($ presenter );
11477 $ control ->renderAsync ();
11578 }
11679
11780
118- public function testRenderAsyncRenderer ()
81+ public function testRenderAsyncRenderer (): void
11982 {
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 ' );
83+ $ control = Mockery::mock (AsyncControl::class)->makePartial ()->shouldAllowMockingProtectedMethods ();
12984 $ asyncRendered = FALSE ;
13085 $ control ->setAsyncRenderer (function () use (&$ asyncRendered ) {
13186 $ asyncRendered = TRUE ;
13287 });
133- $ control ->renderAsync ();
88+ $ control ->doRender ();
13489 Assert::equal (TRUE , $ asyncRendered );
13590 }
13691
13792
138- protected function tearDown ()
93+ protected function tearDown (): void
13994 {
14095 parent ::tearDown ();
14196 Mockery::close ();
0 commit comments