Skip to content

Commit

Permalink
Merge pull request #33 from AlexCatch/feature/identified-by-uuid
Browse files Browse the repository at this point in the history
Add IdentifiedByUUID trait
  • Loading branch information
AlexCatch authored Jul 1, 2019
2 parents 7375dc6 + 234b4ea commit d11919b
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 3 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ If you have never used the Composer dependency manager before, head to the [Comp
- [`ApiResponse`](#apiresponse)
- [`Response`](#response)
- [`ResponseCache`](#responsecache)

- [`IdentifiedByUUID`](#identifiedbyuuid)

### `Models`
The [Models helper](src/LangleyFoxall/Helpers/Models.php) offers helpful functions to do with [Eloquent Models](https://laravel.com/docs/eloquent).
Expand Down Expand Up @@ -656,4 +656,33 @@ if($cache->hasData()){
}
return ApiResponse::success($data)->cache(1, $cache)->json();
```
```

### `IdentifiedByUUID`

The `IdentifiedByUUID` trait allows you to easily use V4 UUIDs over incrementing primary keys.

###### Example Usage

Change your migrations to allow the use of a UUID.

```php
public function up()
{
Schema::create('demos', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->timestamps();
});
}
```

Use the trait.

```php
class Demo extends Model
{
use IdentifiedByUUID;
}
```

Now when the model is saved, it will automatically be populated with a V4 UUID.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@
"php": "^7.0.0",
"illuminate/support": ">=5.0",
"illuminate/database": ">=5.0",
"illuminate/http": ">=5.0"
"illuminate/http": ">=5.0",
"orchestra/testbench": "3.8.x-dev"
},
"autoload": {
"psr-4": {
"LangleyFoxall\\": "src/LangleyFoxall/"
}
},
"autoload-dev": {
"psr-4": {
"LangleyFoxall\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.x-dev"
Expand Down
25 changes: 25 additions & 0 deletions phpunit.xml
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>
31 changes: 31 additions & 0 deletions src/LangleyFoxall/Helpers/Traits/IdentifiedByUUID.php
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;
}
}
43 changes: 43 additions & 0 deletions tests/Traits/IdentifiedByUUID/IdentifiedByUUIDTest.php
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);
}
}
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');
}
}
21 changes: 21 additions & 0 deletions tests/Traits/IdentifiedByUUID/Models/Demo.php
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'];
}

0 comments on commit d11919b

Please sign in to comment.