Skip to content

Commit

Permalink
Allow disabling data cache + always disable it in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Feb 9, 2024
1 parent ea7ad7e commit 13f9d5f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/advanced-usage/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion src/LaravelDataServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

Check failure on line 35 in src/LaravelDataServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Consider using bind method instead or pass a closure.
return DataConfig::createFromConfig(config('data'));
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Commands/DataStructuresCacheCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Expand Down
29 changes: 28 additions & 1 deletion tests/Support/Caching/CachedDataConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()));
Expand All @@ -43,6 +60,8 @@
});

it('will load cached data classes', function () {
ensureDataWillBeCached();

$dataClass = FakeDataStructureFactory::class(SimpleData::class);
$dataClass->prepareForCache();

Expand Down Expand Up @@ -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');
});

0 comments on commit 13f9d5f

Please sign in to comment.