summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Router/Runner/PhpScript.php27
-rw-r--r--tests/Router/Runner/ControllerTest.php1
-rw-r--r--tests/Router/Runner/PhpScriptTest.php148
3 files changed, 112 insertions, 64 deletions
diff --git a/src/Router/Runner/PhpScript.php b/src/Router/Runner/PhpScript.php
index d223bd7..51480c5 100644
--- a/src/Router/Runner/PhpScript.php
+++ b/src/Router/Runner/PhpScript.php
@@ -10,7 +10,18 @@ use Psr\Http\Message\ResponseInterface;
* Route to a PHP script
*/
class PhpScript extends Runner
-{
+{
+ /**
+ * Include a file
+ * @param type $file
+ * @param ServerRequestInterface $request
+ * @param ResponseInterface $response
+ */
+ protected function includeScript($file, ServerRequestInterface $request, ResponseInterface $response)
+ {
+ return include $file;
+ }
+
/**
* Route to a file
*
@@ -23,16 +34,16 @@ class PhpScript extends Runner
$route = $request->getAttribute('route');
$file = !empty($route->file) ? ltrim($route->file, '/') : '';
- if (!file_exists($file)) {
- throw new \RuntimeException("Failed to route using '$file': File '$file' doesn't exist.");
- }
-
if ($file[0] === '~' || strpos($file, '..') !== false) {
- throw new \RuntimeException("Won't route using '$file': '~', '..' are not allowed in filename.");
+ throw new \RuntimeException("Won't route to '$file': '~', '..' are not allowed in filename");
}
- $result = include $file;
+ if (!file_exists($file)) {
+ throw new \RuntimeException("Failed to route using '$file': File doesn't exist");
+ }
+
+ $result = $this->includeScript($file, $request, $response);
- return $result === true ? $response : $result;
+ return $result === true || $result === 1 ? $response : $result;
}
}
diff --git a/tests/Router/Runner/ControllerTest.php b/tests/Router/Runner/ControllerTest.php
index f8fe12e..694fe5b 100644
--- a/tests/Router/Runner/ControllerTest.php
+++ b/tests/Router/Runner/ControllerTest.php
@@ -34,6 +34,7 @@ class ControllerTest extends \PHPUnit_Framework_TestCase
$runner->expects($this->once())->method('instantiate')->with($class)->willReturn($controller);
$result = $runner($request, $response);
+
$this->assertSame($finalResponse, $result);
}
diff --git a/tests/Router/Runner/PhpScriptTest.php b/tests/Router/Runner/PhpScriptTest.php
index 9ecab8a..658998b 100644
--- a/tests/Router/Runner/PhpScriptTest.php
+++ b/tests/Router/Runner/PhpScriptTest.php
@@ -3,90 +3,126 @@
namespace Jasny\Router;
use Jasny\Router\Route;
-use Jasny\Router\Runner\PhpScript;
+use Jasny\Router\Runner;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
+use org\bovigo\vfs\vfsStream;
+use org\bovigo\vfs\vfsStreamDirectory;
+
+/**
+ * @covers Jasny\Router\Runner\PhpScript
+ */
class PhpScriptTest extends \PHPUnit_Framework_TestCase
{
/**
- * Test creating PhpScript runner
- *
- * @dataProvider phpScriptProvider
- * @param Route $route
- * @param boolean $positive
+ * @var vfsStreamDirectory
*/
- public function testPhpScript($route, $positive)
- {
- $runner = new PhpScript($route);
+ protected $root;
+
+ public function setUp()
+ {
+ $this->root = vfsStream::setup('root');
+ $this->root->addChild(vfsStream::newFile('true.php')->setContent('<?php ?>'));
+ $this->root->addChild(vfsStream::newFile('foo.php')->setContent('<?php return "foo"; ?>'));
+ }
+ public function testInvoke()
+ {
$request = $this->createMock(ServerRequestInterface::class);
$response = $this->createMock(ResponseInterface::class);
- $request->expects($this->once())->method('getAttribute')->with($this->equalTo('route'))->will($this->returnValue($route));
-
- if (!$positive) $this->expectException(\RuntimeException::class);
- $result = $runner->run($request, $response);
+ $finalResponse = $this->createMock(ResponseInterface::class);
- if ($route->type === 'returnTrue') {
- $this->assertEquals($response, $result, "Request object was not returned as result");
- } else {
- $this->assertEquals($request, $result['request'], "Request object was not passed correctly to result");
- $this->assertEquals($response, $result['response'], "Response object was not passed correctly to result");
- }
+ $route = $this->createMock(Route::class);
+ $route->file = vfsStream::url('root/foo.php');
+
+ $request->expects($this->once())->method('getAttribute')->with('route')->willReturn($route);
+
+ $runner = $this->getMockBuilder(Runner\PhpScript::class)->setMethods(['includeScript'])->getMock();
+ $runner->expects($this->once())->method('includeScript')
+ ->with(vfsStream::url('root/foo.php'), $request, $response)
+ ->willReturn($finalResponse);
- unlink($route->file);
+ $result = $runner($request, $response);
+
+ $this->assertSame($finalResponse, $result);
}
-
- /**
- * Provide data fpr testing 'create' method
- */
+
public function phpScriptProvider()
{
+ $routeTrue = $this->createMock(Route::class);
+ $routeTrue->file = vfsStream::url('root/true.php');
+
+ $routeFoo = $this->createMock(Route::class);
+ $routeFoo->file = vfsStream::url('root/foo.php');
+
return [
- [Route::create(['test' => 'test']), false],
- [Route::create(['fn' => 'testFunction', 'value' => 'test']), false],
- [Route::create(['controller' => 'TestController', 'value' => 'test']), false],
- [Route::create(['file' => '', 'value' => 'test']), false],
- [Route::create(['file' => 'some_file.php', 'value' => 'test']), false],
- [Route::create(['file' => '../' . basename(getcwd()), 'value' => 'test']), false],
- [Route::create(['file' => $this->createTmpScript('returnTrue'), 'type' => 'returnTrue']), true],
- [Route::create(['file' => $this->createTmpScript('returnNotTrue'), 'type' => 'returnNotTrue']), true]
+ [$routeTrue, 1],
+ [$routeFoo, 'foo']
];
}
-
+
/**
- * Create single tmp script file for testing
- *
- * @param string $type ('returnTrue', 'returnNotTrue')
- * @return string $path
+ * @dataProvider phpScriptProvider
+ *
+ * @param Route $route
+ * @param mixed $expected
*/
- public function createTmpScript($type)
+ public function testInvokeIncludeScript($route, $expected)
{
- $dir = rtrim(sys_get_temp_dir(), '/');
-
- do {
- $name = $this->getRandomString() . '-test-script.php';
- $path = $dir . '/' . $name;
+ $request = $this->createMock(ServerRequestInterface::class);
+ $response = $this->createMock(ResponseInterface::class);
+ $request->expects($this->once())->method('getAttribute')->with($this->equalTo('route'))->will($this->returnValue($route));
- if (!file_exists($path)) break;
- } while (true);
+ $runner = new Runner\PhpScript($route);
+
+ if ($expected === 1) {
+ $expected = $response;
+ }
+
+ $result = $runner->run($request, $response);
+
+ $this->assertSame($expected, $result);
+ }
- $content = $type === 'returnTrue' ? "<?php\n return true;" : "<?php\n return ['request' => \$request, 'response' => \$response];";
- $bytes = file_put_contents($path, $content);
+ /**
+ * @expectedException \RuntimeException
+ * @expectedExceptionMessage Failed to route using 'vfs://root/bar.php': File doesn't exist
+ */
+ public function testInvokeWithNonExistingFile()
+ {
+ $request = $this->createMock(ServerRequestInterface::class);
+ $response = $this->createMock(ResponseInterface::class);
- $this->assertTrue((int)$bytes > 0);
+ $route = $this->createMock(Route::class);
+ $route->file = vfsStream::url('root/bar.php');
+
+ $request->expects($this->once())->method('getAttribute')->with('route')->willReturn($route);
+
+ $runner = $this->getMockBuilder(Runner\PhpScript::class)->setMethods(['includeScript'])->getMock();
+ $runner->expects($this->never())->method('includeScript');
- return $path;
+ $runner($request, $response);
}
/**
- * Get random string of given length (no more then length of md5 hash)
- *
- * @param int $length
- * @return string
+ * @expectedException \RuntimeException
+ * @expectedExceptionMessage Won't route to 'vfs://root/../bar.php': '~', '..' are not allowed in filename
*/
- public function getRandomString($length = 10)
- {
- return substr(md5(microtime(true) * mt_rand()), 0, $length);
+ public function testInvokeWithIlligalFilename()
+ {
+ $request = $this->createMock(ServerRequestInterface::class);
+ $response = $this->createMock(ResponseInterface::class);
+
+ $route = $this->createMock(Route::class);
+ $route->file = vfsStream::url('root/../bar.php');
+
+ $request->expects($this->once())->method('getAttribute')->with('route')->willReturn($route);
+
+ $runner = $this->getMockBuilder(Runner\PhpScript::class)->setMethods(['includeScript'])->getMock();
+ $runner->expects($this->never())->method('includeScript');
+
+ $runner($request, $response);
}
+
}