php - Laravel 5: Model->fill() ignores $fillable property in unit tests -
i have user controller following validation rules:
public function store(request $request) { ... $this->validate($request, [ 'name' => 'required', 'email' => 'email|required|unique:users', 'password' => 'confirmed|max:32|min:8|required', 'roles' => 'exists:roles,id|required', ]); $user = new user(); $user->fill($request->all()); ... }
my user.php model defines fillable properties as:
protected $fillable = ['name', 'email'];
to pass confirmed
validation, have send both password
, password_confirmation
fields in post request.
during development works fine, in unit tests i'm getting database error. tries insert data password_confirmation column. it's ignores $fillable
array.
i know "laravel losts event handlers between tests" bug/issue (https://github.com/laravel/framework/issues/1181). think maybe i'm missing call model function aside model::boot()
(i'm calling user::boot()
in test's setup()
function).
thanks,
edit
reading model.php source, i've found calling model::unguard()
https://github.com/laravel/framework/blob/5.1/src/illuminate/database/eloquent/model.php#l2180 after setup() function , before test. if call user::reguard()
@ beggining of test passes, (i don't know why), unguard() , reguard() functions called multiple times , test gets slow.
found problem: base seeder in v5.0.x called model::unguard() (https://github.com/laravel/laravel/blob/v5.0.22/database/seeds/databaseseeder.php#l15) while v5.1.x updated , added call model::reguard() (https://github.com/laravel/laravel/blob/v5.1.0/database/seeds/databaseseeder.php#l19) (i using v5.0.22).
Comments
Post a Comment