generated from rawilk/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #12 from rawilk/feature/uuid-generator
Add uuid generator
- Loading branch information
Showing
3 changed files
with
46 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\HumanKeys\Generators; | ||
|
||
use Illuminate\Support\Str; | ||
use Rawilk\HumanKeys\Contracts\Generator; | ||
|
||
class UuidGenerator implements Generator | ||
{ | ||
public function generate(?string $prefix = null): string | ||
{ | ||
return implode('_', [ | ||
$prefix, | ||
str_replace('-', '', Str::uuid()), | ||
]); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Support\Str; | ||
use Rawilk\HumanKeys\Generators\UuidGenerator; | ||
use Rawilk\HumanKeys\Tests\Fixture\Models\Post; | ||
|
||
beforeEach(function () { | ||
config([ | ||
'human-keys.generator' => UuidGenerator::class, | ||
]); | ||
}); | ||
|
||
it('can generate uuid ids for a model', function () { | ||
$uuid = str_replace('-', '', (string) Str::uuid()); | ||
|
||
Str::createUuidsUsing(fn () => $uuid); | ||
|
||
$post = Post::create(['name' => 'Example post']); | ||
|
||
expect($post) | ||
->id->toBe("pos_{$uuid}"); | ||
|
||
Str::createUuidsNormally(); | ||
}); |