Skip to content

Commit

Permalink
refactor RequestStack to handle empty currentRequest & add popRequest()
Browse files Browse the repository at this point in the history
  • Loading branch information
fprochazka committed May 18, 2017
1 parent 24f5d1d commit eb296d8
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 26 deletions.
70 changes: 44 additions & 26 deletions src/RequestStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class RequestStack extends Nette\Object implements Nette\Http\IRequest
{

/**
* @var array|Nette\Http\IRequest
* @var array|Nette\Http\IRequest[]
*/
private $requests = [];

/**
* @var Nette\Http\IRequest
* @var Nette\Http\IRequest|NULL
*/
private $current;

Expand All @@ -41,7 +41,24 @@ public function pushRequest(Nette\Http\IRequest $request)


/**
* @return Nette\Http\IRequest
* @return Nette\Http\IRequest|null
*/
public function popRequest()
{
if (count($this->requests) === 0) {
return NULL;
}

$head = array_pop($this->requests);
$current = end($this->requests);
$this->current = ($current !== FALSE) ? $current : NULL;
return $head;
}



/**
* @return Nette\Http\IRequest|NULL
*/
public function getCurrentRequest()
{
Expand All @@ -55,7 +72,7 @@ public function getCurrentRequest()
*/
public function getUrl()
{
return $this->current->getUrl();
return $this->current !== NULL ? $this->current->getUrl() : NULL;
}


Expand All @@ -66,10 +83,10 @@ public function getUrl()
public function getQuery($key = NULL, $default = NULL)
{
if (func_num_args() === 0) {
return $this->current->getQuery();
return $this->current !== NULL ? $this->current->getQuery() : [];
}

return $this->current->getQuery($key, $default);
return $this->current !== NULL ? $this->current->getQuery($key, $default) : NULL;
}


Expand All @@ -80,10 +97,10 @@ public function getQuery($key = NULL, $default = NULL)
public function getPost($key = NULL, $default = NULL)
{
if (func_num_args() === 0) {
return $this->current->getPost();
return $this->current !== NULL ? $this->current->getPost() : [];
}

return $this->current->getPost($key, $default);
return $this->current !== NULL ? $this->current->getPost($key, $default) : NULL;
}


Expand All @@ -93,7 +110,7 @@ public function getPost($key = NULL, $default = NULL)
*/
public function getFile($key)
{
return $this->current->getFile($key);
return $this->current !== NULL ? $this->current->getFile($key) : NULL;
}


Expand All @@ -103,7 +120,7 @@ public function getFile($key)
*/
public function getFiles()
{
return $this->current->getFiles();
return $this->current !== NULL ? $this->current->getFiles() : [];
}


Expand All @@ -113,7 +130,7 @@ public function getFiles()
*/
public function getCookie($key, $default = NULL)
{
return $this->current->getCookie($key, $default);
return $this->current !== NULL ? $this->current->getCookie($key, $default) : NULL;
}


Expand All @@ -123,7 +140,7 @@ public function getCookie($key, $default = NULL)
*/
public function getCookies()
{
return $this->current->getCookies();
return $this->current !== NULL ? $this->current->getCookies() : [];
}


Expand All @@ -133,7 +150,7 @@ public function getCookies()
*/
public function getMethod()
{
return $this->current->getMethod();
return $this->current !== NULL ? $this->current->getMethod() : NULL;
}


Expand All @@ -143,7 +160,7 @@ public function getMethod()
*/
public function isMethod($method)
{
return $this->current->isMethod($method);
return $this->current !== NULL ? $this->current->isMethod($method) : FALSE;
}


Expand All @@ -153,7 +170,7 @@ public function isMethod($method)
*/
public function getHeader($header, $default = NULL)
{
return $this->current->getHeader($header, $default);
return $this->current !== NULL ? $this->current->getHeader($header, $default) : NULL;
}


Expand All @@ -163,7 +180,7 @@ public function getHeader($header, $default = NULL)
*/
public function getHeaders()
{
return $this->current->getHeaders();
return $this->current !== NULL ? $this->current->getHeaders() : [];
}


Expand All @@ -173,7 +190,7 @@ public function getHeaders()
*/
public function isSecured()
{
return $this->current->isSecured();
return $this->current !== NULL ? $this->current->isSecured() : FALSE;
}


Expand All @@ -183,7 +200,7 @@ public function isSecured()
*/
public function isAjax()
{
return $this->current->isAjax();
return $this->current !== NULL ? $this->current->isAjax() : FALSE;
}


Expand All @@ -193,7 +210,7 @@ public function isAjax()
*/
public function getRemoteAddress()
{
return $this->current->getRemoteAddress();
return $this->current !== NULL ? $this->current->getRemoteAddress() : NULL;
}


Expand All @@ -203,7 +220,7 @@ public function getRemoteAddress()
*/
public function getRemoteHost()
{
return $this->current->getRemoteHost();
return $this->current !== NULL ? $this->current->getRemoteHost() : NULL;
}


Expand All @@ -213,7 +230,7 @@ public function getRemoteHost()
*/
public function getRawBody()
{
return $this->current->getRawBody();
return $this->current !== NULL ? $this->current->getRawBody() : NULL;
}


Expand All @@ -226,19 +243,20 @@ public function getRawBody()
*/
public function detectLanguage(array $langs)
{
if ($this->current instanceof Nette\Http\Request) {
return $this->current->detectLanguage($langs);
}
return $this->current instanceof Nette\Http\Request
? $this->current->detectLanguage($langs)
: NULL;
}



/**
* @return Url|null
* @return Url|NULL
*/
public function getReferer()
{
return ($url = $this->getHeader('referer')) ? new Url($url) : NULL;
$url = $this->getHeader('referer');
return $url !== NULL ? new Url($url) : NULL;
}

}
125 changes: 125 additions & 0 deletions tests/KdybyTests/RequestStack/RequestStack.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/**
* @testCase
*/

namespace KdybyTests\RequestStack;

use Kdyby;
use Kdyby\RequestStack\RequestStack;
use KdybyTests;
use Nette;
use Nette\Http\Url;
use Nette\Http\UrlScript;
use Tester;
use Tester\Assert;

require_once __DIR__ . '/../bootstrap.php';



/**
* @author Filip Procházka <filip@prochazka.su>
*/
class RequestStackTest extends Tester\TestCase
{

public function testPushAndPop()
{
$stack = new RequestStack();

$url = new UrlScript('https://www.kdyby.org/');

$request1 = new Nette\Http\Request($url);
$stack->pushRequest($request1);
Assert::same($request1, $stack->getCurrentRequest());

$request2 = new Nette\Http\Request($url);
$stack->pushRequest($request2);
Assert::same($request2, $stack->getCurrentRequest());

$head = $stack->popRequest();
Assert::same($request2, $head);
Assert::same($request1, $stack->getCurrentRequest());

$head = $stack->popRequest();
Assert::same($request1, $head);
Assert::null($stack->getCurrentRequest());
}

public function testEmptyStack()
{
$stack = new RequestStack();

Assert::null($stack->getCurrentRequest());
Assert::null($stack->popRequest());
Assert::null($stack->getCurrentRequest());
Assert::null($stack->getUrl());
Assert::same([], $stack->getQuery());
Assert::null($stack->getQuery('foo'));
Assert::same([], $stack->getPost());
Assert::null($stack->getPost('foo'));
Assert::null($stack->getFile('f'));
Assert::same([], $stack->getFiles());
Assert::null($stack->getCookie('c'));
Assert::same([], $stack->getCookies());
Assert::null($stack->getMethod());
Assert::false($stack->isMethod('GET'));
Assert::null($stack->getHeader('Accept-Language'));
Assert::same([], $stack->getHeaders());
Assert::false($stack->isSecured());
Assert::false($stack->isAjax());
Assert::null($stack->getRemoteAddress());
Assert::null($stack->getRemoteHost());
Assert::null($stack->getRawBody());
Assert::null($stack->detectLanguage(['en']));
Assert::null($stack->getReferer());
}

public function testNonEmptyStack()
{
$stack = new RequestStack();

$httpRequest = new Nette\Http\Request(
new UrlScript('https://www.kdyby.org/?hello=no'),
NULL,
['foo' => 'foo'],
['f' => $f = new Nette\Http\FileUpload([])],
['c' => 'C'],
['Accept-Language' => 'en', 'X-Requested-With' => 'XMLHttpRequest', 'Referer' => 'https://www.example.com'],
'GET',
'127.0.0.1',
'localhost',
function () {
return 'body';
}
);
$stack->pushRequest($httpRequest);

Assert::same($httpRequest, $stack->getCurrentRequest());
Assert::equal($httpRequest->getUrl(), $stack->getUrl());
Assert::same(['hello' => 'no'], $stack->getQuery());
Assert::same('no', $stack->getQuery('hello'));
Assert::same(['foo' => 'foo'], $stack->getPost());
Assert::same('foo', $stack->getPost('foo'));
Assert::same($f, $stack->getFile('f'));
Assert::same(['f' => $f], $stack->getFiles());
Assert::same('C', $stack->getCookie('c'));
Assert::same(['c' => 'C'], $stack->getCookies());
Assert::same('GET', $stack->getMethod());
Assert::true($stack->isMethod('GET'));
Assert::same('en', $stack->getHeader('Accept-Language'));
Assert::same(['accept-language' => 'en', 'x-requested-with' => 'XMLHttpRequest', 'referer' => 'https://www.example.com'], $stack->getHeaders());
Assert::true($stack->isSecured());
Assert::true($stack->isAjax());
Assert::same('127.0.0.1', $stack->getRemoteAddress());
Assert::same('localhost', $stack->getRemoteHost());
Assert::same('body', $stack->getRawBody());
Assert::same('en', $stack->detectLanguage(['en']));
Assert::equal(new Url('https://www.example.com'), $stack->getReferer());
}

}

(new RequestStackTest())->run();

0 comments on commit eb296d8

Please sign in to comment.