Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions src/Anonymized.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/AnonymizedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function getAnonymizedAttributes(Generator $faker): array
{
return [
'name' => $faker->name(),
'email' => $faker->email(),
'address' => $faker->address(),
];
}
Expand Down
42 changes: 24 additions & 18 deletions tests/Unit/AnonymizedTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

use DirectoryTree\Anonymize\AnonymizeManager;
use DirectoryTree\Anonymize\Facades\Anonymize;
use DirectoryTree\Anonymize\Tests\AnonymizedModel;
use Faker\Factory;

it('does not leak data via serialize', function () {
setManager()->enable();
Anonymize::enable();

$model = new AnonymizedModel([
'name' => 'Foo Bar',
Expand All @@ -15,7 +14,7 @@
});

it('invalidates attribute cache when seed changes', function () {
setManager()->enable();
Anonymize::enable();

$model = new AnonymizedModel([
'id' => 1,
Expand All @@ -32,7 +31,7 @@
});

it('generates different attributes for models with distinct ids', function () {
setManager()->enable();
Anonymize::enable();

$model1Attributes = (new AnonymizedModel([
'id' => 1,
Expand All @@ -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,
Expand All @@ -64,7 +63,7 @@
});

it('overwrites only anonymized attributes', function () {
setManager()->enable();
Anonymize::enable();

$attributes = (new AnonymizedModel([
'favourite_color' => 'blue',
Expand All @@ -74,7 +73,7 @@
});

it('anonymizes only attributes that exist on the model', function () {
setManager()->enable();
Anonymize::enable();

$attributes = (new AnonymizedModel([
'name' => 'Foo Bar',
Expand All @@ -84,7 +83,7 @@
});

it('anonymizes attributes array when anonymization is enabled', function () {
setManager()->enable();
Anonymize::enable();

$attributes = (new AnonymizedModel([
'name' => 'original-title',
Expand All @@ -96,7 +95,7 @@
});

it('anonymizes attributes when anonymization is enabled', function () {
setManager()->enable();
Anonymize::enable();

$model = new AnonymizedModel([
'name' => 'original-name',
Expand All @@ -108,7 +107,7 @@
});

it('does not anonymize attributes array when anonymization is disabled', function () {
setManager()->disable();
Anonymize::disable();

$original = [
'name' => 'Foo Bar',
Expand All @@ -121,7 +120,7 @@
});

it('does not anonymize attributes when anonymization is disabled', function () {
setManager()->disable();
Anonymize::disable();

$model = new AnonymizedModel([
'name' => 'Foo Bar',
Expand All @@ -133,7 +132,7 @@
});

it('disables anonymization within withoutAnonymization block', function () {
setManager()->enable();
Anonymize::enable();

$original = [
'name' => 'Foo Bar',
Expand All @@ -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'],
]);