From 431d1a5633920b3b083214cf22aa1aa2aade6d90 Mon Sep 17 00:00:00 2001 From: l0gicgate Date: Fri, 22 Feb 2019 16:53:59 -0700 Subject: [PATCH 1/4] refactor logic in Router::setCacheFile() --- Slim/Router.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Slim/Router.php b/Slim/Router.php index db7106f68..d6af16442 100644 --- a/Slim/Router.php +++ b/Slim/Router.php @@ -170,12 +170,19 @@ public function setBasePath(string $basePath): self */ public function setCacheFile(?string $cacheFile): RouterInterface { - $this->cacheFile = $cacheFile; + if ($cacheFile && file_exists($cacheFile) && !is_readable($cacheFile)) { + throw new RuntimeException( + sprintf('Router cache file `%s` is not readable', $cacheFile) + ); + } - if (is_string($cacheFile) && !is_writable(dirname($cacheFile))) { - throw new RuntimeException('Router cacheFile directory must be writable'); + 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; } From c64e9b00a28b490e441d3f7efd086a2cb3d5b4bf Mon Sep 17 00:00:00 2001 From: l0gicgate Date: Fri, 22 Feb 2019 16:54:36 -0700 Subject: [PATCH 2/4] add PhpFunctionOverrides and rename HeaderFunctions to HeaderStack --- composer.json | 2 +- .../{HeaderFunctions.php => HeaderStack.php} | 32 +------ tests/Assets/PhpFunctionOverrides.php | 87 +++++++++++++++++++ 3 files changed, 90 insertions(+), 31 deletions(-) rename tests/Assets/{HeaderFunctions.php => HeaderStack.php} (79%) create mode 100644 tests/Assets/PhpFunctionOverrides.php diff --git a/composer.json b/composer.json index 49415e2e3..5bbefefa9 100644 --- a/composer.json +++ b/composer.json @@ -53,7 +53,7 @@ }, "autoload-dev": { "files": [ - "tests/Assets/HeaderFunctions.php" + "tests/Assets/PhpFunctionOverrides.php" ] }, "scripts": { diff --git a/tests/Assets/HeaderFunctions.php b/tests/Assets/HeaderStack.php similarity index 79% rename from tests/Assets/HeaderFunctions.php rename to tests/Assets/HeaderStack.php index 3ab2be304..80e90c187 100644 --- a/tests/Assets/HeaderFunctions.php +++ b/tests/Assets/HeaderStack.php @@ -6,7 +6,7 @@ * We put these into the Slim namespace, so that Slim\App will use these versions of header() and * headers_sent() when we test its output. */ -namespace Slim; +namespace Slim\Tests\Assets; /** * Zend Framework (http://framework.zend.com/) @@ -31,7 +31,7 @@ /** * Store output artifacts */ -class HeaderStackTestAsset +class HeaderStack { /** * @var string[][] @@ -84,31 +84,3 @@ public static function has($header) return false; } } - -/** - * Have headers been sent? - * - * @return false - */ -function headers_sent() -{ - return false; -} - -/** - * Emit a header, without creating actual output artifacts - * - * @param string $string - * @param bool $replace - * @param int|null $statusCode - */ -function header($string, $replace = true, $statusCode = null) -{ - HeaderStackTestAsset::push( - [ - 'header' => $string, - 'replace' => $replace, - 'status_code' => $statusCode, - ] - ); -} diff --git a/tests/Assets/PhpFunctionOverrides.php b/tests/Assets/PhpFunctionOverrides.php new file mode 100644 index 000000000..3458e5433 --- /dev/null +++ b/tests/Assets/PhpFunctionOverrides.php @@ -0,0 +1,87 @@ + $string, + 'replace' => $replace, + 'status_code' => $statusCode, + ] + ); +} + +/** + * 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; +} From af678408163f2136c186f2211cfb4615bd8d7e43 Mon Sep 17 00:00:00 2001 From: l0gicgate Date: Fri, 22 Feb 2019 16:54:56 -0700 Subject: [PATCH 3/4] refactor RouterTest and ResponseEmitterTest --- tests/ResponseEmitterTest.php | 14 ++++++------ tests/RouterTest.php | 41 +++++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/tests/ResponseEmitterTest.php b/tests/ResponseEmitterTest.php index ccf14721e..a6d30f339 100644 --- a/tests/ResponseEmitterTest.php +++ b/tests/ResponseEmitterTest.php @@ -8,8 +8,8 @@ */ namespace Slim\Tests; -use Slim\HeaderStackTestAsset; use Slim\ResponseEmitter; +use Slim\Tests\Assets\HeaderStack; use Slim\Tests\Mocks\MockStream; use Slim\Tests\Mocks\SmallChunksStream; @@ -21,12 +21,12 @@ class ResponseEmitterTest extends TestCase { public function setUp() { - HeaderStackTestAsset::reset(); + HeaderStack::reset(); } public function tearDown() { - HeaderStackTestAsset::reset(); + HeaderStack::reset(); } public function testRespond() @@ -47,8 +47,8 @@ public function testRespondNoContent() $responseEmitter = new ResponseEmitter(); $responseEmitter->emit($response); - $this->assertEquals(false, HeaderStackTestAsset::has('Content-Type')); - $this->assertEquals(false, HeaderStackTestAsset::has('Content-Length')); + $this->assertEquals(false, HeaderStack::has('Content-Type')); + $this->assertEquals(false, HeaderStack::has('Content-Length')); $this->expectOutputString(''); } @@ -143,7 +143,7 @@ public function testResponseReplacesPreviouslySetHeaders() ['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200], ]; - $this->assertSame($expectedStack, HeaderStackTestAsset::stack()); + $this->assertSame($expectedStack, HeaderStack::stack()); } public function testResponseDoesNotReplacePreviouslySetSetCookieHeaders() @@ -161,7 +161,7 @@ public function testResponseDoesNotReplacePreviouslySetSetCookieHeaders() ['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200], ]; - $this->assertSame($expectedStack, HeaderStackTestAsset::stack()); + $this->assertSame($expectedStack, HeaderStack::stack()); } public function testIsResponseEmptyWithNonEmptyBodyAndTriggeringStatusCode() diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 8f52f8ab0..d26916b36 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -23,6 +23,16 @@ class RouterTest extends TestCase /** @var Router */ protected $router; + /** @var string */ + protected $cacheFile; + + public function tearDown() + { + if (file_exists($this->cacheFile)) { + unlink($this->cacheFile); + } + } + public function setUp() { $callableResolver = new CallableResolver(); @@ -315,16 +325,33 @@ public function testPathForWithModifiedRoutePattern() } /** - * Test if cacheFile is not a writable directory - * - * @expectedException \RuntimeException - * @expectedExceptionMessage Router cacheFile directory must be writable + * Test cache file exists but is not writable */ - public function testSettingInvalidCacheFileNotExisting() + public function testCacheFileExistsAndIsNotReadable() { - $this->router->setCacheFile( - dirname(__FILE__) . uniqid(microtime(true)) . '/' . uniqid(microtime(true)) + $this->cacheFile = __DIR__ . '/non-readable.cache'; + file_put_contents($this->cacheFile, ''); + + $this->expectException( + '\RuntimeException', + sprintf('Router cache file `%s` is not readable', $this->cacheFile) ); + + $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->expectException( + '\RuntimeException', + sprintf('Router cache file directory `%s` is not writable', dirname($cacheFile)) + ); + + $this->router->setCacheFile($cacheFile); } /** From f735bb69ca66a8cbf7efcf45a586301d2af9a43a Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Sat, 2 Mar 2019 11:20:42 +0000 Subject: [PATCH 4/4] Update docblock in PhpFunctionOverrides --- tests/Assets/PhpFunctionOverrides.php | 29 +++++---------------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/tests/Assets/PhpFunctionOverrides.php b/tests/Assets/PhpFunctionOverrides.php index 3458e5433..c7cbbbb33 100644 --- a/tests/Assets/PhpFunctionOverrides.php +++ b/tests/Assets/PhpFunctionOverrides.php @@ -1,35 +1,16 @@