-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from przemyslaw-przylucki/feat/multiple-validat…
…ion-messages [RFC] Multiple validation messages
- Loading branch information
Showing
6 changed files
with
130 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Support; | ||
|
||
final class LanguageHelper | ||
{ | ||
/** | ||
* @param string[] $parts | ||
* | ||
* @return string | ||
*/ | ||
public static function join(array $parts): string | ||
{ | ||
$last = array_pop($parts); | ||
|
||
if ($parts) { | ||
return implode(', ', $parts) . ' ' . 'and' . ' ' . $last; | ||
} | ||
|
||
return $last; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Unit\Validation; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
use Tempest\Validation\Exceptions\ValidationException; | ||
use Tempest\Validation\Rule; | ||
|
||
/** | ||
* @internal | ||
* @small | ||
*/ | ||
final class ValidationExceptionTest extends TestCase | ||
{ | ||
public function test_exception_message(): void | ||
{ | ||
$this->expectException(ValidationException::class); | ||
|
||
$this->expectExceptionMessage('Value should be a valid email address'); | ||
|
||
throw new ValidationException(new stdClass(), [ | ||
'email' => [ | ||
new class () implements Rule { | ||
public function isValid(mixed $value): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function message(): string|array | ||
{ | ||
return 'Value should be a valid email address'; | ||
} | ||
}, | ||
], | ||
]); | ||
} | ||
|
||
public function test_exception_message_with_multiple_messages(): void | ||
{ | ||
$this->expectException(ValidationException::class); | ||
|
||
$this->expectExceptionMessage('Value should be a valid email address'); | ||
$this->expectExceptionMessage("Value should praise tempest, old gods from the past and the new gods from the future"); | ||
|
||
throw new ValidationException(new stdClass(), [ | ||
'email' => [ | ||
new class () implements Rule { | ||
public function isValid(mixed $value): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function message(): string|array | ||
{ | ||
return 'Value should be a valid email address'; | ||
} | ||
}, | ||
new class () implements Rule { | ||
public function isValid(mixed $value): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function message(): string|array | ||
{ | ||
return [ | ||
'Value should praise tempest', | ||
'old gods from the past', | ||
'the new gods from the future', | ||
]; | ||
} | ||
}], | ||
]); | ||
} | ||
} |