Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,8 @@ public function getCachedConfigPath()
*/
public function routesAreCached()
{
return $this['files']->exists($this->getCachedRoutesPath());
return ($this->bound('routes.cached') && $this->make('routes.cached') === true) ||
$this['files']->exists($this->getCachedRoutesPath());
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Foundation/Testing/CachedState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Foundation\Testing;

class CachedState
{
public static array $cachedRoutes;
}
5 changes: 5 additions & 0 deletions src/Illuminate/Foundation/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public function createApplication()
{
$app = require Application::inferBasePath().'/bootstrap/app.php';

if (isset(CachedState::$cachedRoutes) &&
in_array(WithCachedRoutes::class, class_uses_recursive(static::class))) {
$app->booting(fn () => $this->markRoutesCached($app));
}

$app->make(Kernel::class)->bootstrap();

return $app;
Expand Down
53 changes: 53 additions & 0 deletions src/Illuminate/Foundation/Testing/WithCachedRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Illuminate\Foundation\Testing;

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;

trait WithCachedRoutes
{
/**
* After creating the routes once, we can cache them for the remaining tests.
*
* @return void
*/
protected function setUpWithCachedRoutes(): void
{
if ((CachedState::$cachedRoutes ?? null) === null) {
$routes = $this->app['router']->getRoutes();

$routes->refreshNameLookups();
$routes->refreshActionLookups();

CachedState::$cachedRoutes = $routes->compile();
}

$this->markRoutesCached($this->app);
}

/**
* Reset the route service provider so it's not defaulting to loading cached routes.
*
* This is helpful if some of the tests in the suite apply this trait while others do not.
*
* @return void
*/
protected function tearDownWithCachedRoutes(): void
{
RouteServiceProvider::loadCachedRoutesUsing(null);
}

/**
* Inform the container to treat routes as cached.
*/
protected function markRoutesCached(Application $app): void
{
$app->instance('routes.cached', true);

RouteServiceProvider::loadCachedRoutesUsing(
static fn () => app('router')->setCompiledRoutes(CachedState::$cachedRoutes)
);
}

}
25 changes: 25 additions & 0 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,31 @@ public function testAbortAcceptsHeaders()
$this->assertSame(['X-FOO' => 'BAR'], $exception->getHeaders());
}
}

public function test_routes_are_cached()
{
$app = new Application();
$app->instance('routes.cached', true);
$this->assertTrue($app->routesAreCached());
}

public function test_routes_are_not_cached_by_instance_falls_back_to_file()
{
$app = new Application();
$files = new class
{
public string $pathRequested;
public function exists(string $path): bool
{
$this->pathRequested = $path;
return false;
}
};
$app->instance('files', $files);

$this->assertFalse($app->routesAreCached());
$this->assertStringContainsString('routes-v7.php', $files->pathRequested);
}
}

class ApplicationBasicServiceProviderStub extends ServiceProvider
Expand Down