Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 06ba6a7

Browse files
Merge pull request #45 from afshiney/feature/efficient-uuid-validation
Efficient UUID "Existing" Validation
2 parents d4666e7 + 7e46f93 commit 06ba6a7

File tree

8 files changed

+188
-8
lines changed

8 files changed

+188
-8
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ class Post extends Model
7676
}
7777
```
7878

79+
### Validation
80+
81+
Should you wish to use the efficient UUID column as part of your validation strategy, you may use the `EfficientUuidExists` rule as normal.
82+
83+
```php
84+
use Dyrynda\Database\Rules\EfficientUuidExists;
85+
86+
public function update(Request $request, User $user)
87+
{
88+
$request->validate([
89+
// Using the default column name
90+
'uuid' => [new EfficientUuidExists(Post::class)],
91+
92+
// Using a custom column name
93+
'custom_uuid' => [new EfficientUuidExists(Post::class, 'custom_uuid')],
94+
]);
95+
}
96+
```
97+
7998
## Support
8099

81100
If you are having general issues with this package, feel free to contact me on [Twitter](https://twitter.com/michaeldyrynda).

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
],
1313
"require": {
1414
"php": "^7.3|^8.0",
15+
"dyrynda/laravel-model-uuid": "^6.5",
1516
"illuminate/container": "^8.12",
1617
"illuminate/contracts": "^8.12",
1718
"illuminate/database": "^8.12",
1819
"ramsey/uuid": "^4.1"
1920
},
2021
"require-dev": {
21-
"phpunit/phpunit": "^9.3",
22+
"laravel/legacy-factories": "^1.1",
2223
"mockery/mockery": "^1.4.2",
23-
"orchestra/testbench": "^6.0"
24+
"orchestra/testbench": "^6.0",
25+
"phpunit/phpunit": "^9.3"
2426
},
2527
"autoload": {
2628
"psr-4": {

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@
2222
<directory>./tests/</directory>
2323
</testsuite>
2424
</testsuites>
25+
<php>
26+
<env name="DB_CONNECTION" value="testing"/>
27+
</php>
2528
</phpunit>

src/Rules/EfficientUuidExists.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Dyrynda\Database\Rules;
4+
5+
use Ramsey\Uuid\Uuid;
6+
use Illuminate\Contracts\Validation\Rule;
7+
8+
class EfficientUuidExists implements Rule
9+
{
10+
/** @var \Dyrynda\Database\GeneratesUuid */
11+
protected $model;
12+
13+
/** @var string */
14+
protected $column;
15+
16+
public function __construct(string $model, string $column = 'uuid')
17+
{
18+
$this->model = new $model;
19+
20+
$this->column = $column;
21+
}
22+
23+
public function passes($attribute, $value): bool
24+
{
25+
if (Uuid::isValid($value)) {
26+
$binaryUuid = Uuid::fromString(strtolower($value))->getBytes();
27+
28+
return $this->model->where($this->column, $binaryUuid)->exists();
29+
}
30+
31+
return false;
32+
}
33+
34+
public function message(): string
35+
{
36+
return trans('validation.exists');
37+
}
38+
}

tests/EfficientUuidExistsRuleTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Dyrynda\Database\Rules\EfficientUuidExists;
6+
use Ramsey\Uuid\Uuid;
7+
use Tests\Fixtures\EfficientUuidPost;
8+
9+
class EfficientUuidExistsRuleTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_passes_valid_existing_uuid()
13+
{
14+
/** @var \Tests\Fixtures\EfficientUuidPost $post */
15+
$post = factory(EfficientUuidPost::class)->create();
16+
17+
$rule = new EfficientUuidExists(EfficientUuidPost::class);
18+
19+
$this->assertTrue($rule->passes('uuid', $post->uuid));
20+
}
21+
22+
/** @test */
23+
public function it_fails_on_non_existing_uuid()
24+
{
25+
$uuid = Uuid::uuid4();
26+
27+
$rule = new EfficientUuidExists(EfficientUuidPost::class);
28+
29+
$this->assertFalse($rule->passes('post_id', $uuid));
30+
}
31+
32+
/** @test */
33+
public function it_fails_on_any_non_uuid_invalid_strings()
34+
{
35+
$uuid = "1235123564354633";
36+
37+
$rule = new EfficientUuidExists(EfficientUuidPost::class, 'uuid');
38+
39+
$this->assertFalse($rule->passes('post_id', $uuid));
40+
}
41+
42+
/** @test */
43+
public function it_works_with_custom_uuid_column_name()
44+
{
45+
/** @var \Tests\Fixtures\EfficientUuidPost $post */
46+
$post = factory(EfficientUuidPost::class)->create();
47+
48+
$rule = new EfficientUuidExists(EfficientUuidPost::class, 'custom_uuid');
49+
50+
$this->assertTrue($rule->passes('custom_uuid', $post->custom_uuid));
51+
}
52+
}

tests/Fixtures/EfficientUuidPost.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Tests\Fixtures;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Dyrynda\Database\Casts\EfficientUuid;
7+
use Dyrynda\Database\Support\GeneratesUuid;
8+
9+
class EfficientUuidPost extends Model
10+
{
11+
use GeneratesUuid;
12+
13+
protected $table = 'posts';
14+
15+
public $timestamps = false;
16+
17+
protected $casts = [
18+
'uuid' => EfficientUuid::class,
19+
'custom_uuid' => EfficientUuid::class,
20+
];
21+
22+
public function uuidColumn(): string
23+
{
24+
return 'uuid';
25+
}
26+
27+
public function uuidColumns(): array
28+
{
29+
return ['uuid', 'custom_uuid'];
30+
}
31+
}

tests/TestCase.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,37 @@
22

33
namespace Tests;
44

5+
use Illuminate\Support\Facades\Schema;
6+
use Illuminate\Database\Schema\Blueprint;
57
use Dyrynda\Database\LaravelEfficientUuidServiceProvider;
68

79
class TestCase extends \Orchestra\Testbench\TestCase
810
{
9-
protected function getPackageProviders($app)
10-
{
11-
return [
12-
LaravelEfficientUuidServiceProvider::class,
13-
];
14-
}
11+
protected function setUp(): void
12+
{
13+
parent::setUp();
14+
15+
$this->withFactories(realpath(__DIR__ . '/database/factories'));
16+
17+
$this->setupDatabase($this->app);
18+
}
19+
20+
protected function getPackageProviders($app)
21+
{
22+
return [
23+
LaravelEfficientUuidServiceProvider::class,
24+
];
25+
}
26+
27+
protected function setupDatabase($app)
28+
{
29+
Schema::dropAllTables();
30+
31+
$app['db']->connection()->getSchemaBuilder()->create('posts', function (Blueprint $table) {
32+
$table->increments('id');
33+
$table->efficientUuid('uuid');
34+
$table->efficientUuid('custom_uuid');
35+
$table->string('title');
36+
});
37+
}
1538
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
/** @var \Illuminate\Database\Eloquent\Factory $factory */
4+
5+
use Faker\Generator as Faker;
6+
use Tests\Fixtures\EfficientUuidPost;
7+
8+
$factory->define(EfficientUuidPost::class, function (Faker $faker) {
9+
return [
10+
'title' => $faker->sentence,
11+
];
12+
});

0 commit comments

Comments
 (0)