Skip to content

Commit

Permalink
Improve routeCacheFile test
Browse files Browse the repository at this point in the history
Ensure that the cached routes file actually contains dispatchable
routes.
  • Loading branch information
akrabat committed May 8, 2016
1 parent f19cf30 commit 0f57290
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,17 @@ public function testSettingInvalidCacheFileNotExisting()
}

/**
* Test route dispatcher is created in case of route cache
* Test cached routes file is created & that it holds our routes.
*/
public function testCreateDispatcherWithRouteCache()
public function testRouteCacheFileCanBeDispatched()
{
$methods = ['GET'];
$pattern = '/hello/{first}/{last}';
$callable = function ($request, $response, $args) {
echo sprintf('Hello %s %s', $args['first'], $args['last']);
};
$route = $this->router->map($methods, $pattern, $callable)->setName('foo');

$cacheFile = dirname(__FILE__) . '/' . uniqid(microtime(true));
$this->router->setCacheFile($cacheFile);
$class = new \ReflectionClass($this->router);
Expand All @@ -363,6 +370,19 @@ public function testCreateDispatcherWithRouteCache()
$this->assertInstanceOf('\FastRoute\Dispatcher', $dispatcher);
$this->assertFileExists($cacheFile, 'cache file was not created');

// instantiate a new router & load the cached routes file & see if
// we can dispatch to the route we cached.
$router2 = new Router();
$router2->setCacheFile($cacheFile);

$class = new \ReflectionClass($router2);
$method = $class->getMethod('createDispatcher');
$method->setAccessible(true);

$dispatcher2 = $method->invoke($this->router);
$result = $dispatcher2->dispatch('GET', '/hello/josh/lockhart');
$this->assertSame(\FastRoute\Dispatcher::FOUND, $result[0]);

unlink($cacheFile);
}

Expand Down

0 comments on commit 0f57290

Please sign in to comment.