Skip to content

Commit

Permalink
PresenterComponent::redirect() fixed compatibility with func_get_args…
Browse files Browse the repository at this point in the history
…() in HHVM & PHP 7 [Closes #77]
  • Loading branch information
dg committed May 3, 2015
1 parent 969c365 commit 27e8aa6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/Application/UI/PresenterComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,12 @@ public function isLinkCurrent($destination = NULL, $args = array())
public function redirect($code, $destination = NULL, $args = array())
{
if (!is_numeric($code)) { // first parameter is optional
$args = $destination;
$args = is_array($destination) ? $destination : array_slice(func_get_args(), 1);
$destination = $code;
$code = NULL;
}

if (!is_array($args)) {
$args = array_slice(func_get_args(), is_numeric($code) ? 2 : 1);
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}

$presenter = $this->getPresenter();
Expand Down
78 changes: 78 additions & 0 deletions tests/Application/PresenterComponent.redirect().phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* Test: Nette\Application\UI\PresenterComponent::redirect()
*/

use Nette\Http,
Nette\Application,
Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class TestPresenter extends Application\UI\Presenter
{
public $response;

function actionFoo($val)
{
}

function sendResponse(Application\IResponse $response)
{
$this->response = $response;
}
}


$presenter = new TestPresenter;
$presenter->setParent(NULL, 'test');
$presenter->injectPrimary(
NULL,
NULL,
new Application\Routers\SimpleRouter,
new Http\Request(new Http\UrlScript('http://localhost')),
new Http\Response
);


test(function() use ($presenter) {
$presenter->redirect('foo');
Assert::type('Nette\Application\Responses\RedirectResponse', $presenter->response);
Assert::same(302, $presenter->response->getCode());
Assert::same('http://localhost/?action=foo&presenter=test', $presenter->response->getUrl());
});


test(function() use ($presenter) {
$presenter->redirect('foo', array('arg' => 1));
Assert::type('Nette\Application\Responses\RedirectResponse', $presenter->response);
Assert::same(302, $presenter->response->getCode());
Assert::same('http://localhost/?arg=1&action=foo&presenter=test', $presenter->response->getUrl());
});


test(function() use ($presenter) {
$presenter->redirect('foo', 2);
Assert::type('Nette\Application\Responses\RedirectResponse', $presenter->response);
Assert::same(302, $presenter->response->getCode());
Assert::same('http://localhost/?val=2&action=foo&presenter=test', $presenter->response->getUrl());
});


test(function() use ($presenter) {
$presenter->redirect(301, 'foo', array('arg' => 1));
Assert::type('Nette\Application\Responses\RedirectResponse', $presenter->response);
Assert::same(301, $presenter->response->getCode());
Assert::same('http://localhost/?arg=1&action=foo&presenter=test', $presenter->response->getUrl());
});


test(function() use ($presenter) {
$presenter->redirect(301, 'foo', 2);
Assert::type('Nette\Application\Responses\RedirectResponse', $presenter->response);
Assert::same(301, $presenter->response->getCode());
Assert::same('http://localhost/?val=2&action=foo&presenter=test', $presenter->response->getUrl());
});

0 comments on commit 27e8aa6

Please sign in to comment.