jasonmccreary / laravel-test-assertions

A set of helpful assertions when testing Laravel applications.
321 stars 34 forks source link

Closure Based Rules Doesn't Pass Unit Validation Test #29

Open JeffreyDavidson opened 3 years ago

JeffreyDavidson commented 3 years ago

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',
julianphp commented 1 year ago

+1