Skip to content

Commit

Permalink
Merge branch 'sudopeople/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldyrynda committed Sep 13, 2023
2 parents 81976f5 + b14f0d6 commit e2f72e9
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 8 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ class Post extends Model
}
```

By default, this package will use UUID version 4 values, however, you are welcome to use `uuid1`, `uuid4`, or `uuid6` by specifying the protected property `$uuidVersion` in your model. Should you wish to take advantage of [ordered UUID (version 4) values that were introduced in Laravel 5.6](https://laravel.com/docs/master/helpers#method-str-ordered-uuid), you should specify `ordered` as the `$uuidVersion` in your model.
The package will use `uuid` as the column name to store the generated UUID value by default. If you prefer a different name, you may change the `model-uuid.column_name` config variable.

You may also override the `uuidColumn` method on a per-model basis.

By default, this package will use UUID version 4 values, however, you are welcome to use `uuid1`, `uuid4`, `uuid6`, or `uuid7` by specifying the protected property `$uuidVersion` in your model. Should you wish to take advantage of [ordered UUID (version 4) values that were introduced in Laravel 5.6](https://laravel.com/docs/master/helpers#method-str-ordered-uuid), you should specify `ordered` as the `$uuidVersion` in your model.

```php
<?php
Expand Down Expand Up @@ -195,6 +199,13 @@ This package is installed via [Composer](https://getcomposer.org/). To install,
```bash
composer require dyrynda/laravel-model-uuid
```

If you wish to override default configuration, you may publish the configuration file to your application.

```bash
php artisan vendor:publish --tag=model-uuid-config
```

## Support

If you are having general issues with this package, feel free to contact me on [Twitter](https://twitter.com/michaeldyrynda).
Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"laravel/legacy-factories": "^1.3",
"laravel/pint": "^1.13",
"orchestra/testbench": "^8.0",
"phpunit/phpunit": "^9.6.0 || ^10.0.7"
"phpunit/phpunit": "^9.6.0 || ^10.0.7",
"spatie/laravel-package-tools": "^1.16"
},
"autoload-dev": {
"psr-4": {
Expand All @@ -50,6 +51,11 @@
"extra": {
"branch-alias": {
"dev-main": "7.x-dev"
},
"laravel": {
"providers": [
"Dyrynda\\Database\\Support\\LaravelModelUuidServiceProvider"
]
}
}
}
8 changes: 8 additions & 0 deletions config/model-uuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
/**
* The default column name which should be used to store the generated UUID value.
*/
'column_name' => 'uuid',
];
2 changes: 1 addition & 1 deletion src/GeneratesUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static function bootGeneratesUuid(): void
*/
public function uuidColumn(): string
{
return 'uuid';
return config('model-uuid.column_name', 'uuid');
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/LaravelModelUuidServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Dyrynda\Database\Support;

use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class LaravelModelUuidServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package
->name('model-uuid')
->hasConfigFile()
->hasInstallCommand(function (InstallCommand $command) {
$command->publishConfigFile();
});
}
}
34 changes: 34 additions & 0 deletions tests/Feature/GeneratesUuidTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\Feature;

use Config;
use Dyrynda\Database\Support\GeneratesUuid;
use Tests\TestCase;

class GeneratesUuidTest extends TestCase
{
/**
* @test
*/
public function it_gets_default_column_name()
{
$testModelThatGeneratesUuid = new class()
{
use GeneratesUuid;
};

$this->assertSame(
$testModelThatGeneratesUuid->uuidColumn(),
'uuid',
'The UUID column should be "uuid" when no default is configured.'
);

Config::set('model-uuid.column_name', 'uuid_custom');
$this->assertSame(
$testModelThatGeneratesUuid->uuidColumn(),
'uuid_custom',
'The UUID column should match the configured value.'
);
}
}
13 changes: 8 additions & 5 deletions tests/Unit/UuidResolversTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit;

use Dyrynda\Database\Support\GeneratesUuid;
use Mockery;
use PHPUnit\Framework\TestCase;

class UuidResolversTest extends TestCase
Expand All @@ -29,12 +30,14 @@ public static function provider_for_it_handles_uuid_versions(): array
*/
public function it_handles_uuid_versions($version, $resolved)
{
/* @var \Dyrynda\Database\Support\GeneratesUuid $generator */
$generator = $this->getMockForTrait(GeneratesUuid::class, mockedMethods: [
'uuidVersion',
]);
$generator->method('uuidVersion')->willReturn($version);
$generator = Mockery::mock(UuidTestClass::class)->makePartial();
$generator->shouldReceive('uuidVersion')->once()->andReturn($version);

$this->assertSame($resolved, $generator->resolveUuidVersion());
}
}

class UuidTestClass
{
use GeneratesUuid;
}

0 comments on commit e2f72e9

Please sign in to comment.