Skip to content

Commit 33bfafa

Browse files
authored
Test route caching (#30) (#31)
- Test route caching
1 parent 92ed3a4 commit 33bfafa

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/UrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct($routes, Request $request, $assetRoot = null)
2323
if (!$routes instanceof RouteCollection && !(interface_exists('\Illuminate\Routing\RouteCollectionInterface') && is_subclass_of($routes, '\Illuminate\Routing\RouteCollectionInterface'))) {
2424
throw new \InvalidArgumentException('The $routes parameter has to be of type RouteCollection or RouteCollectionInterface for L6+.');
2525
}
26-
26+
2727
parent::__construct($routes, $request, $assetRoot);
2828
}
2929

tests/Stubs/Controller.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace CodeZero\LocalizedRoutes\Tests\Stubs;
4+
5+
class Controller extends \Illuminate\Routing\Controller
6+
{
7+
public function index()
8+
{
9+
return 'ok';
10+
}
11+
}

tests/Unit/UrlGeneratorTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace CodeZero\LocalizedRoutes\Tests\Unit;
44

5+
use CodeZero\LocalizedRoutes\Tests\Stubs\Controller;
56
use CodeZero\LocalizedRoutes\Tests\Stubs\Model;
67
use CodeZero\LocalizedRoutes\Tests\TestCase;
78
use CodeZero\LocalizedRoutes\UrlGenerator;
@@ -228,6 +229,46 @@ public function it_generates_a_signed_route_url_for_a_specific_locale()
228229
$this->get($tamperedUrl)->assertSee('Invalid Signature');
229230
}
230231

232+
/** @test */
233+
public function it_allows_routes_to_be_cached()
234+
{
235+
$this->withoutExceptionHandling();
236+
$this->setSupportedLocales(['en']);
237+
$this->setAppLocale('en');
238+
239+
Route::get('en/route', Controller::class.'@index');
240+
241+
$this->cacheRoutes();
242+
243+
$this->get('en/route')->assertSuccessful();
244+
}
245+
246+
/**
247+
* Cache registered routes.
248+
*
249+
* @return void
250+
*/
251+
protected function cacheRoutes()
252+
{
253+
$routes = Route::getRoutes();
254+
255+
foreach ($routes as $route) {
256+
$route->prepareForSerialization();
257+
}
258+
259+
$isLaravel7orGreater = method_exists($routes, 'compile');
260+
261+
if ($isLaravel7orGreater) {
262+
$this->app['router']->setCompiledRoutes(
263+
$routes->compile()
264+
);
265+
266+
return;
267+
}
268+
269+
$this->app['router']->setRoutes($routes);
270+
}
271+
231272
/**
232273
* Register a route.
233274
*

0 commit comments

Comments
 (0)