Skip to content

Commit 0b56bb0

Browse files
authored
Merge pull request #3 from DirectoryTree/bug-2
Bug 2 - Anonymization manager static set on model instance isn't flushed with application container
2 parents f6ded8c + a04b5fe commit 0b56bb0

File tree

3 files changed

+32
-40
lines changed

3 files changed

+32
-40
lines changed

src/Anonymized.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
namespace DirectoryTree\Anonymize;
44

55
use Faker\Generator;
6-
use Illuminate\Container\Container;
6+
use Illuminate\Support\Facades\App;
77

88
/**
99
* @mixin Anonymizable
1010
*/
1111
trait Anonymized
1212
{
13-
/**
14-
* The anonymize manager instance.
15-
*/
16-
protected static ?AnonymizeManager $anonymizeManager;
17-
1813
/**
1914
* Whether to enable anonymization for the current model instance.
2015
*/
@@ -31,21 +26,11 @@ trait Anonymized
3126
protected string $anonymizedAttributeCacheSeed;
3227

3328
/**
34-
* Set the anonymize manager instance.
29+
* Get the anonymize manager instance.
3530
*/
36-
public static function setManager(AnonymizeManager $manager): void
31+
protected static function getAnonymizeManager(): AnonymizeManager
3732
{
38-
static::$anonymizeManager = $manager;
39-
}
40-
41-
/**
42-
* Boot the anonymized trait.
43-
*/
44-
protected static function bootAnonymized(): void
45-
{
46-
if (! isset(static::$anonymizeManager)) {
47-
static::setManager(Container::getInstance()->make(AnonymizeManager::class));
48-
}
33+
return App::make(AnonymizeManager::class);
4934
}
5035

5136
/**
@@ -62,7 +47,7 @@ public function attributesToArray(): array
6247
{
6348
$attributes = parent::attributesToArray();
6449

65-
if ($this->anonymizeEnabled && static::$anonymizeManager?->isEnabled()) {
50+
if ($this->anonymizeEnabled && static::getAnonymizeManager()->isEnabled()) {
6651
$attributes = $this->addAnonymizedAttributesToArray($attributes);
6752
}
6853

@@ -76,7 +61,7 @@ public function attributesToArray(): array
7661
*/
7762
public function getAttributeValue($key): mixed
7863
{
79-
if (! $this->anonymizeEnabled || ! static::$anonymizeManager?->isEnabled()) {
64+
if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) {
8065
return parent::getAttributeValue($key);
8166
}
8267

@@ -120,7 +105,7 @@ protected function getCachedAnonymizedAttributes(): array
120105

121106
if (! isset($this->anonymizedAttributeCache) || $this->anonymizedAttributeCacheSeed !== $seed) {
122107
$this->anonymizedAttributeCache = $this->getAnonymizedAttributes(
123-
static::$anonymizeManager->faker($seed)
108+
static::getAnonymizeManager()->faker($seed)
124109
);
125110

126111
$this->anonymizedAttributeCacheSeed = $seed;

tests/AnonymizedModel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function getAnonymizedAttributes(Generator $faker): array
1414
{
1515
return [
1616
'name' => $faker->name(),
17+
'email' => $faker->email(),
1718
'address' => $faker->address(),
1819
];
1920
}

tests/Unit/AnonymizedTest.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
22

3-
use DirectoryTree\Anonymize\AnonymizeManager;
3+
use DirectoryTree\Anonymize\Facades\Anonymize;
44
use DirectoryTree\Anonymize\Tests\AnonymizedModel;
5-
use Faker\Factory;
65

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

109
$model = new AnonymizedModel([
1110
'name' => 'Foo Bar',
@@ -15,7 +14,7 @@
1514
});
1615

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

2019
$model = new AnonymizedModel([
2120
'id' => 1,
@@ -32,7 +31,7 @@
3231
});
3332

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

3736
$model1Attributes = (new AnonymizedModel([
3837
'id' => 1,
@@ -48,7 +47,7 @@
4847
});
4948

5049
it('generates the same attributes for models with the same id', function () {
51-
setManager()->enable();
50+
Anonymize::enable();
5251

5352
$model1Attributes = (new AnonymizedModel([
5453
'id' => 1,
@@ -64,7 +63,7 @@
6463
});
6564

6665
it('overwrites only anonymized attributes', function () {
67-
setManager()->enable();
66+
Anonymize::enable();
6867

6968
$attributes = (new AnonymizedModel([
7069
'favourite_color' => 'blue',
@@ -74,7 +73,7 @@
7473
});
7574

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

7978
$attributes = (new AnonymizedModel([
8079
'name' => 'Foo Bar',
@@ -84,7 +83,7 @@
8483
});
8584

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

8988
$attributes = (new AnonymizedModel([
9089
'name' => 'original-title',
@@ -96,7 +95,7 @@
9695
});
9796

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

101100
$model = new AnonymizedModel([
102101
'name' => 'original-name',
@@ -108,7 +107,7 @@
108107
});
109108

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

113112
$original = [
114113
'name' => 'Foo Bar',
@@ -121,7 +120,7 @@
121120
});
122121

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

126125
$model = new AnonymizedModel([
127126
'name' => 'Foo Bar',
@@ -133,7 +132,7 @@
133132
});
134133

135134
it('disables anonymization within withoutAnonymization block', function () {
136-
setManager()->enable();
135+
Anonymize::enable();
137136

138137
$original = [
139138
'name' => 'Foo Bar',
@@ -154,9 +153,16 @@
154153
expect($seed)->toContain($id);
155154
});
156155

157-
function setManager(): AnonymizeManager
158-
{
159-
AnonymizedModel::setManager($manager = new AnonymizeManager(Factory::create()));
156+
it('flushes anonymization manager enablement', function (string $attribute, string $value) {
157+
$model = new AnonymizedModel([$attribute => $value]);
160158

161-
return $manager;
162-
}
159+
expect($model->getAttributeValue($attribute))->toBe($value);
160+
161+
Anonymize::enable();
162+
163+
expect($model->getAttributeValue($attribute))->not->toBe($value);
164+
})->with([
165+
['name', 'Foo Bar'],
166+
['email', '[email protected]'],
167+
['address', '1600 Pennsylvania Avenue'],
168+
]);

0 commit comments

Comments
 (0)