2017-12-13 7 views
4

私はlaravelのモデルにいくつかのテストを書いていますが、​​を使用してアクティビティログを有効にするといくつか問題が発生します。TDD Laravel - laravelとspatie/laravel-activitylogの機能テストでJSONのエンコーディングエラーが発生する

だから、私はFactoryを使用して、ユーザーを作成し、私はシステムに認証し、私はログアウトしてみますと、私はこのエラーを取得:

1) Tests\Feature\Usuario\CriarUsuarioTest::testAcessaPaginaDeRegistro Illuminate\Database\Eloquent\JsonEncodingException: Unable to encode attribute [properties] for model [Spatie\Activitylog\Models\Activity] to JSON: Type is not supported.

私のテスト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() 
{ 
    // routeLogout() goes to '/logout' route. 
    $this->post(routeLogout()); // <- Here, where the error occurs 
    return $this; 
} 

マイ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 = [/*...*/] ; // I remove this to post here on SO 
    protected static $logFillable = true; 
    public $timestamps = true; 
    protected $dates = [ 
     'created_at', 
     'updated_at', 
     'deleted_at' 
    ]; 
    protected static function boot() 
    { 
     // Handle the \LogsActivity boot method 
     parent::boot(); 
     static::saving(function ($usuario){ 
      $usuario->uuid = Uuid::generate()->string; 
     }); 
    } 
    public function getRouteKeyName() 
    { 
     return 'uuid'; 
    } 
} 

マイconfig/activitylog.php FIL E:

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, 
]; 

マイphpunit.xmlファイル:

<?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> 

マイcreate_activity_log_migrationファイル:私はモデルのアクティビティログを無効にするとき、私はそれから気づく

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateActivityLogTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    */ 
    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'); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    */ 
    public function down() 
    { 
     Schema::drop('activity_log'); 
    } 
} 

、その作業罰金。そして、私はtinkerやブラウザでシステムを使用すると、ログも同様に動作します。

+0

'routeLogout()'の定義を表示できますか? – Camilo

+0

申し訳ありません、忘れました。 'routeLogout()'は単に '/ logout'パスの助けになります。 – Abe

+0

'/ logout'ルートのリクエストを受け取る方法を表示できますか? – Camilo

答えて

1

私は、エラーを再現することができていないが、私はいくつかの考慮事項を持っている:

  • あなたはログアウトルートに投稿するときにエラーがトリガされたと言うが、それは活動を誘発するべきではありませんロガー、そう?つまり、createdupdated、またはdeletedというイベントは発生しません。
  • 代わりに、工場でユーザーを作成するとcreatedイベントが実際にトリガーされます。これにより、アクティビティログがトリガされます。
  • ロガーが新しいActivityを作成しようとすると、例外Illuminate\Database\Eloquent\JsonEncodingException: Unable to encode attribute [properties] for model [Spatie\Activitylog\Models\Activity] to JSON: Type is not supported.が発生します。これはほぼ確実にIlluminate\Database\Eloquent\Concerns\[email protected]メソッドにスローされます。これは属性値の単純なjson_encodeを実行します。
  • JSON_ERROR_UNSUPPORTED_TYPE - A value of an unsupported type was given to json_encode(), such as a resource.

  • The value being encoded. Can be any type except a resource.

  • ので、リソースがログに記録されるpropertiesの一部である可能性がありますか?ユーザーの作成直後にdd($user->attributeValuesToBeLogged('created'))を試してください。
0

私は答えを見つけました(しかし、これを解決する最良の方法はわかりません)。

$this->createApplication();SetUpの方法に入れるだけで、TestCase.phpファイル内にエラーが表示されなくなります。

皆さん、ありがとうございます。

関連する問題