Skip to content

Commit 38de27e

Browse files
Merge pull request owen-it#550 from specialtactics/bugfix/fix-for-non-integer-key-models
Bugfix/fix for non integer key models
2 parents 71af2b7 + d33caca commit 38de27e

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"require-dev": {
4949
"phpunit/phpunit": "^8.0",
5050
"mockery/mockery": "^1.0",
51-
"orchestra/testbench": "^4.0"
51+
"orchestra/testbench": "^3.8",
52+
"ramsey/uuid": "^3.0"
5253
},
5354
"autoload": {
5455
"psr-4": {

src/Models/Audit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ class Audit extends Model implements \OwenIt\Auditing\Contracts\Audit
1919
protected $casts = [
2020
'old_values' => 'json',
2121
'new_values' => 'json',
22-
'auditable_id' => 'integer',
22+
// Note: Please do not add 'auditable_id' in here, as it will break non-integer PK models
2323
];
2424
}

tests/Models/ApiModel.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace OwenIt\Auditing\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
use OwenIt\Auditing\Contracts\Auditable;
8+
9+
class ApiModel extends Model implements Auditable
10+
{
11+
use \OwenIt\Auditing\Auditable;
12+
use SoftDeletes;
13+
14+
/**
15+
* @var string UUID key
16+
*/
17+
public $primaryKey = 'api_model_id';
18+
19+
/**
20+
* @var bool Set to false for UUID keys
21+
*/
22+
public $incrementing = false;
23+
24+
/**
25+
* @var string Set to string for UUID keys
26+
*/
27+
protected $keyType = 'string';
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected $dates = [
33+
'published_at',
34+
];
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
protected $fillable = [
40+
'api_model_id',
41+
'content',
42+
'published_at',
43+
];
44+
}

tests/Unit/AuditableTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OwenIt\Auditing\Models\Audit;
1414
use OwenIt\Auditing\Redactors\LeftRedactor;
1515
use OwenIt\Auditing\Redactors\RightRedactor;
16+
use OwenIt\Auditing\Tests\Models\ApiModel;
1617
use OwenIt\Auditing\Tests\Models\Article;
1718
use OwenIt\Auditing\Tests\Models\User;
1819
use ReflectionClass;
@@ -995,9 +996,16 @@ public function itTransitionsWhenTheAuditAuditableIdTypeDoesNotMatchTheModelIdTy
995996
{
996997
$model = factory(Article::class)->create();
997998

999+
// Key depends on type
1000+
if ($model->getKeyType() == 'string') {
1001+
$key = (string) $model->id;
1002+
} else {
1003+
$key = (int) $model->id;
1004+
}
1005+
9981006
$audit = factory(Audit::class)->create([
9991007
'auditable_type' => Article::class,
1000-
'auditable_id' => (string) $model->id,
1008+
'auditable_id' => $key,
10011009
]);
10021010

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

1121+
/**
1122+
* @test
1123+
*/
1124+
public function itWorksWithStringKeyModels()
1125+
{
1126+
$model = factory(ApiModel::class)->create();
1127+
$model->save();
1128+
$model->refresh();
1129+
1130+
$this->assertCount(1, $model->audits);
1131+
1132+
$model->content = 'Something else';
1133+
$model->save();
1134+
$model->refresh();
1135+
1136+
$this->assertCount(2, $model->audits);
1137+
}
1138+
11131139
/**
11141140
* @return array
11151141
*/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use OwenIt\Auditing\Tests\Models\ApiModel;
5+
use Ramsey\Uuid\Uuid;
6+
7+
/*
8+
|--------------------------------------------------------------------------
9+
| APIModel Factories
10+
|--------------------------------------------------------------------------
11+
|
12+
*/
13+
14+
$factory->define(ApiModel::class, function (Faker $faker) {
15+
return [
16+
'api_model_id' => Uuid::uuid4(),
17+
'content' => $faker->unique()->paragraph(6),
18+
'published_at' => null,
19+
];
20+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateApiModelsTestTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('api_models', function (Blueprint $table) {
17+
$table->uuid('api_model_id');
18+
$table->text('content');
19+
$table->timestamp('published_at')->nullable();
20+
$table->timestamps();
21+
$table->softDeletes();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::drop('api_models');
33+
}
34+
}

0 commit comments

Comments
 (0)