Skip to content

Commit

Permalink
[feature/validation-wrapping] Refactor ValidationExceptionBuilder for…
Browse files Browse the repository at this point in the history
… readability

Added newline separators in ValidationExceptionBuilder class properties and methods for improved readability. Modified test cases to include spaces in callback expectations for consistency.
  • Loading branch information
midnite81 committed Sep 4, 2024
1 parent 8a665b6 commit f562f10
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
26 changes: 25 additions & 1 deletion src/Validation/ValidationExceptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@
class ValidationExceptionBuilder
{
protected string $message;

protected string $redirectUrl;

protected ?string $fragment = null;

protected array $queryParameters = [];

protected ?string $routeName = null;

protected array $routeParameters = [];

protected ?string $errorBag = null;

protected bool $shouldFlash = false;

protected string $flashKey = 'error';

protected ?string $flashMessage = null;

protected string $exceptionClass = ValidationException::class;

protected mixed $exceptionCallback = null;

/**
Expand Down Expand Up @@ -55,6 +66,7 @@ public function redirectTo(string $url): self
{
$this->redirectUrl = $url;
$this->routeName = null; // Reset route if URL is set directly

return $this;
}

Expand All @@ -67,6 +79,7 @@ public function redirectBack(): self
{
$this->redirectUrl = URL::previous();
$this->routeName = null; // Reset route if redirecting back

return $this;
}

Expand All @@ -81,6 +94,7 @@ public function redirectRoute(string $name, array $parameters = []): self
{
$this->routeName = $name;
$this->routeParameters = $parameters;

return $this;
}

Expand All @@ -93,6 +107,7 @@ public function redirectRoute(string $name, array $parameters = []): self
public function fragment(string $fragment): self
{
$this->fragment = $fragment;

return $this;
}

Expand All @@ -105,6 +120,7 @@ public function fragment(string $fragment): self
public function withQueryParameters(array $params): self
{
$this->queryParameters = $params;

return $this;
}

Expand All @@ -117,6 +133,7 @@ public function withQueryParameters(array $params): self
public function errorBag(string $errorBag): self
{
$this->errorBag = $errorBag;

return $this;
}

Expand All @@ -132,6 +149,7 @@ public function flash(?string $message = null, string $key = 'error'): self
$this->shouldFlash = true;
$this->flashMessage = $message;
$this->flashKey = $key;

return $this;
}

Expand All @@ -140,14 +158,16 @@ public function flash(?string $message = null, string $key = 'error'): self
*
* @param string $exceptionClass The fully qualified class name of the exception.
* @return self
*
* @throws \InvalidArgumentException If the class is not a subclass of Exception.
*/
public function withException(string $exceptionClass): self
{
if (!is_subclass_of($exceptionClass, Exception::class)) {
throw new \InvalidArgumentException("The provided class must be a subclass of Exception.");
throw new \InvalidArgumentException('The provided class must be a subclass of Exception.');
}
$this->exceptionClass = $exceptionClass;

return $this;
}

Expand All @@ -160,6 +180,7 @@ public function withException(string $exceptionClass): self
public function withExceptionCallback(callable $callback): self
{
$this->exceptionCallback = $callback;

return $this;
}

Expand Down Expand Up @@ -189,6 +210,7 @@ public function throwException(): void
* Throw the configured exception if the given condition is true.
*
* @param bool|callable $condition A boolean value or a callback that returns a boolean.
*
* @throws Exception
*/
public function throwExceptionIf($condition): void
Expand All @@ -204,6 +226,7 @@ public function throwExceptionIf($condition): void
* Throw the configured exception unless the given condition is true.
*
* @param bool|callable $condition A boolean value or a callback that returns a boolean.
*
* @throws Exception
*/
public function throwExceptionUnless($condition): void
Expand Down Expand Up @@ -252,6 +275,7 @@ protected function createDefaultException(string $url): Exception
if ($this->errorBag) {
$exception->errorBag($this->errorBag);
}

return $exception;
}

Expand Down
20 changes: 10 additions & 10 deletions tests/Validation/ValidationExceptionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
$builder = ValidationExceptionBuilder::message('Test message')
->withException(\RuntimeException::class);

expect(fn() => $builder->throwException())->toThrow(\RuntimeException::class, 'Test message');
expect(fn () => $builder->throwException())->toThrow(\RuntimeException::class, 'Test message');
});

test('it uses custom exception callback', function () {
Expand All @@ -147,25 +147,25 @@
return new \RuntimeException("Custom: $message");
});

expect(fn() => $builder->throwException())->toThrow(\RuntimeException::class, 'Custom: Test message');
expect(fn () => $builder->throwException())->toThrow(\RuntimeException::class, 'Custom: Test message');
});

test('it throws exception conditionally with if', function () {
$builder = ValidationExceptionBuilder::message('Test message');

expect(fn() => $builder->throwExceptionIf(true))->toThrow(ValidationException::class);
expect(fn() => $builder->throwExceptionIf(false))->not->toThrow(ValidationException::class);
expect(fn () => $builder->throwExceptionIf(true))->toThrow(ValidationException::class);
expect(fn () => $builder->throwExceptionIf(false))->not->toThrow(ValidationException::class);

expect(fn() => $builder->throwExceptionIf(fn() => true))->toThrow(ValidationException::class);
expect(fn() => $builder->throwExceptionIf(fn() => false))->not->toThrow(ValidationException::class);
expect(fn () => $builder->throwExceptionIf(fn () => true))->toThrow(ValidationException::class);
expect(fn () => $builder->throwExceptionIf(fn () => false))->not->toThrow(ValidationException::class);
});

test('it throws exception conditionally with unless', function () {
$builder = ValidationExceptionBuilder::message('Test message');

expect(fn() => $builder->throwExceptionUnless(false))->toThrow(ValidationException::class);
expect(fn() => $builder->throwExceptionUnless(true))->not->toThrow(ValidationException::class);
expect(fn () => $builder->throwExceptionUnless(false))->toThrow(ValidationException::class);
expect(fn () => $builder->throwExceptionUnless(true))->not->toThrow(ValidationException::class);

expect(fn() => $builder->throwExceptionUnless(fn() => false))->toThrow(ValidationException::class);
expect(fn() => $builder->throwExceptionUnless(fn() => true))->not->toThrow(ValidationException::class);
expect(fn () => $builder->throwExceptionUnless(fn () => false))->toThrow(ValidationException::class);
expect(fn () => $builder->throwExceptionUnless(fn () => true))->not->toThrow(ValidationException::class);
});

0 comments on commit f562f10

Please sign in to comment.