More routing tests.

This commit is contained in:
Lonnie Ezell 2017-04-19 22:05:40 -05:00
parent ec49bf9c2d
commit e5a15b7616
2 changed files with 128 additions and 63 deletions

View File

@ -138,7 +138,8 @@ if (! function_exists('view_cell'))
*/
function view_cell(string $library, $params = null, int $ttl = 0, string $cacheName = null)
{
return Services::viewcell()->render($library, $params, $ttl, $cacheName);
return Services::viewcell()
->render($library, $params, $ttl, $cacheName);
}
}
@ -376,14 +377,14 @@ if (! function_exists('lang'))
*/
function lang(string $line, array $args = [], string $locale = null)
{
return Services::language($locale)->getLine($line, $args);
return Services::language($locale)
->getLine($line, $args);
}
}
//--------------------------------------------------------------------
if (! function_exists('log_message'))
{
/**
@ -414,6 +415,7 @@ if ( ! function_exists('log_message'))
if (ENVIRONMENT == 'testing')
{
$logger = new \CodeIgniter\Log\TestLogger(new \Config\Logger());
return $logger->log($level, $message, $context);
}
@ -477,11 +479,12 @@ if ( ! function_exists('remove_invisible_characters'))
*
* @param string
* @param bool
*
* @return string
*/
function remove_invisible_characters($str, $url_encoded = TRUE)
function remove_invisible_characters($str, $url_encoded = true)
{
$non_displayables = array();
$non_displayables = [];
// every control character except newline (dec 10),
// carriage return (dec 13) and horizontal tab (dec 09)
@ -496,8 +499,7 @@ if ( ! function_exists('remove_invisible_characters'))
do
{
$str = preg_replace($non_displayables, '', $str, -1, $count);
}
while ($count);
} while ($count);
return $str;
}
@ -631,8 +633,14 @@ if (! function_exists('force_https'))
*/
function force_https(int $duration = 31536000, RequestInterface $request = null, ResponseInterface $response = null)
{
if (is_null($request)) $request = Services::request(null, true);
if (is_null($response)) $response = Services::response(null, true);
if (is_null($request))
{
$request = Services::request(null, true);
}
if (is_null($response))
{
$response = Services::response(null, true);
}
if ($request->isSecure())
{
@ -643,7 +651,8 @@ if (! function_exists('force_https'))
// the session ID for safety sake.
if (class_exists('Session', false))
{
Services::session(null, true)->regenerate();
Services::session(null, true)
->regenerate();
}
$uri = $request->uri;
@ -690,7 +699,7 @@ if (! function_exists('redirect'))
$uri = $route;
}
$response->redirect($uri);
return $response->redirect($uri);
}
}
@ -719,7 +728,7 @@ if (! function_exists('redirect_with_input'))
$input = [
'get' => $_GET ?? [],
'post' => $_POST ?? []
'post' => $_POST ?? [],
];
$session->setFlashdata('_ci_old_input', $input);
@ -740,9 +749,10 @@ if ( ! function_exists('stringify_attributes'))
*
* @param mixed string, array, object
* @param bool
*
* @return string
*/
function stringify_attributes($attributes, $js = FALSE) : string
function stringify_attributes($attributes, $js = false): string
{
$atts = '';
@ -781,7 +791,9 @@ if ( ! function_exists('is_really_writable'))
* on Unix servers if safe_mode is on.
*
* @link https://bugs.php.net/bug.php?id=54709
*
* @param string
*
* @return bool
*/
function is_really_writable($file)
@ -798,23 +810,25 @@ if ( ! function_exists('is_really_writable'))
if (is_dir($file))
{
$file = rtrim($file, '/').'/'.md5(mt_rand());
if (($fp = @fopen($file, 'ab')) === FALSE)
if (($fp = @fopen($file, 'ab')) === false)
{
return FALSE;
return false;
}
fclose($fp);
@chmod($file, 0777);
@unlink($file);
return TRUE;
return true;
}
elseif ( ! is_file($file) OR ($fp = @fopen($file, 'ab')) === FALSE)
elseif (! is_file($file) OR ($fp = @fopen($file, 'ab')) === false)
{
return FALSE;
return false;
}
fclose($fp);
return TRUE;
return true;
}
}
@ -828,6 +842,7 @@ if ( ! function_exists('slash_item'))
* Fetch a config file item with slash appended (if not empty)
*
* @param string $item Config item name
*
* @return string|null The configuration item or NULL if
* the item doesn't exist
*/

View File

@ -64,4 +64,54 @@ class CommomFunctionsTest extends \CIUnitTestCase
$this->assertEquals('bar', env('foo', 'baz'));
}
public function testRedirectReturnsNamedRouteFirst()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$response = $this->createMock(\CodeIgniter\HTTP\Response::class);
$routes = new \CodeIgniter\Router\RouteCollection();
\CodeIgniter\Services::injectMock('response', $response);
\CodeIgniter\Services::injectMock('routes', $routes);
$routes->add('home/base', 'Controller::index', ['as' => 'base']);
$response->method('redirect')
->will($this->returnArgument(0));
$this->assertEquals('/home/base', redirect('base'));
}
public function testRedirectReverseRouting()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$response = $this->createMock(\CodeIgniter\HTTP\Response::class);
$routes = new \CodeIgniter\Router\RouteCollection();
\CodeIgniter\Services::injectMock('response', $response);
\CodeIgniter\Services::injectMock('routes', $routes);
$routes->add('home/base', 'Controller::index', ['as' => 'base']);
$response->method('redirect')
->will($this->returnArgument(0));
$this->assertEquals('/home/base', redirect('Controller::index'));
}
public function testRedirectNormalRouting()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$response = $this->createMock(\CodeIgniter\HTTP\Response::class);
$routes = new \CodeIgniter\Router\RouteCollection();
\CodeIgniter\Services::injectMock('response', $response);
\CodeIgniter\Services::injectMock('routes', $routes);
$response->method('redirect')
->will($this->returnArgument(0));
$this->assertEquals('/home/base', redirect('/home/base'));
$this->assertEquals('home/base', redirect('home/base'));
}
}