-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from AlexCatch/feature/identified-by-uuid
Add IdentifiedByUUID trait
- Loading branch information
Showing
7 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Helpers Laravel Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/LangleyFoxall</directory> | ||
</whitelist> | ||
</filter> | ||
<php> | ||
<env name="DB_CONNECTION" value="testing"/> | ||
</php> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace LangleyFoxall\Helpers\Traits; | ||
|
||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* Trait IdentifiedByUUID. | ||
*/ | ||
trait IdentifiedByUUID | ||
{ | ||
/** | ||
* Boot the trait. | ||
*/ | ||
public static function bootIdentifiedByUUID() | ||
{ | ||
static::creating(function ($model) { | ||
if (!$model->getKey()) { | ||
$model->{$model->getKeyName()} = (string) Str::uuid(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getIncrementing() | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace LangleyFoxall\Traits\IdentifiedByUUID; | ||
|
||
use LangleyFoxall\Traits\IdentifiedByUUID\Models\Demo; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class IdentifiedByUUIDTest extends TestCase | ||
{ | ||
/** | ||
* Setup the test. | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->loadMigrationsFrom(__DIR__.'/Migrations'); | ||
$this->artisan('migrate', ['--database' => 'testing'])->run(); | ||
} | ||
|
||
/** | ||
* Check that saving a model correctly saves a UUID. | ||
* | ||
* @return void | ||
*/ | ||
public function testSaveStoresUUID() | ||
{ | ||
$demo = Demo::create(['text' => 'hello world']); | ||
$this->assertRegExp('/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', $demo->uuid); | ||
} | ||
|
||
/** | ||
* Check that updating the model does not change the UUID. | ||
*/ | ||
public function testUpdateDoesNotChangeUUID() | ||
{ | ||
$demo = Demo::create(['text' => 'hello world']); | ||
$uuid = $demo->uuid; | ||
|
||
$demo->update(['text' => 'updated text']); | ||
|
||
$this->assertTrue($demo->uuid === $uuid); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/Traits/IdentifiedByUUID/Migrations/2019_06_28_121259_create_test_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateTestTable extends Migration | ||
{ | ||
/** | ||
* Run the Migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('demos', function (Blueprint $table) { | ||
$table->uuid('uuid')->primary(); | ||
$table->string('text'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the Migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('demos'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace LangleyFoxall\Traits\IdentifiedByUUID\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use LangleyFoxall\Helpers\Traits\IdentifiedByUUID; | ||
|
||
class Demo extends Model | ||
{ | ||
use IdentifiedByUUID; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $primaryKey = 'uuid'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $fillable = ['text']; | ||
} |