diff --git a/src/Anonymized.php b/src/Anonymized.php index e2cc00d..976e794 100644 --- a/src/Anonymized.php +++ b/src/Anonymized.php @@ -3,18 +3,13 @@ namespace DirectoryTree\Anonymize; use Faker\Generator; -use Illuminate\Container\Container; +use Illuminate\Support\Facades\App; /** * @mixin Anonymizable */ trait Anonymized { - /** - * The anonymize manager instance. - */ - protected static ?AnonymizeManager $anonymizeManager; - /** * Whether to enable anonymization for the current model instance. */ @@ -31,21 +26,11 @@ trait Anonymized protected string $anonymizedAttributeCacheSeed; /** - * Set the anonymize manager instance. + * Get the anonymize manager instance. */ - public static function setManager(AnonymizeManager $manager): void + protected static function getAnonymizeManager(): AnonymizeManager { - static::$anonymizeManager = $manager; - } - - /** - * Boot the anonymized trait. - */ - protected static function bootAnonymized(): void - { - if (! isset(static::$anonymizeManager)) { - static::setManager(Container::getInstance()->make(AnonymizeManager::class)); - } + return App::make(AnonymizeManager::class); } /** @@ -62,7 +47,7 @@ public function attributesToArray(): array { $attributes = parent::attributesToArray(); - if ($this->anonymizeEnabled && static::$anonymizeManager?->isEnabled()) { + if ($this->anonymizeEnabled && static::getAnonymizeManager()->isEnabled()) { $attributes = $this->addAnonymizedAttributesToArray($attributes); } @@ -76,7 +61,7 @@ public function attributesToArray(): array */ public function getAttributeValue($key): mixed { - if (! $this->anonymizeEnabled || ! static::$anonymizeManager?->isEnabled()) { + if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) { return parent::getAttributeValue($key); } @@ -120,7 +105,7 @@ protected function getCachedAnonymizedAttributes(): array if (! isset($this->anonymizedAttributeCache) || $this->anonymizedAttributeCacheSeed !== $seed) { $this->anonymizedAttributeCache = $this->getAnonymizedAttributes( - static::$anonymizeManager->faker($seed) + static::getAnonymizeManager()->faker($seed) ); $this->anonymizedAttributeCacheSeed = $seed; diff --git a/tests/AnonymizedModel.php b/tests/AnonymizedModel.php index 72e8890..f669996 100644 --- a/tests/AnonymizedModel.php +++ b/tests/AnonymizedModel.php @@ -14,6 +14,7 @@ public function getAnonymizedAttributes(Generator $faker): array { return [ 'name' => $faker->name(), + 'email' => $faker->email(), 'address' => $faker->address(), ]; } diff --git a/tests/Unit/AnonymizedTest.php b/tests/Unit/AnonymizedTest.php index d9e5e5c..06c7c07 100644 --- a/tests/Unit/AnonymizedTest.php +++ b/tests/Unit/AnonymizedTest.php @@ -1,11 +1,10 @@ enable(); + Anonymize::enable(); $model = new AnonymizedModel([ 'name' => 'Foo Bar', @@ -15,7 +14,7 @@ }); it('invalidates attribute cache when seed changes', function () { - setManager()->enable(); + Anonymize::enable(); $model = new AnonymizedModel([ 'id' => 1, @@ -32,7 +31,7 @@ }); it('generates different attributes for models with distinct ids', function () { - setManager()->enable(); + Anonymize::enable(); $model1Attributes = (new AnonymizedModel([ 'id' => 1, @@ -48,7 +47,7 @@ }); it('generates the same attributes for models with the same id', function () { - setManager()->enable(); + Anonymize::enable(); $model1Attributes = (new AnonymizedModel([ 'id' => 1, @@ -64,7 +63,7 @@ }); it('overwrites only anonymized attributes', function () { - setManager()->enable(); + Anonymize::enable(); $attributes = (new AnonymizedModel([ 'favourite_color' => 'blue', @@ -74,7 +73,7 @@ }); it('anonymizes only attributes that exist on the model', function () { - setManager()->enable(); + Anonymize::enable(); $attributes = (new AnonymizedModel([ 'name' => 'Foo Bar', @@ -84,7 +83,7 @@ }); it('anonymizes attributes array when anonymization is enabled', function () { - setManager()->enable(); + Anonymize::enable(); $attributes = (new AnonymizedModel([ 'name' => 'original-title', @@ -96,7 +95,7 @@ }); it('anonymizes attributes when anonymization is enabled', function () { - setManager()->enable(); + Anonymize::enable(); $model = new AnonymizedModel([ 'name' => 'original-name', @@ -108,7 +107,7 @@ }); it('does not anonymize attributes array when anonymization is disabled', function () { - setManager()->disable(); + Anonymize::disable(); $original = [ 'name' => 'Foo Bar', @@ -121,7 +120,7 @@ }); it('does not anonymize attributes when anonymization is disabled', function () { - setManager()->disable(); + Anonymize::disable(); $model = new AnonymizedModel([ 'name' => 'Foo Bar', @@ -133,7 +132,7 @@ }); it('disables anonymization within withoutAnonymization block', function () { - setManager()->enable(); + Anonymize::enable(); $original = [ 'name' => 'Foo Bar', @@ -154,9 +153,16 @@ expect($seed)->toContain($id); }); -function setManager(): AnonymizeManager -{ - AnonymizedModel::setManager($manager = new AnonymizeManager(Factory::create())); +it('flushes anonymization manager enablement', function (string $attribute, string $value) { + $model = new AnonymizedModel([$attribute => $value]); - return $manager; -} + expect($model->getAttributeValue($attribute))->toBe($value); + + Anonymize::enable(); + + expect($model->getAttributeValue($attribute))->not->toBe($value); +})->with([ + ['name', 'Foo Bar'], + ['email', 'foo.bar@example.com'], + ['address', '1600 Pennsylvania Avenue'], +]);