unit testing - Laravel 5/Faker - Factory data changes -


been using faker in combination sqlite in-memory database testing in laravel lately , have strange issue model has factory , except first variable (which happens primary key of table) gets set correctly.

let's explain further, variable i'm pointing @ uses following rule in factory:

'kvk' => strval($faker->randomnumber(9)), 

so should become string containing 9 digit number. next step calling factory controller test , have user model uses 'kvk' variable company foreign key reference:

$this->_company = factory(company::class)->create([ 'address_id' => $this->_address->id ]);  $this->_user = factory(user::class)->create([ 'kvk' => $this->_company->kvk ]);  

but when put echo $this->_company->kvk; in between, shows me 'kvk set 1, should not possible because of rule put in factory.

finally user used mock session in test , used check wether should have rights edit address using following check:

$user = auth::user();  $company = company::where('kvk', $user->kvk)->first();  $address = address::whereid($id)->first();  if($company->address_id != $address->id) {      return abort(403);  }  

so first current logged in user (which mocked created user above, , works perfectly. next company associated user (which should 1 created above, since used company->kvk foreign key reference), when output saved company log see kvk set 9 digit string it's supposed to.

i can't put finger on why @ first kvk set 1 , afterwards in test seems fine way should be, test fails because wrong reference set in user can't find right company. have idea reason this? i've tried setting 'kvk' explicitly while creating factory, not work , stil output 1.

after diving source of laravel, found out had models. while did set primary key attribute in models, forgot set auto increment boolean false. caused models cast primary key auto increment integer every time saved database. solved issue.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -