vigneshc91 / laravel-test-generator

Auto generate laravel unit test file based on the routes and form request files
https://vigneshc91.github.io/laravel-test-generator/
MIT License
21 stars 22 forks source link

TestGenerator will fail when using Class based rules #2

Open tarreislam opened 4 years ago

tarreislam commented 4 years ago

Take this validation request as an example:

    public function rules()
    {
        return [
            'organisation_id' => ['required', Rule::in(Organisation::pluck('id'))],
            'name' => ['required'],
            'function' => ['required'],
            'email' => ['nullable', 'mail'],
            'phone1' => ['required', new PhoneNumberRule],
            'phone2' => ['nullable', new PhoneNumberRule]
        ];
    }

Rule::in will cause preg_match() expects parameter 2 to be string, object given at

    protected function isDateFormat($rules)
    {
        $format = array_filter($rules, function($val){
            return preg_match('/^date_format/', $val); // $val = Class
        });
        return count($format);
    }

A potential fix would be to change This piece of code to something like

        foreach ($this->params as $key => $val) {
            $rules = $this->rules[$key];

            $rules = array_map(function ($rule) {
                if (is_object($rule)) {
                    return mb_strtolower(class_basename($rule));
                }
                return $rule;
            }, $rules);

            $case[$val] = $this->getValue($val, $rules);
        }