-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
233 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
/vendor | ||
/composer.lock | ||
/composer.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: php | ||
php: | ||
- 7.0 | ||
- 7.1 | ||
|
||
script: | ||
- vendor/bin/phpstan analyse -l 5 -c phpstan.neon src | ||
- vendor/bin/tester tests | ||
|
||
before_script: | ||
- travis_retry composer install --no-interaction --prefer-dist | ||
|
||
sudo: false | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
parameters: | ||
ignoreErrors: | ||
- '#Call to an undefined method [a-zA-Z0-9\\_]+::renderAsync\(\)#' | ||
- '#Call to an undefined method [a-zA-Z0-9\\_]+::render\(\)#' | ||
fileExtensions: | ||
- phpt | ||
|
||
autoload_directories: | ||
# - %rootDir%/../../../test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Pd\AsyncControl\UI; | ||
|
||
use Nette\Application\UI\Control; | ||
|
||
|
||
class AsyncControl extends Control | ||
{ | ||
|
||
use AsyncControlTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Pd\AsyncControl\UI; | ||
|
||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
|
||
require_once __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
|
||
final class AsyncControlLinkTest extends TestCase | ||
{ | ||
|
||
public function testLink() | ||
{ | ||
$link = new AsyncControlLink; | ||
Assert::equal('Load content', $link->getMessage()); | ||
Assert::equal([], $link->getAttributes()); | ||
|
||
$link = new AsyncControlLink('Custom message', ['foo' => 'bar']); | ||
Assert::equal('Custom message', $link->getMessage()); | ||
Assert::equal(['foo' => 'bar'], $link->getAttributes()); | ||
|
||
AsyncControlLink::setDefault('Default message', ['bar' => 'baz']); | ||
|
||
$link = new AsyncControlLink; | ||
Assert::equal('Default message', $link->getMessage()); | ||
Assert::equal(['bar' => 'baz'], $link->getAttributes()); | ||
|
||
$link = new AsyncControlLink('Custom message', ['foo' => 'bar']); | ||
Assert::equal('Custom message', $link->getMessage()); | ||
Assert::equal(['foo' => 'bar'], $link->getAttributes()); | ||
} | ||
} | ||
|
||
|
||
(new AsyncControlLinkTest)->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Pd\AsyncControl\UI; | ||
|
||
use Mockery; | ||
use Nette\Application\UI\ITemplate; | ||
use Nette\Application\UI\ITemplateFactory; | ||
use Nette\Application\UI\Presenter; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
|
||
require_once __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
|
||
final class AsyncControlTest extends TestCase | ||
{ | ||
|
||
const VALID_SIGNAL = 'control-form-submit'; | ||
const FRAGMENT_PARAMETER = '_escaped_fragment_'; | ||
|
||
|
||
public function testHandleAjax() | ||
{ | ||
$presenter = Mockery::mock(Presenter::class); | ||
$presenter->shouldReceive('isAjax')->once()->andReturn(TRUE); | ||
$presenter->shouldReceive('getPayload')->andReturn($payload = new \stdClass); | ||
$presenter->shouldReceive('sendPayload')->once(); | ||
/** | ||
* @var AsyncControl|Mockery\Mock $control | ||
*/ | ||
$control = Mockery::mock(AsyncControl::class)->makePartial(); | ||
$control->shouldReceive('getPresenter')->andReturn($presenter); | ||
$renderedContent = 'rendered content'; | ||
$control->shouldReceive('renderAsync')->once()->andReturnUsing(function () use ($renderedContent) { | ||
echo $renderedContent; | ||
}) | ||
; | ||
$control->shouldReceive('getSnippetId')->with('async')->andReturn($snippetId = 'snippet-control-async'); | ||
$control->handleAsyncLoad(); | ||
|
||
Assert::equal(['snippets' => [$snippetId => $renderedContent]], (array) $payload); | ||
} | ||
|
||
|
||
public function testHandleNoAjax() | ||
{ | ||
$presenter = Mockery::mock(Presenter::class); | ||
$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'); | ||
$control->shouldNotReceive('getSnippetId'); | ||
$control->handleAsyncLoad(); | ||
} | ||
|
||
|
||
public function testRenderAsyncLoadLink() | ||
{ | ||
/** | ||
* @var AsyncControl|Mockery\Mock $control | ||
*/ | ||
$control = Mockery::mock(AsyncControl::class)->makePartial(); | ||
|
||
$template = Mockery::mock(ITemplate::class); | ||
$template->shouldReceive('setFile')->once()->withAnyArgs(); | ||
$template->shouldReceive('render')->once(); | ||
|
||
$templateFactory = Mockery::mock(ITemplateFactory::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() | ||
{ | ||
$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() | ||
{ | ||
$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->renderAsync(); | ||
} | ||
|
||
|
||
public function testRenderAsyncRenderer() | ||
{ | ||
$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'); | ||
$asyncRendered = FALSE; | ||
$control->setAsyncRenderer(function () use (&$asyncRendered) { | ||
$asyncRendered = TRUE; | ||
}); | ||
$control->renderAsync(); | ||
Assert::equal(TRUE, $asyncRendered); | ||
} | ||
|
||
|
||
protected function tearDown() | ||
{ | ||
parent::tearDown(); | ||
Mockery::close(); | ||
} | ||
} | ||
|
||
|
||
(new AsyncControlTest)->run(); |