From 13f9d5ff17a41ee3686da414e874b933d10fab5d Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Fri, 9 Feb 2024 13:37:36 +0100 Subject: [PATCH] Allow disabling data cache + always disable it in unit tests --- docs/advanced-usage/performance.md | 12 ++++++++ src/LaravelDataServiceProvider.php | 2 +- .../DataStructuresCacheCommandTest.php | 10 +++++++ .../Support/Caching/CachedDataConfigTest.php | 29 ++++++++++++++++++- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/docs/advanced-usage/performance.md b/docs/advanced-usage/performance.md index f113e93a5..18f65b72b 100644 --- a/docs/advanced-usage/performance.md +++ b/docs/advanced-usage/performance.md @@ -62,4 +62,16 @@ When using reflection discovery, the base directory and root namespace can be co ], ``` +The caching mechanism can be disabled by setting the `enabled` option to `false`: + +```php +'structure_caching' => [ + 'enabled' => false, +], +``` + You can read more about reflection discovery [here](https://github.com/spatie/php-structure-discoverer#parsers). + +## Testing + +When running tests, the cache is automatically disabled. This ensures that the analysis results are always up-to-date during development and testing. And that the cache won't interfere with your caching mocks. diff --git a/src/LaravelDataServiceProvider.php b/src/LaravelDataServiceProvider.php index 6e4fb16d6..aa76cb584 100644 --- a/src/LaravelDataServiceProvider.php +++ b/src/LaravelDataServiceProvider.php @@ -32,7 +32,7 @@ public function packageRegistered(): void $this->app->singleton( DataConfig::class, function () { - if (! config('data.structure_caching.enabled')) { + if (config('data.structure_caching.enabled') || $this->app->runningUnitTests()) { return DataConfig::createFromConfig(config('data')); } diff --git a/tests/Commands/DataStructuresCacheCommandTest.php b/tests/Commands/DataStructuresCacheCommandTest.php index 55f878185..a578dffc2 100644 --- a/tests/Commands/DataStructuresCacheCommandTest.php +++ b/tests/Commands/DataStructuresCacheCommandTest.php @@ -2,10 +2,20 @@ use Illuminate\Support\Facades\App; use Spatie\LaravelData\Support\Caching\CachedDataConfig; +use Spatie\LaravelData\Support\Caching\DataStructureCache; use Spatie\LaravelData\Support\DataConfig; use Spatie\LaravelData\Tests\Fakes\SimpleData; it('can cache data structures', function () { + // Ensure we cache + App::forgetInstance(DataConfig::class); + app()->singleton( + DataConfig::class, + function () { + return app()->make(DataStructureCache::class)->getConfig() ?? DataConfig::createFromConfig(config('data')); + } + ); + config()->set('data.structure_caching.directories', [ __DIR__.'/../Fakes', ]); diff --git a/tests/Support/Caching/CachedDataConfigTest.php b/tests/Support/Caching/CachedDataConfigTest.php index 0d2e4c94f..cb8d4b069 100644 --- a/tests/Support/Caching/CachedDataConfigTest.php +++ b/tests/Support/Caching/CachedDataConfigTest.php @@ -10,7 +10,20 @@ use Spatie\LaravelData\Tests\Factories\FakeDataStructureFactory; use Spatie\LaravelData\Tests\Fakes\SimpleData; +function ensureDataWillBeCached() +{ + App::forgetInstance(DataConfig::class); + app()->singleton( + DataConfig::class, + function () { + return app()->make(DataStructureCache::class)->getConfig() ?? DataConfig::createFromConfig(config('data')); + } + ); +} + it('will use a cached data config if available', function () { + ensureDataWillBeCached(); + $cache = app(DataStructureCache::class); $cachedDataConfig = new CachedDataConfig(); @@ -23,12 +36,16 @@ }); it('will use a non cached config when a cached version is not available', function () { + ensureDataWillBeCached(); + $config = app(DataConfig::class); expect($config)->toBeInstanceOf(DataConfig::class); }); it('will use a cached data config if the cached version is invalid', function () { + ensureDataWillBeCached(); + ['store' => $store, 'prefix' => $prefix] = config('data.structure_caching.cache'); cache()->store($store)->forever("{$prefix}.config", serialize(new CachedDataConfig())); @@ -43,6 +60,8 @@ }); it('will load cached data classes', function () { + ensureDataWillBeCached(); + $dataClass = FakeDataStructureFactory::class(SimpleData::class); $dataClass->prepareForCache(); @@ -73,7 +92,15 @@ function (MockInterface $spy) use ($dataClass) { Cache::expects('get')->once(); - $data = SimpleData::from('Hello world'); + SimpleData::from('Hello world'); + + cache()->get('something-just-to-test-the-mock'); +}); + +it('will not cache when unit testing', function (){ + Cache::expects('get')->once(); + + SimpleData::from('Hello world'); cache()->get('something-just-to-test-the-mock'); });