Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Exception handling almost completely contained within `Zepto\Router
Browse files Browse the repository at this point in the history
…`, removed extraneous code from ``Zepto\Zepto`` and updated test classes.
  • Loading branch information
hassankhan committed Feb 17, 2014
1 parent 7c9b982 commit 0e1cee5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
13 changes: 10 additions & 3 deletions library/Zepto/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,15 @@ public function match($url, $http_method = self::METHOD_GET)
public function run()
{
// If no routes have been added, then throw an exception
if (empty($this->routes)) {
throw new \RuntimeException('No routes exist in the routing table. Add some');
try {
if (empty($this->routes)) {
throw new \RuntimeException('No routes exist in the routing table. Add some');
}
}
catch (\Exception $e) {
$this->current_http_status = \Symfony\Component\HttpFoundation\Response::HTTP_INTERNAL_SERVER_ERROR;
$this->error($e);
return FALSE;
}

// Try and get a matching route for the current URL
Expand Down Expand Up @@ -391,7 +398,7 @@ public function not_found($callback = null)
*/
protected function default_not_found_handler()
{
return $this->generate_error_template('Page Not Found', "Couldn't find your, like, page, dude");
return $this->generate_error_template('Page Not Found', "Couldn't find your, like, page, dude" . $this->request->getPathInfo());
}

/**
Expand Down
15 changes: 7 additions & 8 deletions library/Zepto/Zepto.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,15 @@ function($c) use ($settings) {
}

/**
* Executes router and returns result of callback function for specified route
* Executes router and returns true for a successful route,
*
* @return
* @return bool|null
*/
public function run()
{
$this->run_hooks('before_response_send');
try {
return $this->app['router']->run();
} catch (\Exception $e) {
$this->app['router']->error($e);
}
$this->run_hooks('after_response_send');
return $this->app['router']->run();
// $this->run_hooks('after_response_send');
}

/**
Expand Down Expand Up @@ -256,6 +252,9 @@ protected function setup_router()
// Render template with Twig
return $app['twig']->render($template_name, $options);
});

// If a file's name is 404.md or 500.md, use that to set an ErrorRoute with them

}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Zepto/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ public function testRunWithParameters()

/**
* @covers Zepto\Router::run
* @expectedException RuntimeException
*/
public function testRunBeforeAddingRoutes()
{
$this->router->run();
$this->assertFalse($this->router->run());
$this->assertEquals(500, $this->router->current_http_status());
}

/**
Expand Down
26 changes: 16 additions & 10 deletions tests/Zepto/ZeptoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ protected function setUp()
$_SERVER['SERVER_NAME'] = 'zepto';
$_SERVER['SERVER_PORT'] = '80';
$_SERVER['SCRIPT_NAME'] = '/zepto/';
$_SERVER['REQUEST_URL'] = '/zepto/';
$_SERVER['REQUEST_URI'] = '/zepto/';
$_SERVER['PATH_INFO'] = '/bar/xyz';
$_SERVER['REQUEST_URL'] = '';
$_SERVER['REQUEST_URI'] = '';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['QUERY_STRING'] = 'one=1&two=2&three=3';
$_SERVER['QUERY_STRING'] = '';
$_SERVER['HTTPS'] = '';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
unset($_SERVER['CONTENT_TYPE'], $_SERVER['CONTENT_LENGTH']);
Expand Down Expand Up @@ -197,18 +196,25 @@ public function testRouterSetup()

/**
* @covers Zepto\Zepto::run
* @todo Implement testRun().
*/
public function testRun()
{
ob_start();
$zepto = new Zepto();
$this->assertTrue($zepto->run());
ob_end_clean();
}

// Check to see that the index page has loaded
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
/**
* @covers Zepto\Zepto::run
*/
public function testRunError()
{
$_SERVER['REQUEST_URL'] = '/non-existent';
$_SERVER['REQUEST_URI'] = '/non-existent';
ob_start();
$zepto = new Zepto();
$this->assertFalse($zepto->run());
ob_end_clean();
}

Expand Down

0 comments on commit 0e1cee5

Please sign in to comment.