Codeception issue. Repository not saving data after first test.
I have a UserRepository class that I am running functional tests on. In my storeNewUser method I fire two other methods $this->setUserRole() and $this->setUserMeta().
I have 3 tests, one to ensure the user itself is saved, the second to test if the role is being set, and the 3rd naturally to see if the user meta is being set.
Now for some reason the user is created and the role is set for all 3 tests, however on the 2nd and 3rd tests no user meta is saved. I can't for the life of me figure out why this is happening.
I have the Db module set to populate: true and cleanup: false for both Db and Laravel4 modules.
I don't understand why the roles are all being set properly but not the userMeta. Any ideas?
UserRepository:
/**
* Store a new user and relevant meta data
*
* @param array $input
*
* @see app/start/globals.php For the catch function for the exception.
* @throws FormValidationException
*
* @return User
*/
public function storeNewUser(array $input)
{
$this->validation->validate($input);
$user = $this->user->create([
'username' => $input['company'],
'email' => $input['email'],
'password' => $input['password'],
]);
$this->setUserRole($user, $input['role']);
$this->setUserMeta($user, $input);
return $user;
}
/**
* Set a role on a given user.
*
* @param $user
* @param $role
*
* @return User
*/
public function setUserRole(User $user, $role)
{
$role = Role::where('name', $role)->first();
$user->roles()->sync([$role->id]);
return $user;
}
/**
* @param User $user
* @param array $input
*
* @return User
*/
public function setUserMeta(User $user, array $input)
{
$user->setMeta('address', $input['address'] ?: null);
$user->setMeta('city', $input['city'] ?: null);
$user->setMeta('state', $input['state'] ?: null);
$user->setMeta('postal', $input['postcode'] ?: null);
$user->setMeta('phone', $input['phone'] ?: null);
$user->save();
return $user;
}
UserRepoTest:
class VerifyUserRepositoryTest extends \Codeception\TestCase\Test
{
/**
* @var \IntegrationTester
*/
protected $tester;
protected $repo;
protected function _before()
{
$this->repo = $this->tester->grabService('CRMS\Users\VerifyUserRepository');
}
protected function _after()
{
}
/** @test */
public function it_stores_new_user_in_db()
{
$faker = Faker::create();
$userData = [
'email'=>$faker->email,
'company'=>'NewUserTest',
'password'=>$faker->word,
'address' => $faker->address,
'phone' => $faker->phoneNumber,
'city' => $faker->city,
'state' => $faker->state,
'postcode' => $faker->postcode,
'role' => 'Dealer'
];
$this->repo->storeNewUser($userData);
$this->tester->seeRecord('users',['username'=>$userData['company']]);
}
/**
* @test
* @depends it_stores_new_user_in_db
*/
public function it_sets_a_role_to_a_user()
{
$faker = Faker::create();
$userData = [
'email'=>$faker->email,
'company'=>'UserRoleTest',
'password'=>$faker->word,
'address' => $faker->address,
'phone' => $faker->phoneNumber,
'city' => $faker->city,
'state' => $faker->state,
'postcode' => $faker->postcode,
'role' => 'Dealer'
];
$user = $this->repo->storeNewUser($userData);
$this->tester->seeRecord('role_user', ['user_id' => $user->id]);
}
/**
* @test
* @depends it_stores_new_user_in_db
*/
public function it_sets_user_metadata()
{
$faker = Faker::create();
$userData = [
'email'=>$faker->email,
'company'=> 'UserMetaTest',
'password'=>$faker->word,
'address' => $faker->address,
'phone' => $faker->phoneNumber,
'city' => $faker->city,
'state' => $faker->state,
'postcode' => $faker->postcode,
'role' => 'Dealer'
];
$user = $this->repo->storeNewUser($userData);
$this->tester->canSeeRecord('user_meta', ['user_id'=>$user->id, 'value' => $userData['address']]);
}
Now as I said. The user created by it_stores_new_user_in_db test is created WITH the metadata properly, but both other tests only create the user and set the role, but no meta data. Any suggestions would be appreciated.
Please or to participate in this conversation.