I am writing some tests for my models in laravel and I am having some problems when I enable Activity Log with spatie / laravel-activitylog .
So, I create a user using Factory
, I authenticate with the system, and when I try to log out, I get this error:
1) Tests \ Function \ Usuario \ CriarUsuarioTest :: testAcessaPaginaDeRegistro Illuminate \ Database \ Eloquent \ JsonEncodingException: Unable to encode [property] attribute for model [Spatie \ Activitylog \ Models \ Activity] for JSON: Type not supported.
My test TestCase.php
:
protected function setUp()
{
parent::setUp();
$this->user = create('App\Models\Usuario');
$this->singIn($this->user)
->disableExceptionHandling();
}
...
...
...
protected function singIn($user)
{
$this->actingAs($user);
return $this;
}
protected function singOut()
{
$this->post(routeLogout());
return $this;
}
My model App/Models/Usuario.php
:
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Webpatser\Uuid\Uuid;
use Spatie\Activitylog\Traits\LogsActivity;
class Usuario extends Authenticatable
{
use LogsActivity, Notifiable, SoftDeletes;
protected $table = 'usuario';
protected $fillable = [] ;
protected static $logFillable = true;
public $timestamps = true;
protected $dates = [
'created_at',
'updated_at',
'deleted_at'
];
protected static function boot()
{
parent::boot();
static::saving(function ($usuario){
$usuario->uuid = Uuid::generate()->string;
});
}
public function getRouteKeyName()
{
return 'uuid';
}
}
My config/activitylog.php
file:
return [
'enabled' => env('ACTIVITY_LOGGER_ENABLED', true),
'delete_records_older_than_days' => 365,
'default_log_name' => 'default',
'default_auth_driver' => null,
'subject_returns_soft_deleted_models' => false,
'activity_model' => \Spatie\Activitylog\Models\Activity::class,
];
My phpunit.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="API_DEBUG" value="true"/>
<env name="memory_limit" value="512M"/>
<env name="APP_DATABASE" value="test"/>
</php>
</phpunit>
My create_activity_log_migration
file:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateActivityLogTable extends Migration
{
public function up()
{
Schema::create('activity_log', function (Blueprint $table) {
$table->increments('id');
$table->string('log_name')->nullable();
$table->string('description');
$table->integer('subject_id')->nullable();
$table->string('subject_type')->nullable();
$table->integer('causer_id')->nullable();
$table->string('causer_type')->nullable();
$table->text('properties')->nullable();
$table->timestamps();
$table->index('log_name');
});
}
public function down()
{
Schema::drop('activity_log');
}
}
, Activity Log , . , , .