Skip to content

Commit eee6143

Browse files
authored
Merge pull request spatie#1121 from spatie/fix-enum-casting
Fix enum casting
2 parents 10e0f0e + fb36c79 commit eee6143

14 files changed

+147
-114
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"require": {
3434
"php": "^8.0",
3535
"illuminate/config": "^8.0 || ^9.0",
36-
"illuminate/database": "^8.53 || ^9.0",
36+
"illuminate/database": "^8.69 || ^9.27",
3737
"illuminate/support": "^8.0 || ^9.0",
3838
"spatie/laravel-package-tools": "^1.6.3"
3939
},

src/Actions/ResolveForPropertyValueAction.php

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

src/ActivityLogger.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Illuminate\Support\Str;
1111
use Illuminate\Support\Traits\Conditionable;
1212
use Illuminate\Support\Traits\Macroable;
13-
use Spatie\Activitylog\Actions\ResolveForPropertyValueAction;
1413
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
1514

1615
class ActivityLogger
@@ -103,15 +102,13 @@ public function setEvent(string $event): static
103102

104103
public function withProperties(mixed $properties): static
105104
{
106-
$this->getActivity()->properties = collect($properties)->map(fn ($value) => ResolveForPropertyValueAction::execute($value));
105+
$this->getActivity()->properties = collect($properties);
107106

108107
return $this;
109108
}
110109

111110
public function withProperty(string $key, mixed $value): static
112111
{
113-
$value = ResolveForPropertyValueAction::execute($value);
114-
115112
$this->getActivity()->properties = $this->getActivity()->properties->put($key, $value);
116113

117114
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: 0 additions & 37 deletions
This file was deleted.

tests/ActivityLoggerTest.php

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

33
use Carbon\Carbon;
4+
use Illuminate\Database\Eloquent\JsonEncodingException;
45
use Illuminate\Support\Collection;
56
use Spatie\Activitylog\Exceptions\CouldNotLogActivity;
67
use Spatie\Activitylog\Facades\CauserResolver;
78
use Spatie\Activitylog\LogOptions;
89
use Spatie\Activitylog\Models\Activity;
10+
use Spatie\Activitylog\Test\Enums\NonBackedEnum;
911
use Spatie\Activitylog\Test\Models\Article;
1012
use Spatie\Activitylog\Test\Models\User;
1113
use Spatie\Activitylog\Traits\LogsActivity;
@@ -431,10 +433,20 @@ public function tapActivity(Activity $activity, string $eventName)
431433
expect($this->getLastActivity())->toBeNull();
432434
});
433435

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

439-
expect($activityModel)->toBeInstanceOf(Activity::class);
440-
}
442+
$this->assertSame(0, $this->getLastActivity()->properties['int_backed_enum']);
443+
$this->assertSame('published', $this->getLastActivity()->properties['string_backed_enum']);
444+
})->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum");
445+
446+
it('does not log non backed enums in properties', function () {
447+
activity()
448+
->withProperty('non_backed_enum', NonBackedEnum::Published)
449+
->log($this->activityDescription);
450+
})
451+
->throws(JsonEncodingException::class)
452+
->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+
}

0 commit comments

Comments
 (0)