Skip to content

Commit 0b19df4

Browse files
authored
Merge pull request #6 from netsells/feature/laravel-11-support
Add support for Laravel 11
2 parents 945b659 + 14752f4 commit 0b19df4

File tree

5 files changed

+67
-41
lines changed

5 files changed

+67
-41
lines changed

.github/workflows/tests.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
php: ['8.1', '8.2']
15-
laravel: ['^9.1', '^10.0']
14+
php: ['8.2', '8.3']
15+
laravel: ['^10.0', '^11.0']
1616

1717
steps:
1818
- name: Checkout the repo

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
}
2323
],
2424
"require": {
25-
"php": "^8.1",
26-
"hashids/hashids": "^4.1",
27-
"laravel/framework": "^9.1 || ^10.0"
25+
"php": "^8.2",
26+
"hashids/hashids": "^5.0",
27+
"illuminate/contracts": "^10.0|^11.0",
28+
"illuminate/database": "^10.0|^11.0",
29+
"illuminate/support": "^10.0|^11.0",
30+
"illuminate/validation": "^10.0|^11.0"
2831
},
2932
"require-dev": {
30-
"orchestra/testbench": "^7.1 || ^8.0"
33+
"orchestra/testbench": "^8.0|^9.0"
3134
},
3235
"autoload": {
3336
"psr-4": {

src/ExistsWithHashedIdRule.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Netsells\HashModelIds;
44

5-
use Illuminate\Contracts\Validation\Rule;
5+
use Closure;
6+
use Illuminate\Contracts\Validation\ValidationRule;
67
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Support\Traits\ForwardsCalls;
@@ -11,7 +12,7 @@
1112
/**
1213
* @mixin \Illuminate\Database\Eloquent\Builder
1314
*/
14-
class ExistsWithHashedIdRule implements Rule
15+
class ExistsWithHashedIdRule implements ValidationRule
1516
{
1617
use ForwardsCalls;
1718

@@ -52,33 +53,41 @@ public function __construct(string $class)
5253
}
5354

5455
/**
55-
* Determine if the validation rule passes.
56+
* Run the validation rule.
5657
*
57-
* @param string $attribute
58+
* @param string|null $attribute
5859
* @param mixed $value
59-
* @return bool
60+
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
61+
* @return void
6062
*/
61-
public function passes($attribute, $value): bool
63+
public function validate(?string $attribute, mixed $value, Closure $fail): void
6264
{
6365
if (! is_string($value) || empty($value)) {
64-
return false;
66+
$this->fail($fail);
67+
68+
return;
6569
}
6670

67-
return $this->class::whereHashedId($value)
71+
$doesntExist = $this->class::whereHashedId($value)
6872
->tap(function (Builder $query) {
6973
$this->applyConstraints($query);
7074
})
71-
->exists();
75+
->doesntExist();
76+
77+
if ($doesntExist) {
78+
$this->fail($fail);
79+
}
7280
}
7381

7482
/**
75-
* Get the validation error message.
83+
* Fail the validation.
7684
*
77-
* @return string
85+
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
86+
* @return void
7887
*/
79-
public function message(): string
88+
public function fail(Closure $fail): void
8089
{
81-
return __('hashModelIds::validation.model_not_exist_for_hashed_id', [
90+
$fail('hashModelIds::validation.model_not_exist_for_hashed_id')->translate([
8291
'name' => class_basename($this->class),
8392
]);
8493
}

tests/Integration/ExistsWithHashedIdRuleTest.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Netsells\HashModelIds\Tests\Integration;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Translation\Translator;
7+
use Illuminate\Validation\InvokableValidationRule;
8+
use Illuminate\Validation\Validator;
69
use InvalidArgumentException;
710
use Netsells\HashModelIds\ExistsWithHashedIdRule;
811
use Netsells\HashModelIds\Tests\Integration\Fixtures\Models;
@@ -25,7 +28,9 @@ public function testRuleExpectsHashingModels(): void
2528

2629
public function testRuleHandlesExpectedValueTypes(): void
2730
{
28-
$rule = new ExistsWithHashedIdRule(Models\Foo::class);
31+
$rule = InvokableValidationRule::make(
32+
invokable: new ExistsWithHashedIdRule(Models\Foo::class),
33+
)->setValidator(validator: $this->getValidator());
2934

3035
$this->assertFalse($rule->passes(null, null));
3136
$this->assertFalse($rule->passes(null, []));
@@ -36,7 +41,9 @@ public function testRulePassesForExistingHashedId(): void
3641
{
3742
$foo = Models\Foo::create();
3843

39-
$rule = new ExistsWithHashedIdRule(Models\Foo::class);
44+
$rule = InvokableValidationRule::make(
45+
invokable: new ExistsWithHashedIdRule(Models\Foo::class),
46+
)->setValidator(validator: $this->getValidator());
4047

4148
$this->assertTrue($rule->passes(null, $foo->hashed_id));
4249
}
@@ -45,7 +52,9 @@ public function testRuleFailsForNonExistentHashedId(): void
4552
{
4653
$foo = Models\Foo::create();
4754

48-
$rule = new ExistsWithHashedIdRule(Models\Foo::class);
55+
$rule = InvokableValidationRule::make(
56+
invokable: new ExistsWithHashedIdRule(Models\Foo::class),
57+
)->setValidator(validator: $this->getValidator());
4958

5059
$this->assertFalse($rule->passes(null, $foo->id));
5160
}
@@ -54,25 +63,32 @@ public function testRuleHandlesConstraintsFluently(): void
5463
{
5564
$foo = Models\Foo::create([]);
5665

57-
$rule = new ExistsWithHashedIdRule(Models\Foo::class);
58-
59-
$this->assertFalse(
60-
$rule
66+
$rule = InvokableValidationRule::make(
67+
invokable: (new ExistsWithHashedIdRule(Models\Foo::class))
6168
->where(function (Builder $query) use ($foo) {
6269
$query->whereId($foo->hashed_id);
63-
})
64-
->passes(null, $foo->hashed_id)
65-
);
70+
}),
71+
)->setValidator(validator: $this->getValidator());
6672

67-
$rule = new ExistsWithHashedIdRule(Models\Foo::class);
73+
$this->assertFalse($rule->passes(null, $foo->hashed_id));
6874

69-
$this->assertTrue(
70-
$rule
75+
$rule = InvokableValidationRule::make(
76+
invokable: (new ExistsWithHashedIdRule(Models\Foo::class))
7177
->where(function (Builder $query) use ($foo) {
7278
$query->whereId($foo->id);
7379
})
74-
->whereNotNull('created_at')
75-
->passes(null, $foo->hashed_id)
80+
->whereNotNull('created_at'),
81+
)->setValidator(validator: $this->getValidator());
82+
83+
$this->assertTrue($rule->passes(null, $foo->hashed_id));
84+
}
85+
86+
private function getValidator(): Validator
87+
{
88+
return new Validator(
89+
translator: $this->app->make(abstract: Translator::class),
90+
data: [],
91+
rules: [],
7692
);
7793
}
7894
}

tests/Unit/ModelIdHasherTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Netsells\HashModelIds\Tests\Unit;
44

5-
use Illuminate\Database\Eloquent\Model;
65
use InvalidArgumentException;
76
use Netsells\HashModelIds\ModelIdHasher;
87
use Netsells\HashModelIds\Tests\Unit\Fixtures\Models;
@@ -27,8 +26,7 @@ public function testHasherCanEncodeAndDecode(): void
2726
{
2827
$idHasher = $this->getNewModelIdHasher();
2928

30-
/** @var Model|MockObject $model */
31-
$model = $this->getMockForAbstractClass(Model::class);
29+
$model = new Models\Foo();
3230

3331
$hash = $idHasher->encode($model, 1);
3432

@@ -39,11 +37,11 @@ public function testHashesDifferForModelsWithSameId(): void
3937
{
4038
$idHasher = $this->getNewModelIdHasher();
4139

42-
/** @var Model|MockObject $foo */
43-
$foo = $this->getMockForAbstractClass(Models\Foo::class);
40+
/** @var \Illuminate\Database\Eloquent\Model|\PHPUnit\Framework\MockObject\MockObject $foo */
41+
$foo = $this->createMock(Models\Foo::class);
4442

45-
/** @var Model|MockObject $bar */
46-
$bar = $this->getMockForAbstractClass(Models\Bar::class);
43+
/** @var \Illuminate\Database\Eloquent\Model|\PHPUnit\Framework\MockObject\MockObject $bar */
44+
$bar = $this->createMock(Models\Bar::class);
4745

4846
$this->assertNotSame($idHasher->encode($foo, 1), $idHasher->encode($bar, 1));
4947
}

0 commit comments

Comments
 (0)