Skip to content

Commit

Permalink
Fix error when converting input to string
Browse files Browse the repository at this point in the history
Some assertors needed to convert the input to string when customizing
exception messages, and that caused PHP errors.

I didn't make the exact change for the length rule because the input
will always be an integer.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
  • Loading branch information
henriquemoody committed Mar 21, 2023
1 parent 0c0ad39 commit 868d59d
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Assertor/AllAssertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private function getCustomizedException(mixed $asserted, ValidationException $ex
}

$params = $exception->getParams();
$params['name'] = $params['input'] . ', and all values of ' . stringify($asserted) . ',';
$params['name'] = stringify($params['input']) . ', and all values of ' . stringify($asserted) . ',';
$exception->updateParams($params);

return $exception;
Expand Down
2 changes: 1 addition & 1 deletion src/Assertor/MaxAssertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private function getCustomizedException(mixed $asserted, ValidationException $ex
}

$params = $exception->getParams();
$params['name'] = $params['input'] . ', the maximum of ' . stringify($asserted) . ',';
$params['name'] = stringify($params['input']) . ', the maximum of ' . stringify($asserted) . ',';
$exception->updateParams($params);

return $exception;
Expand Down
2 changes: 1 addition & 1 deletion src/Assertor/MinAssertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function getCustomizedException(iterable $asserted, ValidationException
}

$params = $exception->getParams();
$params['name'] = $params['input'] . ', the minimum of ' . stringify($asserted) . ',';
$params['name'] = stringify($params['input']) . ', the minimum of ' . stringify($asserted) . ',';
$exception->updateParams($params);

return $exception;
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/Assertor/AllAssertorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Respect\Validation\Exceptions\AlwaysInvalidException;
use Respect\Validation\Factory;
use Respect\Validation\Rules\AlwaysInvalid;
use stdClass;

use function array_chunk;
use function count;
Expand Down Expand Up @@ -135,6 +136,33 @@ public function itShouldThrowAndNotModifyValidationExceptionsWhenAssertionFailsA
$this->sut->execute($assertion, $input);
}

/**
* @test
*/
public function itShouldThrowAndModifyValidationExceptionsWhenValuesInTheInputAreNonScalar(): void
{
$input = [new stdClass(), []];
$inInput = $input[0];

$exception = Factory::getDefaultInstance()->exception(new AlwaysInvalid(), $inInput);

$assertion = $this->createMock(Assertion::class);
$assertion
->expects($this->once())
->method('assert')
->with(current($input))
->willThrowException($exception);

self::assertEquals('`[object] (stdClass: { })` is always invalid', $exception->getMessage());

$this->expectException(AlwaysInvalidException::class);
$this->expectExceptionMessage(
'`[object] (stdClass: { })`, and all values of `{ [object] (stdClass: { }), { } }`, is always invalid'
);

$this->sut->execute($assertion, $input);
}

protected function setUp(): void
{
$this->sut = new AllAssertor();
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/Assertor/MaxAssertorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use Respect\Validation\Exceptions\AlwaysInvalidException;
use Respect\Validation\Factory;
use Respect\Validation\Rules\AlwaysInvalid;
use stdClass;

use function current;
use function range;

/**
Expand Down Expand Up @@ -149,6 +151,32 @@ public function itShouldThrowAndNotModifyValidationExceptionsWhenAssertionFailsA
$this->sut->execute($assertion, $input);
}

/**
* @test
*/
public function itShouldThrowAndModifyValidationExceptionsWhenValuesInTheInputAreNonScalar(): void
{
$input = [new stdClass(), []];

$exception = Factory::getDefaultInstance()->exception(new AlwaysInvalid(), current($input));

$assertion = $this->createMock(Assertion::class);
$assertion
->expects($this->once())
->method('assert')
->with(current($input))
->willThrowException($exception);

self::assertEquals('`[object] (stdClass: { })` is always invalid', $exception->getMessage());

$this->expectException(AlwaysInvalidException::class);
$this->expectExceptionMessage(
'`[object] (stdClass: { })`, the maximum of `{ [object] (stdClass: { }), { } }`, is always invalid'
);

$this->sut->execute($assertion, $input);
}

protected function setUp(): void
{
$this->sut = new MaxAssertor();
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/Assertor/MinAssertorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use Respect\Validation\Exceptions\AlwaysInvalidException;
use Respect\Validation\Factory;
use Respect\Validation\Rules\AlwaysInvalid;
use stdClass;

use function current;
use function range;

/**
Expand Down Expand Up @@ -149,6 +151,30 @@ public function itShouldThrowAndNotModifyValidationExceptionsWhenAssertionFailsA
$this->sut->execute($assertion, $input);
}

/**
* @test
*/
public function itShouldThrowAndModifyValidationExceptionsWhenValuesInTheInputAreNonScalar(): void
{
$input = [[], new stdClass()];

$exception = Factory::getDefaultInstance()->exception(new AlwaysInvalid(), current($input));

$assertion = $this->createMock(Assertion::class);
$assertion
->expects($this->once())
->method('assert')
->with(current($input))
->willThrowException($exception);

self::assertEquals('`{ }` is always invalid', $exception->getMessage());

$this->expectException(AlwaysInvalidException::class);
$this->expectExceptionMessage('`{ }`, the minimum of `{ { }, [object] (stdClass: { }) }`, is always invalid');

$this->sut->execute($assertion, $input);
}

protected function setUp(): void
{
$this->sut = new MinAssertor();
Expand Down

0 comments on commit 868d59d

Please sign in to comment.