Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"require-dev": {
"phpunit/phpunit": "^8.0",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^4.0"
"orchestra/testbench": "^3.8",
"ramsey/uuid": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class Audit extends Model implements \OwenIt\Auditing\Contracts\Audit
protected $casts = [
'old_values' => 'json',
'new_values' => 'json',
'auditable_id' => 'integer',
// Note: Please do not add 'auditable_id' in here, as it will break non-integer PK models
];
}
44 changes: 44 additions & 0 deletions tests/Models/ApiModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace OwenIt\Auditing\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use OwenIt\Auditing\Contracts\Auditable;

class ApiModel extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
use SoftDeletes;

/**
* @var string UUID key
*/
public $primaryKey = 'api_model_id';

/**
* @var bool Set to false for UUID keys
*/
public $incrementing = false;

/**
* @var string Set to string for UUID keys
*/
protected $keyType = 'string';

/**
* {@inheritdoc}
*/
protected $dates = [
'published_at',
];

/**
* {@inheritdoc}
*/
protected $fillable = [
'api_model_id',
'content',
'published_at',
];
}
28 changes: 27 additions & 1 deletion tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OwenIt\Auditing\Models\Audit;
use OwenIt\Auditing\Redactors\LeftRedactor;
use OwenIt\Auditing\Redactors\RightRedactor;
use OwenIt\Auditing\Tests\Models\ApiModel;
use OwenIt\Auditing\Tests\Models\Article;
use OwenIt\Auditing\Tests\Models\User;
use ReflectionClass;
Expand Down Expand Up @@ -995,9 +996,16 @@ public function itTransitionsWhenTheAuditAuditableIdTypeDoesNotMatchTheModelIdTy
{
$model = factory(Article::class)->create();

// Key depends on type
if ($model->getKeyType() == 'string') {
$key = (string) $model->id;
} else {
$key = (int) $model->id;
}

$audit = factory(Audit::class)->create([
'auditable_type' => Article::class,
'auditable_id' => (string) $model->id,
'auditable_id' => $key,
]);

$this->assertInstanceOf(Auditable::class, $model->transitionTo($audit));
Expand Down Expand Up @@ -1110,6 +1118,24 @@ public function itTransitionsToAnotherModelState(
$this->assertSame($newExpectation, $models[1]->getDirty());
}

/**
* @test
*/
public function itWorksWithStringKeyModels()
{
$model = factory(ApiModel::class)->create();
$model->save();
$model->refresh();

$this->assertCount(1, $model->audits);

$model->content = 'Something else';
$model->save();
$model->refresh();

$this->assertCount(2, $model->audits);
}

/**
* @return array
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/database/factories/ApiModelFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Faker\Generator as Faker;
use OwenIt\Auditing\Tests\Models\ApiModel;
use Ramsey\Uuid\Uuid;

/*
|--------------------------------------------------------------------------
| APIModel Factories
|--------------------------------------------------------------------------
|
*/

$factory->define(ApiModel::class, function (Faker $faker) {
return [
'api_model_id' => Uuid::uuid4(),
'content' => $faker->unique()->paragraph(6),
'published_at' => null,
];
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

class CreateApiModelsTestTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('api_models', function (Blueprint $table) {
$table->uuid('api_model_id');
$table->text('content');
$table->timestamp('published_at')->nullable();
$table->timestamps();
$table->softDeletes();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('api_models');
}
}