Skip to content

Commit 71e9223

Browse files
Added main code
1 parent 33717ea commit 71e9223

File tree

10 files changed

+229
-4
lines changed

10 files changed

+229
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.idea
2+
/.phpunit.result.cache
23

34
# Created by https://www.gitignore.io/api/macos,phpstorm,composer,visualstudiocode
45
# Edit at https://www.gitignore.io/?templates=macos,phpstorm,composer,visualstudiocode

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818
},
1919
"autoload-dev": {
2020
"psr-4": {
21-
"GoldSpecDigital\\LaravelEloquentUUID\\Tests\\": "test/"
21+
"GoldSpecDigital\\LaravelEloquentUUID\\Tests\\": "tests/"
2222
}
2323
},
2424
"require": {
2525
"php": ">=7.1.0",
26-
"illuminate/database": "~5.8.0",
27-
"illuminate/support": "~5.8.0"
26+
"laravel/framework": "~5.8.0"
2827
},
2928
"require-dev": {
29+
"ext-pdo": "*",
3030
"friendsofphp/php-cs-fixer": "^2.15",
31+
"orchestra/testbench": "^3.5",
3132
"phpunit/phpunit": "^8.2"
3233
},
3334
"config": {

phpunit.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
stopOnFailure="true"
88
>
99
<testsuites>
10-
<testsuite name="Unit Tests">
10+
<testsuite name="GoldSpec Digital Test Suite">
1111
<directory>tests</directory>
1212
</testsuite>
1313
</testsuites>
14+
<filter>
15+
<whitelist>
16+
<directory suffix=".php">src/</directory>
17+
</whitelist>
18+
</filter>
19+
<php>
20+
<env name="DB_CONNECTION" value="testing"/>
21+
</php>
1422
</phpunit>

src/Database/Eloquent/Model.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent;
6+
7+
use Illuminate\Database\Eloquent\Model as BaseModel;
8+
use Illuminate\Support\Str;
9+
10+
abstract class Model extends BaseModel
11+
{
12+
/**
13+
* The "type" of the auto-incrementing ID.
14+
*
15+
* @var string
16+
*/
17+
protected $keyType = 'string';
18+
19+
/**
20+
* Indicates if the IDs are UUIDs.
21+
*
22+
* @var bool
23+
*/
24+
protected $keyIsUuid = true;
25+
26+
/**
27+
* Indicates if the IDs are auto-incrementing.
28+
*
29+
* @var bool
30+
*/
31+
public $incrementing = false;
32+
33+
/**
34+
* The attributes that are mass assignable.
35+
*
36+
* @var array
37+
*/
38+
protected $guarded = [];
39+
40+
/**
41+
* The attributes that should be cast to native types.
42+
*
43+
* @var array
44+
*/
45+
protected $casts = [
46+
'created_at' => 'datetime',
47+
'updated_at' => 'datetime',
48+
'deleted_at' => 'datetime',
49+
];
50+
51+
/**
52+
* The "booting" method of the model.
53+
*/
54+
protected static function boot(): void
55+
{
56+
parent::boot();
57+
58+
static::creating(function (self $model) {
59+
// Automatically generate a UUID if using them, and not provided.
60+
if ($model->keyIsUuid && empty($model->{$model->getKeyName()})) {
61+
$model->{$model->getKeyName()} = Str::uuid()->toString();
62+
}
63+
});
64+
}
65+
}

src/Foundation/Auth/User.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Foundation\Auth;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
8+
use Illuminate\Auth\Authenticatable;
9+
use Illuminate\Auth\MustVerifyEmail;
10+
use Illuminate\Auth\Passwords\CanResetPassword;
11+
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
12+
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
13+
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
14+
use Illuminate\Foundation\Auth\Access\Authorizable;
15+
16+
class User extends Model implements
17+
AuthenticatableContract,
18+
AuthorizableContract,
19+
CanResetPasswordContract
20+
{
21+
use Authenticatable;
22+
use Authorizable;
23+
use CanResetPassword;
24+
use MustVerifyEmail;
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Database\Eloquent;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid;
8+
use GoldSpecDigital\LaravelEloquentUUID\Tests\TestCase;
9+
use Illuminate\Support\Str;
10+
11+
class ModelWithUuidTest extends TestCase
12+
{
13+
/** @test */
14+
public function it_generates_a_uuid_when_the_id_has_not_been_set(): void
15+
{
16+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid $testModel */
17+
$testModel = TestModelWithUuid::query()->create();
18+
19+
$this->assertEquals(36, mb_strlen($testModel->id));
20+
}
21+
22+
/** @test */
23+
public function it_uses_the_uuid_provided_when_id_has_been_set(): void
24+
{
25+
$uuid = Str::uuid()->toString();
26+
27+
/** @var \GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithUuid $testModel */
28+
$testModel = TestModelWithUuid::query()->create([
29+
'id' => $uuid,
30+
]);
31+
32+
$this->assertEquals($uuid, $testModel->id);
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Database\Eloquent;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Tests\Models\TestModelWithoutUuid;
8+
use GoldSpecDigital\LaravelEloquentUUID\Tests\TestCase;
9+
use PDOException;
10+
11+
class ModelWithoutUuidTest extends TestCase
12+
{
13+
/** @test */
14+
public function it_does_not_generate_a_uuid_when_no_id_has_been_set(): void
15+
{
16+
$this->expectException(PDOException::class);
17+
18+
TestModelWithoutUuid::query()->create();
19+
}
20+
}

tests/Models/TestModelWithUuid.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Models;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
8+
9+
class TestModelWithUuid extends Model
10+
{
11+
//
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests\Models;
6+
7+
use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;
8+
9+
class TestModelWithoutUuid extends Model
10+
{
11+
/**
12+
* Indicates if the IDs are UUIDs.
13+
*
14+
* @var bool
15+
*/
16+
protected $keyIsUuid = false;
17+
}

tests/TestCase.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GoldSpecDigital\LaravelEloquentUUID\Tests;
6+
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Foundation\Application;
9+
use Orchestra\Testbench\TestCase as Orchestra;
10+
11+
abstract class TestCase extends Orchestra
12+
{
13+
/**
14+
* Setup the test environment.
15+
*/
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->setUpDatabase($this->app);
21+
}
22+
23+
/**
24+
* @param \Illuminate\Foundation\Application $app
25+
*/
26+
protected function setUpDatabase(Application $app): void
27+
{
28+
$app['db']->connection()
29+
->getSchemaBuilder()
30+
->create('test_model_with_uuids', function (Blueprint $table): void {
31+
$table->uuid('id')->primary();
32+
$table->timestamps();
33+
});
34+
35+
$app['db']->connection()
36+
->getSchemaBuilder()
37+
->create('test_model_without_uuids', function (Blueprint $table): void {
38+
$table->string('id')->primary();
39+
$table->timestamps();
40+
});
41+
}
42+
}

0 commit comments

Comments
 (0)