diff --git a/src/Bind/Validation/BindingExists.php b/src/Bind/Validation/BindingExists.php index 1504896f1..502dbef55 100644 --- a/src/Bind/Validation/BindingExists.php +++ b/src/Bind/Validation/BindingExists.php @@ -2,15 +2,14 @@ namespace Nuwave\Lighthouse\Bind\Validation; -use Closure; -use Illuminate\Contracts\Validation\ValidationRule; +use Illuminate\Contracts\Validation\InvokableRule; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Validation\Validator; use Nuwave\Lighthouse\Bind\BindDirective; use function is_array; -class BindingExists implements ValidationRule, ValidatorAwareRule +class BindingExists implements InvokableRule, ValidatorAwareRule { private ?Validator $validator = null; @@ -18,17 +17,16 @@ public function __construct( private BindDirective $directive, ) {} - public function setValidator(Validator $validator): self - { - $this->validator = $validator; - - return $this; - } - /** + * Because of backwards compatibility with Laravel 9, typehints for this method + * must be set through PHPDoc as the interface did not include typehints. + * @link https://laravel.com/docs/9.x/validation#using-rule-objects + * + * @param string $attribute + * @parent mixed $value * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail */ - public function validate(string $attribute, mixed $value, Closure $fail): void + public function __invoke($attribute, $value, $fail): void { $binding = $this->directive->transform($value); @@ -50,4 +48,18 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $this->validator?->addFailure("$attribute.$key", 'exists'); } } + + /** + * Because of backwards compatibility with Laravel 9, typehints for this method + * must be set through PHPDoc as the interface did not include typehints. + * @link https://laravel.com/docs/9.x/validation#custom-validation-rules + * + * @param \Illuminate\Validation\Validator $validator + */ + public function setValidator($validator): self + { + $this->validator = $validator; + + return $this; + } } diff --git a/tests/Integration/Bind/BindDirectiveTest.php b/tests/Integration/Bind/BindDirectiveTest.php index 86a5a307a..58f3c66d0 100644 --- a/tests/Integration/Bind/BindDirectiveTest.php +++ b/tests/Integration/Bind/BindDirectiveTest.php @@ -1349,14 +1349,15 @@ public function testMultipleBindingsInSameRequest(): void private function assertThrowsMultipleRecordsFoundException(Closure $makeRequest, int $count): void { - $this->assertThrows($makeRequest, function (Error $exception) use ($count): bool { - $expected = new MultipleRecordsFoundException($count); + try { + $makeRequest(); + } catch (Error $error) { + $this->assertInstanceOf(MultipleRecordsFoundException::class, $error->getPrevious()); + $this->assertEquals(new MultipleRecordsFoundException($count), $error->getPrevious()); - if (! $exception->getPrevious() instanceof $expected) { - return false; - } + return; + } - return $exception->getMessage() === $expected->getMessage(); - }); + $this->fail('Request did not throw an exception.'); } }