Skip to content

Commit

Permalink
Test route caching (#30) (#31)
Browse files Browse the repository at this point in the history
- Test route caching
  • Loading branch information
ivanvermeyen authored Sep 15, 2020
1 parent 92ed3a4 commit 33bfafa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct($routes, Request $request, $assetRoot = null)
if (!$routes instanceof RouteCollection && !(interface_exists('\Illuminate\Routing\RouteCollectionInterface') && is_subclass_of($routes, '\Illuminate\Routing\RouteCollectionInterface'))) {
throw new \InvalidArgumentException('The $routes parameter has to be of type RouteCollection or RouteCollectionInterface for L6+.');
}

parent::__construct($routes, $request, $assetRoot);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Stubs/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace CodeZero\LocalizedRoutes\Tests\Stubs;

class Controller extends \Illuminate\Routing\Controller
{
public function index()
{
return 'ok';
}
}
41 changes: 41 additions & 0 deletions tests/Unit/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodeZero\LocalizedRoutes\Tests\Unit;

use CodeZero\LocalizedRoutes\Tests\Stubs\Controller;
use CodeZero\LocalizedRoutes\Tests\Stubs\Model;
use CodeZero\LocalizedRoutes\Tests\TestCase;
use CodeZero\LocalizedRoutes\UrlGenerator;
Expand Down Expand Up @@ -228,6 +229,46 @@ public function it_generates_a_signed_route_url_for_a_specific_locale()
$this->get($tamperedUrl)->assertSee('Invalid Signature');
}

/** @test */
public function it_allows_routes_to_be_cached()
{
$this->withoutExceptionHandling();
$this->setSupportedLocales(['en']);
$this->setAppLocale('en');

Route::get('en/route', Controller::class.'@index');

$this->cacheRoutes();

$this->get('en/route')->assertSuccessful();
}

/**
* Cache registered routes.
*
* @return void
*/
protected function cacheRoutes()
{
$routes = Route::getRoutes();

foreach ($routes as $route) {
$route->prepareForSerialization();
}

$isLaravel7orGreater = method_exists($routes, 'compile');

if ($isLaravel7orGreater) {
$this->app['router']->setCompiledRoutes(
$routes->compile()
);

return;
}

$this->app['router']->setRoutes($routes);
}

/**
* Register a route.
*
Expand Down

0 comments on commit 33bfafa

Please sign in to comment.