123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780 |
- <?php
- namespace Knuckles\Scribe\Tests\Unit;
- use Illuminate\Foundation\Application;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Translation\Translator;
- use Illuminate\Validation\Rule;
- use Illuminate\Validation\ValidationException;
- use Knuckles\Scribe\Extracting\ParsesValidationRules;
- use Knuckles\Scribe\Tests\BaseLaravelTest;
- use Knuckles\Scribe\Tools\DocumentationConfig;
- use Knuckles\Scribe\Tests\Fixtures;
- $invokableRulesSupported = interface_exists(\Illuminate\Contracts\Validation\InvokableRule::class);
- $laravel10Rules = version_compare(Application::VERSION, '10.0', '>=');
- class ValidationRuleParsingTest extends BaseLaravelTest
- {
- private $strategy;
- public function __construct(?string $name = null, array $data = [], $dataName = '')
- {
- parent::__construct($name, $data, $dataName);
- $this->strategy = new class {
- use ParsesValidationRules;
- public function parse($validationRules, $customParameterData = []): array
- {
- $this->config = new DocumentationConfig([]);
- $bodyParametersFromValidationRules = $this->getParametersFromValidationRules($validationRules, $customParameterData);
- return $this->normaliseArrayAndObjectParameters($bodyParametersFromValidationRules);
- }
- };
- }
- /**
- * @test
- * @dataProvider supportedRules
- */
- public function can_parse_supported_rules(array $ruleset, array $customInfo, array $expected)
- {
- // Needed for `exists` rule
- Schema::create('users', function ($table) {
- $table->id();
- });
-
- $results = $this->strategy->parse($ruleset, $customInfo);
- $parameterName = array_keys($ruleset)[0];
- $this->assertEquals($expected['description'], $results[$parameterName]['description']);
- if (isset($expected['type'])) {
- $this->assertEquals($expected['type'], $results[$parameterName]['type']);
- }
- // Validate that the generated values actually pass validation (for rules where we can generate some data)
- if (is_string($ruleset[$parameterName]) && str_contains($ruleset[$parameterName], "exists")) return;
-
- $exampleData = [$parameterName => $results[$parameterName]['example']];
- $validator = Validator::make($exampleData, $ruleset);
- try {
- $validator->validate();
- } catch (ValidationException $e) {
- dump('Rules: ', $ruleset);
- dump('Generated value: ', $exampleData[$parameterName]);
- dump($e->errors());
- $this->fail("Generated example data from validation rule failed to match actual.");
- }
- }
- /** @test */
- public function can_parse_rule_objects()
- {
- $results = $this->strategy->parse([
- 'in_param' => ['numeric', Rule::in([3,5,6])]
- ]);
- $this->assertEquals(
- [3, 5, 6],
- $results['in_param']['enumValues']
- );
- }
- /** @test */
- public function can_transform_arrays_and_objects()
- {
- $ruleset = [
- 'array_param' => 'array|required',
- 'array_param.*' => 'string',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertCount(1, $results);
- $this->assertEquals('string[]', $results['array_param']['type']);
- $ruleset = [
- 'object_param' => 'array|required',
- 'object_param.field1.*' => 'string',
- 'object_param.field2' => 'integer|required',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertCount(3, $results);
- $this->assertEquals('object', $results['object_param']['type']);
- $this->assertEquals('string[]', $results['object_param.field1']['type']);
- $this->assertEquals('integer', $results['object_param.field2']['type']);
- $ruleset = [
- 'array_of_objects_with_array.*.another.*.one.field1.*' => 'string|required',
- 'array_of_objects_with_array.*.another.*.one.field2' => 'integer',
- 'array_of_objects_with_array.*.another.*.two.field2' => 'numeric',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertCount(7, $results);
- $this->assertEquals('object[]', $results['array_of_objects_with_array']['type']);
- $this->assertEquals('object[]', $results['array_of_objects_with_array[].another']['type']);
- $this->assertEquals('object', $results['array_of_objects_with_array[].another[].one']['type']);
- $this->assertEquals('object', $results['array_of_objects_with_array[].another[].two']['type']);
- $this->assertEquals('string[]', $results['array_of_objects_with_array[].another[].one.field1']['type']);
- $this->assertEquals('integer', $results['array_of_objects_with_array[].another[].one.field2']['type']);
- $this->assertEquals('number', $results['array_of_objects_with_array[].another[].two.field2']['type']);
- $ruleset = [
- '*.foo' => 'required|array',
- '*.foo.*' => 'required|array',
- '*.foo.*.bar' => 'required',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertCount(3, $results);
- $this->assertEquals('object', $results['*']['type']);
- $this->assertEquals('object[]', $results['*.foo']['type']);
- $this->assertEquals('string', $results['*.foo[].bar']['type']);
- }
- public static function supportedRules()
- {
- $description = 'A description';
- // Key is just an identifier
- // First array in each key is the validation ruleset,
- // Second is custom information (from bodyParameters() or comments)
- // Third is expected result
- yield 'string' => [
- ['string_param' => 'string'],
- ['string_param' => ['description' => $description]],
- [
- 'type' => 'string',
- 'description' => $description . ".",
- ],
- ];
- yield 'boolean' => [
- ['boolean_param' => 'boolean'],
- [],
- [
- 'type' => 'boolean',
- 'description' => "",
- ],
- ];
- yield 'integer' => [
- ['integer_param' => 'integer'],
- [],
- [
- 'type' => 'integer',
- 'description' => "",
- ],
- ];
- yield 'numeric' => [
- ['numeric_param' => 'numeric'],
- ['numeric_param' => ['description' => $description]],
- [
- 'type' => 'number',
- 'description' => $description . ".",
- ],
- ];
- yield 'file' => [
- ['file_param' => 'file|required'],
- ['file_param' => ['description' => $description]],
- [
- 'description' => "$description. Must be a file.",
- 'type' => 'file',
- ],
- ];
- yield 'image' => [
- ['image_param' => 'image|required'],
- [],
- [
- 'description' => "Must be an image.",
- 'type' => 'file',
- ],
- ];
- yield 'timezone' => [
- ['timezone_param' => 'timezone|required'],
- [],
- [
- 'description' => 'Must be a valid time zone, such as <code>Africa/Accra</code>.',
- 'type' => 'string',
- ],
- ];
- yield 'email' => [
- ['email_param' => 'email|required'],
- [],
- [
- 'description' => 'Must be a valid email address.',
- 'type' => 'string',
- ],
- ];
- yield 'url' => [
- ['url_param' => 'url|required'],
- ['url_param' => ['description' => $description]],
- [
- 'description' => "$description. Must be a valid URL.",
- 'type' => 'string',
- ],
- ];
- yield 'ip' => [
- ['ip_param' => 'ip|required'],
- ['ip_param' => ['description' => $description]],
- [
- 'description' => "$description. Must be a valid IP address.",
- 'type' => 'string',
- ],
- ];
- yield 'json' => [
- ['json_param' => 'json|required'],
- ['json_param' => []],
- [
- 'description' => 'Must be a valid JSON string.',
- 'type' => 'string',
- ],
- ];
- yield 'date' => [
- ['date_param' => 'date|required'],
- [],
- [
- 'description' => 'Must be a valid date.',
- 'type' => 'string',
- ],
- ];
- yield 'date_format' => [
- ['date_format_param' => 'date_format:Y-m-d|required'],
- ['date_format_param' => ['description' => $description]],
- [
- 'description' => "$description. Must be a valid date in the format <code>Y-m-d</code>.",
- 'type' => 'string',
- ],
- ];
- yield 'in' => [
- ['in_param' => 'in:3,5,6'],
- ['in_param' => ['description' => $description]],
- [
- 'description' => $description.".",
- 'type' => 'string',
- 'enumValues' => [3,5,6]
- ],
- ];
- yield 'not_in' => [
- ['not_param' => 'not_in:3,5,6'],
- [],
- [
- 'description' => "Must not be one of <code>3</code>, <code>5</code>, or <code>6</code>.",
- ],
- ];
- yield 'digits' => [
- ['digits_param' => 'digits:8'],
- [],
- [
- 'description' => "Must be 8 digits.",
- 'type' => 'string',
- ],
- ];
- yield 'digits_between' => [
- ['digits_between_param' => 'digits_between:2,8'],
- [],
- [
- 'description' => "Must be between 2 and 8 digits.",
- 'type' => 'string',
- ],
- ];
- yield 'alpha' => [
- ['alpha_param' => 'alpha'],
- [],
- [
- 'description' => "Must contain only letters.",
- 'type' => 'string',
- ],
- ];
- yield 'alpha_dash' => [
- ['alpha_dash_param' => 'alpha_dash'],
- [],
- [
- 'description' => "Must contain only letters, numbers, dashes and underscores.",
- 'type' => 'string',
- ],
- ];
- yield 'alpha_num' => [
- ['alpha_num_param' => 'alpha_num'],
- [],
- [
- 'description' => "Must contain only letters and numbers.",
- 'type' => 'string',
- ],
- ];
- yield 'ends_with' => [
- ['ends_with_param' => 'ends_with:go,ha'],
- [],
- [
- 'description' => "Must end with one of <code>go</code> or <code>ha</code>.",
- 'type' => 'string',
- ],
- ];
- yield 'starts_with' => [
- ['starts_with_param' => 'starts_with:go,ha'],
- [],
- [
- 'description' => "Must start with one of <code>go</code> or <code>ha</code>.",
- 'type' => 'string',
- ],
- ];
- yield 'uuid' => [
- ['uuid_param' => 'uuid'],
- [],
- [
- 'description' => "Must be a valid UUID.",
- 'type' => 'string',
- ],
- ];
- yield 'required_if' => [
- ['required_if_param' => 'required_if:another_field,a_value'],
- [],
- ['description' => "This field is required when <code>another_field</code> is <code>a_value</code>."],
- ];
- yield 'required_unless' => [
- ['required_unless_param' => 'string|required_unless:another_field,a_value'],
- [],
- ['description' => "This field is required unless <code>another_field</code> is in <code>a_value</code>."],
- ];
- yield 'required_with' => [
- ['required_with_param' => 'required_with:another_field,some_other_field'],
- [],
- ['description' => 'This field is required when <code>another_field</code> or <code>some_other_field</code> is present.'],
- ];
- yield 'required_with_all' => [
- ['required_with_all_param' => 'required_with_all:another_field,some_other_field'],
- [],
- ['description' => 'This field is required when <code>another_field</code> and <code>some_other_field</code> are present.'],
- ];
- yield 'required_without' => [
- ['required_without_param' => 'string|required_without:another_field,some_other_field'],
- [],
- ['description' => 'This field is required when <code>another_field</code> or <code>some_other_field</code> is not present.'],
- ];
- yield 'required_without_all' => [
- ['required_without_all_param' => 'string|required_without_all:another_field,some_other_field'],
- [],
- ['description' => 'This field is required when none of <code>another_field</code> and <code>some_other_field</code> are present.'],
- ];
- yield 'same' => [
- ['same_param' => 'same:other_field'],
- [],
- ['description' => "The value and <code>other_field</code> must match."],
- ];
- yield 'different' => [
- ['different_param' => 'string|different:other_field'],
- [],
- ['description' => "The value and <code>other_field</code> must be different."],
- ];
- yield 'after' => [
- ['after_param' => 'after:2020-02-12'],
- [],
- ['description' => "Must be a date after <code>2020-02-12</code>."],
- ];
- yield 'before_or_equal' => [
- ['before_or_equal_param' => 'before_or_equal:2020-02-12'],
- [],
- ['description' => "Must be a date before or equal to <code>2020-02-12</code>."],
- ];
- yield 'size (number)' => [
- ['size_param' => 'numeric|size:6'],
- [],
- ['description' => "Must be 6."],
- ];
- yield 'size (string)' => [
- ['size_param' => 'string|size:6'],
- [],
- ['description' => "Must be 6 characters."],
- ];
- yield 'size (file)' => [
- ['size_param' => 'file|size:6'],
- [],
- ['description' => "Must be a file. Must be 6 kilobytes."],
- ];
- yield 'max (number)' => [
- ['max_param' => 'numeric|max:6'],
- [],
- ['description' => "Must not be greater than 6."],
- ];
- yield 'max (string)' => [
- ['max_param' => 'string|max:6'],
- [],
- ['description' => "Must not be greater than 6 characters."],
- ];
- yield 'max (file)' => [
- ['max_param' => 'file|max:6'],
- [],
- ['description' => "Must be a file. Must not be greater than 6 kilobytes."],
- ];
- yield 'min (number)' => [
- ['min_param' => 'numeric|min:6'],
- [],
- ['description' => "Must be at least 6."],
- ];
- yield 'min (string)' => [
- ['min_param' => 'string|min:6'],
- [],
- ['description' => "Must be at least 6 characters."],
- ];
- yield 'min (file)' => [
- ['min_param' => 'file|min:6'],
- [],
- ['description' => "Must be a file. Must be at least 6 kilobytes."],
- ];
- yield 'between (number)' => [
- ['between_param' => 'numeric|between:1,2'],
- [],
- ['description' => "Must be between 1 and 2."],
- ];
- yield 'between (string)' => [
- ['between_param' => 'string|between:1,2'],
- [],
- ['description' => "Must be between 1 and 2 characters."],
- ];
- yield 'between (file)' => [
- ['between_param' => 'file|between:1,2'],
- [],
- ['description' => "Must be a file. Must be between 1 and 2 kilobytes."],
- ];
- yield 'regex' => [
- ['regex_param' => 'regex:/\d/'],
- [],
- ['description' => 'Must match the regex /\d/.'],
- ];
- yield 'accepted' => [
- ['accepted_param' => 'accepted'],
- [],
- [
- 'type' => 'boolean',
- 'description' => 'Must be accepted.',
- ],
- ];
- yield 'exists' => [
- ['exists_param' => 'exists:users,id'],
- [],
- [
- 'description' => 'The <code>id</code> of an existing record in the users table.',
- ],
- ];
- yield 'unsupported' => [
- ['unsupported_param' => [new DummyValidationRule, 'bail']],
- ['unsupported_param' => ['description' => $description]],
- ['description' => "$description."],
- ];
- if (version_compare(Application::VERSION, '8.53', '>=')) {
- yield 'accepted_if' => [
- ['accepted_if_param' => 'accepted_if:another_field,a_value'],
- [],
- [
- 'type' => 'boolean',
- 'description' => "Must be accepted when <code>another_field</code> is <code>a_value</code>.",
- ],
- ];
- }
- }
- /** @test */
- public function child_does_not_overwrite_parent_status()
- {
- $ruleset = [
- 'array_param' => 'array|required',
- 'array_param.*' => 'array|required',
- 'array_param.*.an_item' => 'string|required',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertCount(2, $results);
- $this->assertEquals(true, $results['array_param']['required']);
- }
- /** @test */
- public function can_parse_custom_closure_rules()
- {
- // Single line DocComment
- $ruleset = [
- 'closure' => [
- 'bail', 'required',
- /** This is a single line parsed closure rule. */
- function ($attribute, $value, $fail) {
- $fail('Always fail.');
- },
- ],
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals(
- 'This is a single line parsed closure rule.',
- $results['closure']['description']
- );
- // Block DocComment
- $ruleset = [
- 'closure' => [
- 'bail', 'required',
- /**
- * This is a block DocComment
- * parsed on a closure rule.
- * Extra info.
- */
- function ($attribute, $value, $fail) {
- $fail('Always fail.');
- },
- ],
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals(
- 'This is a block DocComment parsed on a closure rule. Extra info.',
- $results['closure']['description']
- );
- }
- /** @test */
- public function can_parse_custom_rule_classes()
- {
- $ruleset = [
- 'param1' => ['bail', 'required', new DummyWithDocsValidationRule],
- ];
- global $invokableRulesSupported;
- if ($invokableRulesSupported) {
- $ruleset['param2'] = [new DummyInvokableValidationRule];
- }
- global $laravel10Rules;
- if ($laravel10Rules) {
- $ruleset['param3'] = [new DummyL10ValidationRule];
- }
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals(true, $results['param1']['required']);
- $this->assertEquals('This is a dummy test rule.', $results['param1']['description']);
- if (isset($results['param2'])) $this->assertEquals('This rule is invokable.', $results['param2']['description']);
- if (isset($results['param3'])) $this->assertEquals('This is a custom rule.', $results['param3']['description']);
- }
- /** @test */
- public function can_parse_enum_rules()
- {
- if (phpversion() < 8.1) {
- $this->markTestSkipped('Enums are only supported in PHP 8.1 or later');
- }
- $results = $this->strategy->parse([
- 'enum' => [
- 'required',
- new \Illuminate\Validation\Rules\Enum(Fixtures\TestStringBackedEnum::class),
- // Not supported in Laravel 8
- // Rule::enum(Fixtures\TestStringBackedEnum::class)
- ],
- ]);
- $this->assertEquals('string', $results['enum']['type']);
- $this->assertEquals(
- ['red', 'green', 'blue'],
- $results['enum']['enumValues']
- );
- $this->assertTrue(in_array(
- $results['enum']['example'],
- array_map(fn ($case) => $case->value, Fixtures\TestStringBackedEnum::cases())
- ));
- $results = $this->strategy->parse([
- 'enum' => [
- 'required',
- new \Illuminate\Validation\Rules\Enum(Fixtures\TestIntegerBackedEnum::class),
- // Not supported in Laravel 8
- // Rule::enum(Fixtures\TestIntegerBackedEnum::class)
- ],
- ]);
- $this->assertEquals('integer', $results['enum']['type']);
- $this->assertEquals(
- [1, 2, 3],
- $results['enum']['enumValues']
- );
- $this->assertTrue(in_array(
- $results['enum']['example'],
- array_map(fn ($case) => $case->value, Fixtures\TestIntegerBackedEnum::cases())
- ));
- $results = $this->strategy->parse([
- 'enum' => [
- 'required',
- new \Illuminate\Validation\Rules\Enum(Fixtures\TestStringBackedEnum::class),
- // Not supported in Laravel 8
- // Rule::enum(Fixtures\TestStringBackedEnum::class),
- ],
- ], [
- 'enum' => ['description' => 'A description'],
- ]);
- $this->assertEquals('string', $results['enum']['type']);
- $this->assertEquals(
- 'A description.',
- $results['enum']['description']
- );
- $this->assertTrue(in_array(
- $results['enum']['example'],
- array_map(fn ($case) => $case->value, Fixtures\TestStringBackedEnum::cases())
- ));
- }
- /** @test */
- public function can_translate_validation_rules_with_types_with_translator_without_array_support()
- {
- // Single line DocComment
- $ruleset = [
- 'nested' => [
- 'string', 'max:20',
- ],
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals('Must not be greater than 20 characters.', $results['nested']['description']);
- $this->app->extend('translator', function ($command, $app) {
- $loader = $app['translation.loader'];
- $locale = $app['config']['app.locale'];
- return new DummyTranslator($loader, $locale);
- });
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals('successfully translated by concatenated string.', $results['nested']['description']);
- }
- /** @test */
- public function can_valid_parse_nullable_rules()
- {
- $ruleset = [
- 'nullable_param' => 'nullable|string',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals(true, $results['nullable_param']['nullable']);
- $ruleset = [
- 'nullable_param' => 'string',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals(false, $results['nullable_param']['nullable']);
- $ruleset = [
- 'required_param' => 'required|nullable|string',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals(false, $results['required_param']['nullable']);
- $ruleset = [
- 'array_param' => 'array',
- 'array_param.*.field' => 'nullable|string',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals(false, $results['array_param']['nullable']);
- $this->assertEquals(true, $results['array_param[].field']['nullable']);
- $ruleset = [
- 'object' => 'array',
- 'object.field1' => 'string',
- 'object.field2' => 'nullable|string',
- ];
- $results = $this->strategy->parse($ruleset);
- $this->assertEquals(false, $results['object']['nullable']);
- $this->assertEquals(false, $results['object.field1']['nullable']);
- $this->assertEquals(true, $results['object.field2']['nullable']);
- }
- }
- class DummyValidationRule implements \Illuminate\Contracts\Validation\Rule
- {
- public function passes($attribute, $value)
- {
- return true;
- }
- public function message()
- {
- return '.';
- }
- }
- class DummyWithDocsValidationRule implements \Illuminate\Contracts\Validation\Rule
- {
- public function passes($attribute, $value)
- {
- return true;
- }
- public function message()
- {
- return '.';
- }
- public static function docs()
- {
- return [
- 'description' => 'This is a dummy test rule.',
- 'example' => 'Default example, only added if none other give.',
- ];
- }
- }
- if ($invokableRulesSupported) {
- class DummyInvokableValidationRule implements \Illuminate\Contracts\Validation\InvokableRule
- {
- public function __invoke($attribute, $value, $fail)
- {
- if (strtoupper($value) !== $value) {
- $fail(':attribute must be uppercase.');
- }
- }
- public function docs()
- {
- return [
- 'description' => 'This rule is invokable.',
- ];
- }
- }
- }
- if ($laravel10Rules) {
- // Laravel 10 deprecated the previous Rule and InvokableRule classes for a single interface
- // (https://github.com/laravel/framework/pull/45954)
- class DummyL10ValidationRule implements \Illuminate\Contracts\Validation\ValidationRule
- {
- public function validate(string $attribute, mixed $value, \Closure $fail): void
- {
- if (strtoupper($value) !== $value) {
- $fail('The :attribute must be an attribute.');
- }
- }
- public static function docs()
- {
- return [
- 'description' => 'This is a custom rule.',
- ];
- }
- }
- }
- class DummyTranslator extends Translator
- {
- public function get($key, array $replace = [], $locale = null, $fallback = true)
- {
- if ($key === 'validation.max.string') {
- return 'successfully translated by concatenated string';
- }
- return $key;
- }
- }
|