Skip to content

Commit 760fa22

Browse files
authored
Merge pull request #5 from yabhq/feat/uuid-models
Feat/UUID models
2 parents a8daf5a + 2d16e29 commit 760fa22

10 files changed

+263
-25
lines changed

src/Traits/Uuid.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Yab\Mint\Traits;
4+
5+
use Ramsey\Uuid\Uuid as RamseyUuid;
6+
7+
trait Uuid
8+
{
9+
/**
10+
* Hook into the boot method to catch creating and saving events
11+
*
12+
* @return void
13+
*/
14+
public static function bootUuid()
15+
{
16+
static::creating(function ($model) {
17+
$model->id = RamseyUuid::uuid4()->toString();
18+
});
19+
20+
static::saving(function ($model) {
21+
if ($model->id !== $model->getOriginal('id')) {
22+
$model->id = $model->getOriginal('id');
23+
}
24+
});
25+
}
26+
27+
/**
28+
* Get the route key for the model.
29+
*
30+
* @return string
31+
*/
32+
public function getRouteKeyName()
33+
{
34+
return 'id';
35+
}
36+
37+
/**
38+
* Get the value indicating whether the IDs are incrementing.
39+
*
40+
* @return bool
41+
*/
42+
public function getIncrementing()
43+
{
44+
return false;
45+
}
46+
}

tests/Factories/UuidModelFactory.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Yab\Mint\Tests\Models\UuidModel;
4+
5+
$factory->define(UuidModel::class, function () {
6+
return [];
7+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateArchivableModelsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('archivable_models', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->timestamp('archived_at')->nullable();
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('archivable_models');
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateCastableModelsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('castable_models', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->integer('amount');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('castable_models');
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateEventfulModelsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('eventful_models', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->timestamps();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('eventful_models');
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateImmutableModelsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('immutable_models', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('field')->nullable();
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('immutable_models');
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateUuidModelsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('uuid_models', function (Blueprint $table) {
17+
$table->string('id');
18+
$table->timestamps();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('uuid_models');
30+
}
31+
}

tests/Models/UuidModel.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Yab\Mint\Tests\Models;
4+
5+
use Yab\Mint\Traits\Uuid;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class UuidModel extends Model
9+
{
10+
use Uuid;
11+
}

tests/TestCase.php

+2-25
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace Yab\Mint\Tests;
44

5-
use Illuminate\Support\Facades\Schema;
6-
use Illuminate\Database\Schema\Blueprint;
75
use Orchestra\Testbench\TestCase as OrchestraTestCase;
86

97
abstract class TestCase extends OrchestraTestCase
@@ -17,30 +15,9 @@ public function setUp(): void
1715
{
1816
parent::setUp();
1917

20-
Schema::create('eventful_models', function (Blueprint $table) {
21-
$table->increments('id');
22-
$table->timestamps();
23-
});
24-
25-
Schema::create('archivable_models', function (Blueprint $table) {
26-
$table->increments('id');
27-
$table->timestamp('archived_at')->nullable();
28-
$table->timestamps();
29-
});
30-
31-
Schema::create('immutable_models', function (Blueprint $table) {
32-
$table->increments('id');
33-
$table->string('field')->nullable();
34-
$table->timestamps();
35-
});
36-
37-
Schema::create('castable_models', function (Blueprint $table) {
38-
$table->increments('id');
39-
$table->integer('amount');
40-
$table->timestamps();
41-
});
42-
4318
$this->withFactories(__DIR__ . '/Factories');
19+
20+
$this->loadMigrationsFrom(__DIR__ . '/Migrations');
4421
}
4522

4623
/**

tests/UuidTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Yab\Mint\Tests;
4+
5+
use Ramsey\Uuid\Uuid;
6+
use Yab\Mint\Tests\TestCase;
7+
use Yab\Mint\Tests\Models\UuidModel;
8+
9+
class UuidTest extends TestCase
10+
{
11+
/** @test */
12+
public function a_newly_created_uuid_model_has_a_uuid()
13+
{
14+
$model = factory(UuidModel::class)->create();
15+
16+
$this->assertTrue(Uuid::isValid($model->id));
17+
18+
$this->assertDatabaseHas('uuid_models', [
19+
'id' => $model->id,
20+
]);
21+
}
22+
23+
/** @test */
24+
public function the_uuid_of_a_model_can_not_be_altered()
25+
{
26+
$model = factory(UuidModel::class)->create();
27+
28+
$this->assertTrue(Uuid::isValid($model->id));
29+
30+
$originalId = $model->id;
31+
32+
$model->id = 'something-else';
33+
$model->save();
34+
35+
$this->assertDatabaseHas('uuid_models', [
36+
'id' => $originalId,
37+
]);
38+
}
39+
}

0 commit comments

Comments
 (0)