laravel-test-assertions icon indicating copy to clipboard operation
laravel-test-assertions copied to clipboard

Closure Based Rules Doesn't Pass Unit Validation Test

Open JeffreyDavidson opened this issue 4 years ago • 1 comments
trafficstars

Currently I have a test that doesn't pass because of the Rule class having a closure attached to the rule. I'm seeing this happen with Rule::requiredIf but could also be seeing this using the new Rule::when() but that's just newly introduced to the Laravel framework. Has anyone come across this unexpected situation?

'started_at' => [
    Rule::requiredIf(function () {
        return  ! $this->route('user')->isUnactivated();
    }),
]

Running the following test gives me the following result.

/**
   * @test
    */
   public function rules_returns_validation_requirements()
   {
       $subject = $this->createFormRequest(UpdateRequest::class);

       $rules = $subject->rules();

       $this->assertValidationRules([
           'started_at' => ['nullable', 'string', 'date'],
       ], $rules);

       $this->assertValidationRuleContains($rules['started_at'], RequiredIf::class);
   }
array (
       0 => 'nullable',
  -    1 => 'string',
  -    2 => 'date',
  +    1 => 
  +    Illuminate\Validation\Rules\RequiredIf::__set_state(array(
  +       'condition' => 
  +      Closure::__set_state(array(
  +      )),
  +    )),
  +    2 => 'string',
       3 => 'date',

JeffreyDavidson avatar Sep 07 '21 14:09 JeffreyDavidson

+1

julianphp avatar Mar 21 '23 07:03 julianphp