Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
Allow forms without redirect
Browse files Browse the repository at this point in the history
You can specify path to check after redirect or disable redirect checking using FALSE
  • Loading branch information
mrtnzlml committed Jan 23, 2016
1 parent 6056ab5 commit c183472
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
26 changes: 21 additions & 5 deletions src/traits/TPresenter.php
Expand Up @@ -113,8 +113,8 @@ public function checkRedirect($destination, $path = '/', $params = [], $post = [
$response = $this->check($destination, $params, $post);
if (!$this->exception) {
Assert::same(200, $this->getReturnCode());
Assert::same(302, $response->getCode());
Assert::type('Nette\Application\Responses\RedirectResponse', $response);
Assert::same(302, $response->getCode());
Assert::match("~^https?://fake\.url{$path}[a-z0-9?&=_/]*$~", $response->getUrl());
}
return $response;
Expand Down Expand Up @@ -144,14 +144,30 @@ public function checkJson($destination, $params = [], $post = [])
* @param string $destination
* @param string $formName
* @param array $post
* @param string|boolean $path Path after redirect or FALSE if it's form without redirect
*
* @return \Nette\Application\Responses\RedirectResponse
* @throws \Tester\AssertException
*/
public function checkForm($destination, $formName, $post = [])
public function checkForm($destination, $formName, $post = [], $path = '/')
{
return $this->checkRedirect($destination, '/', [
'do' => $formName . '-submit',
], $post);
if (is_string($path)) {
return $this->checkRedirect($destination, $path, [
'do' => $formName . '-submit',
], $post);
} elseif (is_bool($path)) {
/** @var \Nette\Application\Responses\RedirectResponse $response */
$response = $this->check($destination, [
'do' => $formName . '-submit',
], $post);
if (!$this->exception) {
Assert::same(200, $this->getReturnCode());
Assert::type('Nette\Application\Responses\TextResponse', $response);
}
return $response;
} else {
\Tester\Assert::fail('Path should be string or boolean (probably FALSE).');
}
}

/**
Expand Down
16 changes: 15 additions & 1 deletion tests/Traits/TPresenterTest.phpt
Expand Up @@ -124,11 +124,25 @@ class TPresenterTest extends \Tester\TestCase

public function testForm()
{
$this->checkForm('Presenter:default', 'form', [
$this->checkForm('Presenter:default', 'form1', [
'test' => 'test',
]);
}

public function testFormDifferentDestination()
{
$this->checkForm('Presenter:default', 'form2', [
'test' => 'test',
], '/x/y/json');
}

public function testFormWithoutRedirect()
{
$this->checkForm('Presenter:default', 'form3', [
'test' => 'test',
], FALSE); //do not check redirect
}

public function testSignal()
{
$this->checkSignal('Presenter:default', 'signal');
Expand Down
28 changes: 23 additions & 5 deletions tests/_helpers/presenters/PresenterPresenter.php
Expand Up @@ -54,18 +54,36 @@ public function renderSitemap()
$this->template->sitemap = [0, 1, 2]; //dumb
}

protected function createComponentForm()
protected function createComponentForm1()
{
$form = new \Nette\Application\UI\Form();
$form->addText('test');
$form->onSuccess[] = $this->formSucceeded;
$form->onSuccess[] = function ($_, $values) {
$this->flashMessage(json_encode($values));
$this->redirect('this');
};
return $form;
}

public function formSucceeded($form, $values)
protected function createComponentForm2()
{
$this->flashMessage(json_encode($values));
$this->redirect('this');
$form = new \Nette\Application\UI\Form();
$form->addText('test');
$form->onSuccess[] = function ($_, $values) {
$this->flashMessage(json_encode($values));
$this->redirect('json');
};
return $form;
}

protected function createComponentForm3()
{
$form = new \Nette\Application\UI\Form();
$form->addText('test');
$form->onSuccess[] = function ($_, $values) {
$this->flashMessage(json_encode($values));
};
return $form;
}

public function handleSignal()
Expand Down
5 changes: 3 additions & 2 deletions tests/_helpers/presenters/templates/Presenter/default.latte
Expand Up @@ -8,6 +8,7 @@
<div>
<a n:href="default">{$variable}</a>
</div>
<div>{control form}</div>
<div>{control form1}</div>
<div>{*control form2*}</div>
</body>
</html>
</html>

0 comments on commit c183472

Please sign in to comment.