Skip to content

Commit

Permalink
feat: add support for Laravel 11
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred committed Mar 13, 2024
1 parent 945b659 commit 606d53a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.1', '8.2']
laravel: ['^9.1', '^10.0']
php: ['8.2', '8.3']
laravel: ['^10.0', '^11.0']

steps:
- name: Checkout the repo
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
}
],
"require": {
"php": "^8.1",
"hashids/hashids": "^4.1",
"laravel/framework": "^9.1 || ^10.0"
"php": "^8.2",
"hashids/hashids": "^5.0",
"illuminate/translation": "^10.0|^11.0",
"illuminate/support": "^10.0|^11.0",
"illuminate/validation": "^10.0|^11.0"
},
"require-dev": {
"orchestra/testbench": "^7.1 || ^8.0"
"orchestra/testbench": "^8.0|^9.0"
},
"autoload": {
"psr-4": {
Expand Down
34 changes: 22 additions & 12 deletions src/ExistsWithHashedIdRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Netsells\HashModelIds;

use Closure;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\ForwardsCalls;
Expand All @@ -11,7 +13,7 @@
/**
* @mixin \Illuminate\Database\Eloquent\Builder
*/
class ExistsWithHashedIdRule implements Rule
class ExistsWithHashedIdRule implements ValidationRule
{
use ForwardsCalls;

Expand Down Expand Up @@ -52,33 +54,41 @@ public function __construct(string $class)
}

/**
* Determine if the validation rule passes.
* Run the validation rule.
*
* @param string $attribute
* @param string|null $attribute
* @param mixed $value
* @return bool
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
* @return void
*/
public function passes($attribute, $value): bool
public function validate(?string $attribute, mixed $value, Closure $fail): void
{
if (! is_string($value) || empty($value)) {
return false;
$this->fail($fail);

return;
}

return $this->class::whereHashedId($value)
$doesntExist = $this->class::whereHashedId($value)
->tap(function (Builder $query) {
$this->applyConstraints($query);
})
->exists();
->doesntExist();

if ($doesntExist) {
$this->fail($fail);
}
}

/**
* Get the validation error message.
* Fail the validation.
*
* @return string
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
* @return void
*/
public function message(): string
public function fail(Closure $fail): void
{
return __('hashModelIds::validation.model_not_exist_for_hashed_id', [
$fail('hashModelIds::validation.model_not_exist_for_hashed_id')->translate([
'name' => class_basename($this->class),
]);
}
Expand Down
46 changes: 31 additions & 15 deletions tests/Integration/ExistsWithHashedIdRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Netsells\HashModelIds\Tests\Integration;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Translation\Translator;
use Illuminate\Validation\InvokableValidationRule;
use Illuminate\Validation\Validator;
use InvalidArgumentException;
use Netsells\HashModelIds\ExistsWithHashedIdRule;
use Netsells\HashModelIds\Tests\Integration\Fixtures\Models;
Expand All @@ -25,7 +28,9 @@ public function testRuleExpectsHashingModels(): void

public function testRuleHandlesExpectedValueTypes(): void
{
$rule = new ExistsWithHashedIdRule(Models\Foo::class);
$rule = InvokableValidationRule::make(
invokable: new ExistsWithHashedIdRule(Models\Foo::class),
)->setValidator(validator: $this->getValidator());

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

$rule = new ExistsWithHashedIdRule(Models\Foo::class);
$rule = InvokableValidationRule::make(
invokable: new ExistsWithHashedIdRule(Models\Foo::class),
)->setValidator(validator: $this->getValidator());

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

$rule = new ExistsWithHashedIdRule(Models\Foo::class);
$rule = InvokableValidationRule::make(
invokable: new ExistsWithHashedIdRule(Models\Foo::class),
)->setValidator(validator: $this->getValidator());

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

$rule = new ExistsWithHashedIdRule(Models\Foo::class);

$this->assertFalse(
$rule
$rule = InvokableValidationRule::make(
invokable: (new ExistsWithHashedIdRule(Models\Foo::class))
->where(function (Builder $query) use ($foo) {
$query->whereId($foo->hashed_id);
})
->passes(null, $foo->hashed_id)
);
}),
)->setValidator(validator: $this->getValidator());

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

$this->assertTrue(
$rule
$rule = InvokableValidationRule::make(
invokable: (new ExistsWithHashedIdRule(Models\Foo::class))
->where(function (Builder $query) use ($foo) {
$query->whereId($foo->id);
})
->whereNotNull('created_at')
->passes(null, $foo->hashed_id)
->whereNotNull('created_at'),
)->setValidator(validator: $this->getValidator());

$this->assertTrue($rule->passes(null, $foo->hashed_id));
}

private function getValidator(): Validator
{
return new Validator(
translator: $this->app->make(abstract: Translator::class),
data: [],
rules: [],
);
}
}
12 changes: 5 additions & 7 deletions tests/Unit/ModelIdHasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Netsells\HashModelIds\Tests\Unit;

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

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

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

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

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

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

$this->assertNotSame($idHasher->encode($foo, 1), $idHasher->encode($bar, 1));
}
Expand Down

0 comments on commit 606d53a

Please sign in to comment.