Skip to content

Commit 6cecff5

Browse files
Added a test case for a model with a UUID (or other) string key
1 parent e3846cd commit 6cecff5

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

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: 19 additions & 0 deletions
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;
@@ -1100,6 +1101,24 @@ public function itTransitionsToAnotherModelState(
11001101
$this->assertSame($newExpectation, $models[1]->getDirty());
11011102
}
11021103

1104+
/**
1105+
* @test
1106+
*/
1107+
public function itWorksWithStringKeyModels()
1108+
{
1109+
$model = factory(ApiModel::class)->create();
1110+
$model->save();
1111+
$model->refresh();
1112+
1113+
$this->assertCount(1, $model->audits);
1114+
1115+
$model->content = 'Something else';
1116+
$model->save();
1117+
$model->refresh();
1118+
1119+
$this->assertCount(2, $model->audits);
1120+
}
1121+
11031122
/**
11041123
* @return array
11051124
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use OwenIt\Auditing\Tests\Models\ApiModel;
5+
6+
/*
7+
|--------------------------------------------------------------------------
8+
| Article Factories
9+
|--------------------------------------------------------------------------
10+
|
11+
*/
12+
13+
$factory->define(ApiModel::class, function (Faker $faker) {
14+
return [
15+
'api_model_id' => '8a7c2336-705a-41ad-9231-9199b4a64269',
16+
'content' => $faker->unique()->paragraph(6),
17+
'published_at' => null,
18+
];
19+
});
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)