php - Can't use DB Transactions in Controllers in laravel (DB::beginTransaction) -
i have doctorscontroller
has store
method. i'm testing method , want models not insert
data in testing environment. i'm using db::begintransaction
, db::rollback()
, db::commit()
methods in controllers , testing method prevent insert
queries. problem => database having records while tests running. don't want inserts
during tests.
my test code :
public function teststoremethod() { /* * test when validation fails */ $data = include_once('doctorscontrollerformdata.php'); foreach($data $item){ db::begintransaction(); $response = $this->call('post', 'doctors', $item); $this->assertresponsestatus(400, "response's http code not 400 : " . implode('\n',array_flatten($item))); log::debug($response); db::rollback(); } }
my doctorscontroller code snippet : (which calls userscontroller's
store
method)
public function store() { //some code db::begintransaction(); try{ /* * user creation */ $user = new userscontroller(); $user->setresource('doctor'); $userid = $user->store(); //some other code }catch (inputvalidationfailedexception $e){ db::rollback(); return response::make(json_encode(array('error' => $e->geterrors())), \symfony\component\httpfoundation\response::http_bad_request); }catch(exception $e){ log::error('server error @ doctorscontroller'); db::rollback(); app::abort(500, $e->getmessage()); } }
my userscontroller code snippet :
public function store() { $user = new user; try{ $inputs = input::all(); log::info("input data" . implode("\n", array_flatten(input::all()))); /* * user creation */ $v = validator::make( $inputs, user::$rules ); if($v->fails()) { log::error('validation failed! ' . implode("\n", array_flatten($v->messages()->all()))); throw new inputvalidationfailedexception($v); } $user->fname = input::get('fname');//set first name $user->lname = input::get('lname');//set last name $user->email = input::get('email');//set email $user->cell = input::get('cell');//set cell number $user->password = hash::make(input::get('password'));//set password $user->gender_id = input::get('gender') == 'm' ? 1 : 0;//set gender $user->is_active = 0;//set activation status $user->role_id = ($this->resource == 'doctor') ? 1 : 2; $user->activation_time = 0;//set activation time default 0 $user->expiration_time = 0;//set expiration time default 0 //insert user db $user->save(); }catch (inputvalidationfailedexception $e){ log::info('user validation failed! ' . implode("\n", array_flatten($e->geterrors()))); throw $e; }catch (exception $e){ log::error('server error @ userscontroller!'); throw $e; } return $user->id; }
thanks help.
if you're commiting changes in controller, db:rollback
in test can't undo changes because transaction finished there. being said, shouldn't using the same database testing, development. laravel has migrations & seeding can use refresh , reseed dedicated testing database time run tests.
if possible, suggest using in memory database avoid writing persistent database altogether, explained in video linked below:
Comments
Post a Comment