From da379d0849161a59f2892717fb054170de4da1e7 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sun, 28 Sep 2014 08:24:02 -0400 Subject: [PATCH] Knocked out unit tests for the new Router --- phpunit.xml | 1 + src/Resource.php | 3 +- src/Router.php | 14 ++++++- tests/RouterTest.php | 94 ++++++++++++++++++-------------------------- 4 files changed, 54 insertions(+), 58 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index e6d556d..016e823 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -4,6 +4,7 @@ xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.2/phpunit.xsd" bootstrap="tests/Bootstrap.php" colors="true" + stderr="true" > diff --git a/src/Resource.php b/src/Resource.php index ac22de6..a92a258 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -374,8 +374,7 @@ class Resource extends Object } catch (\Exception $e) { - $this->status = 400; - $this->message = $e->getMessage(); + throw $e; } } diff --git a/src/Router.php b/src/Router.php index 4a067e4..90c8cf8 100644 --- a/src/Router.php +++ b/src/Router.php @@ -66,6 +66,12 @@ class Router extends Object array_unshift($nouns, '', $this->config->pickles['namespace'], 'Resources', $version); $class = implode('\\', $nouns); + // Strips preceding slashs when there is no namespace + if (strpos($class, '\\\\') === 0) + { + $class = substr($class, 2); + } + // Checks that the file is present and contains our class if (!class_exists($class)) { @@ -85,7 +91,13 @@ class Router extends Object $code = $e->getCode(); - $resource->status = $code ? $code : 400; + // Anything below 200 is probably a PHP error + if ($code < 200) + { + $code = 500; + } + + $resource->status = $code; $resource->message = $e->getMessage(); } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index 7f5863e..e4c7902 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -1,63 +1,47 @@ config = Pickles\Config::getInstance(); - $this->config->data['pickles']['disabled'] = false; - $this->config->data['pickles']['profiler'] = false; - setUpRequest('home'); - - $module = 'assertTrue(in_array('Location: https://testsite.com/secure', xdebug_get_headers())); - } - - public function testForceInsecure() - { - setUpRequest('insecure'); - $_SERVER['HTTPS'] = 'on'; - - $module = 'assertTrue(in_array('Location: http://testsite.com/insecure', xdebug_get_headers())); - } - - public function testValidationErrors() - { - setUpRequest('validationerrors'); - - $module = ' "bar"]; }' - . '}'; - - file_put_contents(SITE_MODULE_PATH . 'validationerrors.php', $module); - - new Pickles\Router(); - - $this->expectOutputString('{"status":"error","message":"The test field is required."}'); + } +} + +namespace +{ + class RouterTest extends PHPUnit_Framework_TestCase + { + public function testServerError() + { + $this->expectOutputRegex('/{"status":500,"message":"Undefined index: request"}/'); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + + new Pickles\Router(); + } + + public function testNotFound() + { + $this->expectOutputRegex('/{"status":404,"message":"Not Found."}/'); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_REQUEST['request'] = 'v1/test'; + + new Pickles\Router(); + } + + // We're just testing that the class can be loaded, not that it will + // work. That logic is off in ResourceTest + public function testFound() + { + $this->expectOutputRegex('/{"status":405,"message":"Method not allowed."}/'); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $_REQUEST['request'] = 'v1/resource/1'; + + new Pickles\Router(); + } } }