Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Slim/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,22 @@ public function getBasePath()
public function setCacheFile($cacheFile)
{
if (!is_string($cacheFile) && $cacheFile !== false) {
throw new InvalidArgumentException('Router cacheFile must be a string or false');
throw new InvalidArgumentException('Router cache file must be a string or false');
}

$this->cacheFile = $cacheFile;

if ($cacheFile !== false && !is_writable(dirname($cacheFile))) {
throw new RuntimeException('Router cacheFile directory must be writable');
if ($cacheFile && file_exists($cacheFile) && !is_readable($cacheFile)) {
throw new RuntimeException(
sprintf('Router cache file `%s` is not readable', $cacheFile)
);
}

if ($cacheFile && !file_exists($cacheFile) && !is_writable(dirname($cacheFile))) {
throw new RuntimeException(
sprintf('Router cache file directory `%s` is not writable', dirname($cacheFile))
);
}

$this->cacheFile = $cacheFile;
return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"autoload-dev": {
"files": [
"tests/Assets/HeaderFunctions.php"
"tests/Assets/PhpFunctionOverrides.php"
]
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,31 @@ function header($string, $replace = true, $statusCode = null)
]
);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rename this file now that it containers functions other than header functions. Maybe PhpFunctions.php?

/**
* Is a file descriptor writable
*
* @param $file
* @return bool
*/
function is_readable($file)
{
if (stripos($file, 'non-readable.cache') !== false) {
return false;
}
return true;
}

/**
* Is a path writable
*
* @param $path
* @return bool
*/
function is_writable($path)
{
if (stripos($path, 'non-writable-directory') !== false) {
return false;
}
return true;
}
40 changes: 33 additions & 7 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@
* @copyright Copyright (c) 2011-2017 Josh Lockhart
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
*/

namespace Slim\Tests;

use Slim\Container;
use Slim\Router;

class RouterTest extends \PHPUnit_Framework_TestCase
{
/** @var Router */
protected $router;

/** @var string */
protected $cacheFile;

public function setUp()
{
$this->router = new Router;
}

public function tearDown()
{
if (file_exists($this->cacheFile)) {
unlink($this->cacheFile);
}
}

public function testMap()
{
$methods = ['GET'];
Expand Down Expand Up @@ -333,24 +343,40 @@ public function testSettingInvalidCacheFileValue()
{
$this->setExpectedException(
'\InvalidArgumentException',
'Router cacheFile must be a string'
'Router cache file must be a string'
);
$this->router->setCacheFile(['invalid']);
}

/**
* Test if cacheFile is not a writable directory
* Test cache file exists but is not writable
*/
public function testSettingInvalidCacheFileNotExisting()
public function testCacheFileExistsAndIsNotReadable()
{
$this->cacheFile = __DIR__ . '/non-readable.cache';
file_put_contents($this->cacheFile, '<?php return []; ?>');

$this->setExpectedException(
'\RuntimeException',
'Router cacheFile directory must be writable'
sprintf('Router cache file `%s` is not readable', $this->cacheFile)
);

$this->router->setCacheFile(
dirname(__FILE__) . uniqid(microtime(true)) . '/' . uniqid(microtime(true))
$this->router->setCacheFile($this->cacheFile);
}

/**
* Test cache file does not exist and directory is not writable
*/
public function testCacheFileDoesNotExistsAndDirectoryIsNotWritable()
{
$cacheFile = __DIR__ . '/non-writable-directory/router.cache';

$this->setExpectedException(
'\RuntimeException',
sprintf('Router cache file directory `%s` is not writable', dirname($cacheFile))
);

$this->router->setCacheFile($cacheFile);
}

/**
Expand Down