Skip to content

Commit d7cfdc5

Browse files
author
riccardodallavia
committed
wip
1 parent 4d046e5 commit d7cfdc5

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/Support/Config.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44

55
class Config
66
{
7-
protected static \Closure $callback;
7+
/**
8+
* @var callable|null
9+
*/
10+
protected static $callback;
811

9-
public static function resolvePrunableModelsUsing(\Closure $callback)
12+
public static function resolvePrunableModelsUsing(?callable $callback): void
1013
{
1114
static::$callback = $callback;
1215
}
1316

1417
public static function getPrunableModels(): array
1518
{
16-
if(isset(static::$callback)) {
19+
if (is_callable(static::$callback)) {
1720
return call_user_func(static::$callback);
1821
}
22+
1923
return config('prunable-fields.models', []);
2024
}
2125
}

tests/MassPrunableFieldsTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,3 @@
5656
fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === MassPrunableUser::class
5757
);
5858
})->with('user_with_mass_prunable_fields');
59-
60-
test('should allow the prunable models to be overridden at runtime', function(MassPrunableUser $model) {
61-
\Maize\PrunableFields\Support\Config::resolvePrunableModelsUsing(fn() => [MassPrunableUser::class]);
62-
63-
Event::fake();
64-
65-
pruneFields([]);
66-
67-
Event::assertDispatched(
68-
ModelsFieldsPruned::class,
69-
fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === MassPrunableUser::class
70-
);
71-
72-
})->with('user_with_mass_prunable_fields');

tests/PrunableFieldsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Support\Facades\Event;
44
use Illuminate\Support\Facades\Log;
55
use Maize\PrunableFields\Events\ModelsFieldsPruned;
6+
use Maize\PrunableFields\Support\Config;
67
use Maize\PrunableFields\Tests\Events\UserUpdatedEvent;
78
use Maize\PrunableFields\Tests\Models\PrunableUser;
89

@@ -67,3 +68,18 @@
6768
fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === PrunableUser::class
6869
);
6970
})->with('user_with_prunable_fields');
71+
72+
test('should allow the prunable models to be overridden at runtime', function (PrunableUser $model) {
73+
Config::resolvePrunableModelsUsing(fn () => [PrunableUser::class]);
74+
75+
Event::fake();
76+
77+
pruneFields([]);
78+
79+
Event::assertDispatched(
80+
ModelsFieldsPruned::class,
81+
fn (ModelsFieldsPruned $e) => $e->count === 1 && $e->model === PrunableUser::class
82+
);
83+
84+
Config::resolvePrunableModelsUsing(null);
85+
})->with('user_with_prunable_fields');

0 commit comments

Comments
 (0)