Skip to content

Commit

Permalink
Fix merge conflicts in RouteTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Lockhart committed Apr 5, 2014
2 parents 7c682ad + b2d05e0 commit de74092
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Slim/Slim.php
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,10 @@ public function clearHooks($name = null)
*/
public function add(\Slim\Middleware $newMiddleware)
{
if(in_array($newMiddleware, $this->middleware)) {
$middleware_class = get_class($newMiddleware);
throw new \RuntimeException("Circular Middleware setup detected. Tried to queue the same Middleware instance ({$middleware_class}) twice.");
}
$newMiddleware->setApplication($this);
$newMiddleware->setNextMiddleware($this->middleware[0]);
array_unshift($this->middleware, $newMiddleware);
Expand Down Expand Up @@ -1295,6 +1299,8 @@ public function run()
echo $body;
}

$this->applyHook('slim.after');

restore_error_handler();
}

Expand Down Expand Up @@ -1333,7 +1339,6 @@ public function call()
$this->stop();
} catch (\Slim\Exception\Stop $e) {
$this->response()->write(ob_get_clean());
$this->applyHook('slim.after');
} catch (\Exception $e) {
if ($this->config('debug')) {
throw $e;
Expand Down
12 changes: 12 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ public function testGetCallableAsClassLazyInitialize()
$this->assertTrue(LazyInitializeTestClass::$initialized);
}

public function testGetCallableAsClassLazyInitialize()
{
LazyInitializeTestClass::$initialized = false;

$route = new \Slim\Route('/foo', '\LazyInitializeTestClass:foo');
$this->assertFalse(LazyInitializeTestClass::$initialized);

$route->dispatch();
$this->assertTrue(LazyInitializeTestClass::$initialized);
}


public function testGetCallableAsStaticMethod()
{
$route = new \Slim\Route('/bar', '\Slim\Slim::getInstance');
Expand Down
27 changes: 25 additions & 2 deletions tests/SlimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,15 +805,19 @@ public function testExpiresAsString()
'SCRIPT_NAME' => '/foo', //<-- Physical
'PATH_INFO' => '/bar', //<-- Virtual
));
$expectedDate = gmdate('D, d M Y H:i:s T', strtotime('5 days'));
$s = new \Slim\Slim();
$s->get('/bar', function () use ($s) {
$s->expires('5 days');
});
$s->call();
list($status, $header, $body) = $s->response()->finalize();
$this->assertTrue(isset($header['Expires']));
$this->assertEquals($header['Expires'], $expectedDate);

$this->assertEquals(
strtotime('5 days'),
strtotime($header['Expires']),
1 // delta
);
}

/**
Expand Down Expand Up @@ -1228,6 +1232,25 @@ public function testAddMiddleware()
$this->assertEquals('Hello', $s->response()->header('X-Slim-Test'));
}

/**
* Test exception when adding circular middleware queues
*
* This asserts that the same middleware can NOT be queued twice (usually by accident).
* Circular middleware stack causes a troublesome to debug PHP Fatal error:
*
* > Fatal error: Maximum function nesting level of '100' reached. aborting!
*/
public function testFailureWhenAddingCircularMiddleware()
{
$this->setExpectedException('\RuntimeException');
$middleware = new CustomMiddleware;
$s = new \Slim\Slim;
$s->add($middleware);
$s->add(new CustomMiddleware);
$s->add($middleware);
$s->run();
}

/************************************************
* FLASH MESSAGING
************************************************/
Expand Down

0 comments on commit de74092

Please sign in to comment.