diff --git a/sources/Service/Router/Router.php b/sources/Service/Router/Router.php index 4c11f8fab..5bc9547a7 100644 --- a/sources/Service/Router/Router.php +++ b/sources/Service/Router/Router.php @@ -40,6 +40,11 @@ class Router return static::$oSingleton; } + /** + * @var bool $bUseCache + */ + protected $bUseCache = null; + /**********************/ /* Non-static methods */ /**********************/ @@ -54,6 +59,14 @@ class Router // Don't do anything, we don't want to be initialized } + /** + * @param bool|null $bUseCache Force cache usage for testing purposes, or leave it null for the default behavior + */ + public function SetUseCache(?bool $bUseCache): void + { + $this->bUseCache = $bUseCache; + } + /** * Generate a complete URL for a specific route and optional parameters * @@ -137,7 +150,7 @@ class Router public function GetRoutes(): array { $aRoutes = []; - $bUseCache = false === utils::IsDevelopmentEnvironment(); + $bUseCache = is_null($this->bUseCache) ? (false === utils::IsDevelopmentEnvironment()) : $this->bUseCache; $bMustWriteCache = false; $sCacheFilePath = $this->GetCacheFileAbsPath(); diff --git a/tests/php-unit-tests/unitary-tests/sources/Service/Router/RouterTest.php b/tests/php-unit-tests/unitary-tests/sources/Service/Router/RouterTest.php index 4fb061163..7c1a3ca29 100644 --- a/tests/php-unit-tests/unitary-tests/sources/Service/Router/RouterTest.php +++ b/tests/php-unit-tests/unitary-tests/sources/Service/Router/RouterTest.php @@ -28,6 +28,10 @@ class RouterTest extends ItopDataTestCase parent::setUp(); $this->RequireOnceItopFile('setup/setuputils.class.inc.php'); + + // Speedup test by forcing the use of the cache, even on a development environment + $oRouter = Router::GetInstance(); + $oRouter->SetUseCache(true); } /** @@ -203,12 +207,9 @@ class RouterTest extends ItopDataTestCase $this->fail("Cache file was not generated ($sRoutesCacheFilePath)"); } - clearstatcache(); - $iFirstModificationTimestamp = filemtime($sRoutesCacheFilePath); - $this->debug("Initial timestamp: $iFirstModificationTimestamp"); - - // Wait for just 1s to ensure timestamps would be different is the file is re-generated - sleep(1); + // Set its modification date in the past so that regenerating it will result in a new modification date without any doubt + $iFirstModificationTimestamp = time() - 2; + touch($sRoutesCacheFilePath, $iFirstModificationTimestamp); // Call GetRoutes() again to see if cache gets re-generated or not $this->InvokeNonPublicMethod(Router::class, 'GetRoutes', $oRouter, []);