Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #2568 #2586

Merged
merged 17 commits into from Feb 22, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 36 additions & 5 deletions Slim/Router.php
Expand Up @@ -130,20 +130,51 @@ public function getBasePath()
*/
public function setCacheFile($cacheFile)
{
$this->cacheFile = $cacheFile;
l0gicgate marked this conversation as resolved.
Show resolved Hide resolved

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) && !$this->isCacheFileReadable($cacheFile)) {
throw new RuntimeException(
sprintf('Router cache file `%s` is not readable', $cacheFile)
);
}

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

return $this;
}

/**
* This method is in place to facilitate unit tests
*
* @param $cacheFile
*
* @return bool
*/
protected function isCacheFileReadable($cacheFile)
l0gicgate marked this conversation as resolved.
Show resolved Hide resolved
{
return is_readable($cacheFile);
}

/**
* This method is in place to facilitate unit tests
*
* @param $cacheDir
*
* @return bool
*/
protected function isCacheDirWritable($cacheDir)
{
return is_writable($cacheDir);
}

/**
* @param ContainerInterface $container
*/
Expand Down
62 changes: 55 additions & 7 deletions tests/RouterTest.php
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,62 @@ 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__ . '/' . time() . '.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))
$mock = $this
->getMockBuilder(Router::class)
->disableOriginalConstructor()
->setMethods(['isCacheFileReadable'])
->getMock();

$mock->expects($this->any())
->method('isCacheFileReadable')
->will($this->returnValue(false));

/** @var Router $mock */
$mock->setCacheFile($this->cacheFile);
}

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

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

$mock = $this
->getMockBuilder(Router::class)
->disableOriginalConstructor()
->setMethods(['isCacheDirWritable'])
->getMock();

$mock->expects($this->any())
->method('isCacheDirWritable')
->will($this->returnValue(false));

/** @var Router $mock */
$mock->setCacheFile($cacheFile);
}

/**
Expand Down