<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
arielenter / laravel-phpunit-test-validation-rules example snippets
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\Rules\Password;
Route::patch('/patch', function (Request $request) {
$request->validateWithBag('patch_error_bag',
['accept_field' => ']\.[a-z0-9])*$/i';
$request->validate([
'username_field' => ['.
*
*/
namespace Tests\Feature;
use Arielenter\Validation\Assertions as ValidationAssertions;
use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Password;
class RoutesValidationTest extends TestCase {
use ValidationAssertions;
public function test_single_validation_rule_in_patch_url() {
$this->assertValidationRuleIsImplementedInUrl('/patch',
'accept_field', '', ' 'user_id_field', '101', ['numeric', 'max:100'], 'delete');
// 'numeric|max:100' could also had been used here
}
public function test_all_rules_exhaustively_in_post_url_all_at_once() {
$file = UploadedFile::fake()->image('avatar.jpg');
$tooLong = Str::repeat('x', 21);
$this->assertValidationRulesAreImplementedInUrl(
'/post',
[
['username_field', '', '
/**
* @param array<array> \$list List of arrays where validation rules are
* paired with invalid data examples for them. This nested arrays must have
* the following 3 keys: 0 for Field(s), 1 for Invalid Value Example(s) and
* lastly 2 for the Validation Rule desired to be tested. Key 0 and 1 can
* have multiple field names and invalid value examples respectively by
* nesting them inside an array. Field names must always be string values.
* Composed validation rules can be given either as a pipe | delimited
* string (example ‘numeric|max:100’) or an array (example
* [‘numeric’, ‘max:100’]). Rules can only be string values or instances
* of Illuminate\Contracts\Validation\Rule. Array shape:
* array<array{
* 0: string|array<string>,
* 1: mixed|array<mixed|array<mixed>>,
* 2: string|Rule|array<string|Rule>
* }>
*
*/