Skip to content

Commit a92fde9

Browse files
committed
fix the new enum casting
1 parent e9e96a0 commit a92fde9

12 files changed

+135
-39
lines changed

src/ActivityLogger.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,13 @@ public function setEvent(string $event): static
103103

104104
public function withProperties(mixed $properties): static
105105
{
106-
$this->getActivity()->properties = collect($properties)->map(fn ($value) => ResolveForPropertyValueAction::execute($value));
106+
$this->getActivity()->properties = collect($properties);
107107

108108
return $this;
109109
}
110110

111111
public function withProperty(string $key, mixed $value): static
112112
{
113-
$value = ResolveForPropertyValueAction::execute($value);
114-
115113
$this->getActivity()->properties = $this->getActivity()->properties->put($key, $value);
116114

117115
return $this;

src/Traits/LogsActivity.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,14 @@ public static function logChanges(Model $model): array
367367
$cast = $model->getCasts()[$attribute];
368368

369369
if (function_exists('enum_exists') && enum_exists($cast)) {
370-
$changes[$attribute] = $model->getStorableEnumValue($changes[$attribute]);
370+
if (method_exists($model, 'getStorableEnumValue')) {
371+
$changes[$attribute] = $model->getStorableEnumValue($changes[$attribute]);
372+
} else {
373+
// ToDo: DEPRECATED - only here for Laravel 8 support
374+
$changes[$attribute] = $changes[$attribute] instanceof \BackedEnum
375+
? $changes[$attribute]->value
376+
: $changes[$attribute]->name;
377+
}
371378
}
372379

373380
if ($model->isCustomDateTimeCast($cast) || $model->isImmutableCustomDateTimeCast($cast)) {

tests/AbleStoreNonBackedEnumTest.php

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

33
namespace Spatie\Activitylog\Test;
44

5-
use Spatie\Activitylog\Test\Enum\NonBackedEnum;
5+
use Spatie\Activitylog\Test\Enums\NonBackedEnum;
66
use Spatie\Activitylog\Test\Models\Activity;
77
use Spatie\Activitylog\Test\Models\User;
88

@@ -20,7 +20,7 @@
2020
expect($latestActivity->description)->toEqual($description)
2121
->and($latestActivity->properties['role'])->toEqual('User');
2222
})
23-
->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum");
23+
->skip();
2424

2525
it('can store non-backed enum with properties', function () {
2626
$description = 'ROLE LOG';
@@ -34,4 +34,4 @@
3434
expect($latestActivity->description)->toEqual($description)
3535
->and($latestActivity->properties['role'])->toEqual('User');
3636
})
37-
->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum");
37+
->skip();

tests/ActivityLoggerTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,12 @@ public function tapActivity(Activity $activity, string $eventName)
431431
expect($this->getLastActivity())->toBeNull();
432432
});
433433

434-
// Helpers
435-
function it_returns_an_instance_of_the_activity_after_logging()
436-
{
437-
$activityModel = activity()->log('test');
434+
it('logs backed enums in properties', function () {
435+
activity()
436+
->withProperties(['int_backed_enum' => \Spatie\Activitylog\Test\Enums\IntBackedEnum::Draft])
437+
->withProperty('string_backed_enum', \Spatie\Activitylog\Test\Enums\StringBackedEnum::Published)
438+
->log($this->activityDescription);
438439

439-
expect($activityModel)->toBeInstanceOf(Activity::class);
440-
}
440+
$this->assertSame(0, $this->getLastActivity()->properties['int_backed_enum']);
441+
$this->assertSame('published', $this->getLastActivity()->properties['string_backed_enum']);
442+
})->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum");

tests/DetectsChangesTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@ public function getActivitylogOptions(): LogOptions
10731073
'json' => null,
10741074
'price' => null,
10751075
'interval' => null,
1076+
'status' => null,
10761077
'created_at' => '2017-01-01T12:00:00.000000Z',
10771078
'updated_at' => '2017-01-01T12:00:00.000000Z',
10781079
],
@@ -1117,6 +1118,7 @@ public function getActivitylogOptions(): LogOptions
11171118
'updated_at' => '2017-01-01T12:00:00.000000Z',
11181119
'user.name' => 'user name',
11191120
'interval' => null,
1121+
'status' => null,
11201122
],
11211123
];
11221124

@@ -1237,6 +1239,7 @@ public function getActivitylogOptions(): LogOptions
12371239
'json' => null,
12381240
'price' => null,
12391241
'interval' => null,
1242+
'status' => null,
12401243
'created_at' => '2017-01-01T12:00:00.000000Z',
12411244
],
12421245
];
@@ -1269,6 +1272,7 @@ public function getActivitylogOptions(): LogOptions
12691272
'user_id' => null,
12701273
'price' => null,
12711274
'interval' => null,
1275+
'status' => null,
12721276
],
12731277
];
12741278

tests/Enum/NonBackedEnum.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/Enums/IntBackedEnum.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Spatie\Activitylog\Test\Enums;
4+
5+
enum IntBackedEnum: int
6+
{
7+
case Published = 1;
8+
case Draft = 0;
9+
}

tests/Enums/NonBackedEnum.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Spatie\Activitylog\Test\Enums;
4+
5+
enum NonBackedEnum
6+
{
7+
case Published;
8+
case Draft;
9+
}

tests/Enums/StringBackedEnum.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Spatie\Activitylog\Test\Enums;
4+
5+
enum StringBackedEnum: string
6+
{
7+
case Published = 'published';
8+
case Draft = 'draft';
9+
}

tests/LogsActivitySerializationTest.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)