Skip to content

Commit

Permalink
fix the new enum casting
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Nov 14, 2022
1 parent e9e96a0 commit a92fde9
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 39 deletions.
4 changes: 1 addition & 3 deletions src/ActivityLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,13 @@ public function setEvent(string $event): static

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

return $this;
}

public function withProperty(string $key, mixed $value): static
{
$value = ResolveForPropertyValueAction::execute($value);

$this->getActivity()->properties = $this->getActivity()->properties->put($key, $value);

return $this;
Expand Down
9 changes: 8 additions & 1 deletion src/Traits/LogsActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,14 @@ public static function logChanges(Model $model): array
$cast = $model->getCasts()[$attribute];

if (function_exists('enum_exists') && enum_exists($cast)) {
$changes[$attribute] = $model->getStorableEnumValue($changes[$attribute]);
if (method_exists($model, 'getStorableEnumValue')) {
$changes[$attribute] = $model->getStorableEnumValue($changes[$attribute]);
} else {
// ToDo: DEPRECATED - only here for Laravel 8 support
$changes[$attribute] = $changes[$attribute] instanceof \BackedEnum
? $changes[$attribute]->value
: $changes[$attribute]->name;
}
}

if ($model->isCustomDateTimeCast($cast) || $model->isImmutableCustomDateTimeCast($cast)) {
Expand Down
6 changes: 3 additions & 3 deletions tests/AbleStoreNonBackedEnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Spatie\Activitylog\Test;

use Spatie\Activitylog\Test\Enum\NonBackedEnum;
use Spatie\Activitylog\Test\Enums\NonBackedEnum;
use Spatie\Activitylog\Test\Models\Activity;
use Spatie\Activitylog\Test\Models\User;

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

it('can store non-backed enum with properties', function () {
$description = 'ROLE LOG';
Expand All @@ -34,4 +34,4 @@
expect($latestActivity->description)->toEqual($description)
->and($latestActivity->properties['role'])->toEqual('User');
})
->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum");
->skip();
14 changes: 8 additions & 6 deletions tests/ActivityLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,12 @@ public function tapActivity(Activity $activity, string $eventName)
expect($this->getLastActivity())->toBeNull();
});

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

expect($activityModel)->toBeInstanceOf(Activity::class);
}
$this->assertSame(0, $this->getLastActivity()->properties['int_backed_enum']);
$this->assertSame('published', $this->getLastActivity()->properties['string_backed_enum']);
})->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum");
4 changes: 4 additions & 0 deletions tests/DetectsChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ public function getActivitylogOptions(): LogOptions
'json' => null,
'price' => null,
'interval' => null,
'status' => null,
'created_at' => '2017-01-01T12:00:00.000000Z',
'updated_at' => '2017-01-01T12:00:00.000000Z',
],
Expand Down Expand Up @@ -1117,6 +1118,7 @@ public function getActivitylogOptions(): LogOptions
'updated_at' => '2017-01-01T12:00:00.000000Z',
'user.name' => 'user name',
'interval' => null,
'status' => null,
],
];

Expand Down Expand Up @@ -1237,6 +1239,7 @@ public function getActivitylogOptions(): LogOptions
'json' => null,
'price' => null,
'interval' => null,
'status' => null,
'created_at' => '2017-01-01T12:00:00.000000Z',
],
];
Expand Down Expand Up @@ -1269,6 +1272,7 @@ public function getActivitylogOptions(): LogOptions
'user_id' => null,
'price' => null,
'interval' => null,
'status' => null,
],
];

Expand Down
10 changes: 0 additions & 10 deletions tests/Enum/NonBackedEnum.php

This file was deleted.

9 changes: 9 additions & 0 deletions tests/Enums/IntBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\Activitylog\Test\Enums;

enum IntBackedEnum: int
{
case Published = 1;
case Draft = 0;
}
9 changes: 9 additions & 0 deletions tests/Enums/NonBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\Activitylog\Test\Enums;

enum NonBackedEnum
{
case Published;
case Draft;
}
9 changes: 9 additions & 0 deletions tests/Enums/StringBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\Activitylog\Test\Enums;

enum StringBackedEnum: string
{
case Published = 'published';
case Draft = 'draft';
}
16 changes: 0 additions & 16 deletions tests/LogsActivitySerializationTest.php

This file was deleted.

83 changes: 83 additions & 0 deletions tests/LogsActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use Illuminate\Support\Collection;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Test\Enums\NonBackedEnum;
use Spatie\Activitylog\Test\Models\Article;
use Spatie\Activitylog\Test\Models\ArticleWithLogDescriptionClosure;
use Spatie\Activitylog\Test\Models\Issue733;
use Spatie\Activitylog\Test\Models\User;
use Spatie\Activitylog\Traits\LogsActivity;
Expand Down Expand Up @@ -494,3 +496,84 @@ public function getActivitylogOptions(): LogOptions
$this->assertEquals('created', $this->getLastActivity()->description);
$this->assertEquals('created', $this->getLastActivity()->event);
});

it('can be serialized', function() {
$model = ArticleWithLogDescriptionClosure::create(['name' => 'foo']);

$this->assertNotNull(serialize($model));
});

it('logs non backed enum casted attribute', function () {
$articleClass = new class() extends Article {
use LogsActivity;

protected $casts = [
'status' => NonBackedEnum::class,
];

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(['status']);
}
};

$article = new $articleClass();
$article->status = NonBackedEnum::Draft;
$article->save();

$this->assertInstanceOf(get_class($articleClass), $this->getLastActivity()->subject);
$this->assertEquals($article->id, $this->getLastActivity()->subject->id);
$this->assertSame('Draft', $this->getLastActivity()->properties['attributes']['status']);
$this->assertEquals('created', $this->getLastActivity()->description);
$this->assertEquals('created', $this->getLastActivity()->event);
})->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum");

it('logs int backed enum casted attribute', function () {
$articleClass = new class() extends Article {
use LogsActivity;

protected $casts = [
'status' => \Spatie\Activitylog\Test\Enums\IntBackedEnum::class,
];

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(['status']);
}
};

$article = new $articleClass();
$article->status = \Spatie\Activitylog\Test\Enums\IntBackedEnum::Published;
$article->save();

$this->assertInstanceOf(get_class($articleClass), $this->getLastActivity()->subject);
$this->assertEquals($article->id, $this->getLastActivity()->subject->id);
$this->assertSame(1, $this->getLastActivity()->properties['attributes']['status']);
$this->assertEquals('created', $this->getLastActivity()->description);
$this->assertEquals('created', $this->getLastActivity()->event);
})->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum");

it('logs string backed enum casted attribute', function () {
$articleClass = new class() extends Article {
use LogsActivity;

protected $casts = [
'status' => \Spatie\Activitylog\Test\Enums\StringBackedEnum::class,
];

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->logOnly(['status']);
}
};

$article = new $articleClass();
$article->status = \Spatie\Activitylog\Test\Enums\StringBackedEnum::Draft;
$article->save();

$this->assertInstanceOf(get_class($articleClass), $this->getLastActivity()->subject);
$this->assertEquals($article->id, $this->getLastActivity()->subject->id);
$this->assertSame('draft', $this->getLastActivity()->properties['attributes']['status']);
$this->assertEquals('created', $this->getLastActivity()->description);
$this->assertEquals('created', $this->getLastActivity()->event);
})->skip(version_compare(PHP_VERSION, '8.1', '<'), "PHP < 8.1 doesn't support enum");
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected function createTables(...$tableNames)
$table->text('json')->nullable();
$table->string('interval')->nullable();
$table->decimal('price')->nullable();
$table->string('status')->nullable();
}
});
});
Expand Down

0 comments on commit a92fde9

Please sign in to comment.