Skip to content

Commit 08c1f93

Browse files
[12.x] Introduce WithCachedRoutes testing trait (#57623)
* with cached routes * clean up * clean up * try to guess how Taylor would format it * compile to array * Update WithCachedRoutes.php * I think this is better * comments * lighter check first * static * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 08f12a0 commit 08c1f93

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

src/Illuminate/Foundation/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,8 @@ public function getCachedConfigPath()
13261326
*/
13271327
public function routesAreCached()
13281328
{
1329-
return $this['files']->exists($this->getCachedRoutesPath());
1329+
return ($this->bound('routes.cached') && $this->make('routes.cached') === true) ||
1330+
$this['files']->exists($this->getCachedRoutesPath());
13301331
}
13311332

13321333
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Testing;
4+
5+
class CachedState
6+
{
7+
public static array $cachedRoutes;
8+
}

src/Illuminate/Foundation/Testing/TestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function createApplication()
2929
{
3030
$app = require Application::inferBasePath().'/bootstrap/app.php';
3131

32+
if (isset(CachedState::$cachedRoutes) &&
33+
in_array(WithCachedRoutes::class, class_uses_recursive(static::class))) {
34+
$app->booting(fn () => $this->markRoutesCached($app));
35+
}
36+
3237
$app->make(Kernel::class)->bootstrap();
3338

3439
return $app;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Testing;
4+
5+
use Illuminate\Foundation\Application;
6+
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
7+
8+
trait WithCachedRoutes
9+
{
10+
/**
11+
* After creating the routes once, we can cache them for the remaining tests.
12+
*
13+
* @return void
14+
*/
15+
protected function setUpWithCachedRoutes(): void
16+
{
17+
if ((CachedState::$cachedRoutes ?? null) === null) {
18+
$routes = $this->app['router']->getRoutes();
19+
20+
$routes->refreshNameLookups();
21+
$routes->refreshActionLookups();
22+
23+
CachedState::$cachedRoutes = $routes->compile();
24+
}
25+
26+
$this->markRoutesCached($this->app);
27+
}
28+
29+
/**
30+
* Reset the route service provider so it's not defaulting to loading cached routes.
31+
*
32+
* This is helpful if some of the tests in the suite apply this trait while others do not.
33+
*
34+
* @return void
35+
*/
36+
protected function tearDownWithCachedRoutes(): void
37+
{
38+
RouteServiceProvider::loadCachedRoutesUsing(null);
39+
}
40+
41+
/**
42+
* Inform the container to treat routes as cached.
43+
*/
44+
protected function markRoutesCached(Application $app): void
45+
{
46+
$app->instance('routes.cached', true);
47+
48+
RouteServiceProvider::loadCachedRoutesUsing(
49+
static fn () => app('router')->setCompiledRoutes(CachedState::$cachedRoutes)
50+
);
51+
}
52+
53+
}

tests/Foundation/FoundationApplicationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,31 @@ public function testAbortAcceptsHeaders()
609609
$this->assertSame(['X-FOO' => 'BAR'], $exception->getHeaders());
610610
}
611611
}
612+
613+
public function test_routes_are_cached()
614+
{
615+
$app = new Application();
616+
$app->instance('routes.cached', true);
617+
$this->assertTrue($app->routesAreCached());
618+
}
619+
620+
public function test_routes_are_not_cached_by_instance_falls_back_to_file()
621+
{
622+
$app = new Application();
623+
$files = new class
624+
{
625+
public string $pathRequested;
626+
public function exists(string $path): bool
627+
{
628+
$this->pathRequested = $path;
629+
return false;
630+
}
631+
};
632+
$app->instance('files', $files);
633+
634+
$this->assertFalse($app->routesAreCached());
635+
$this->assertStringContainsString('routes-v7.php', $files->pathRequested);
636+
}
612637
}
613638

614639
class ApplicationBasicServiceProviderStub extends ServiceProvider

0 commit comments

Comments
 (0)