重複したユーザーを挿入できないことをテストしようとしています。これを行うには、同じ詳細を持つ 2 つのユーザー オブジェクトを作成します。アカウント名を変更するだけです。これも一意であるためです。
ただし、私のセッターは、会社名を 2 回目に設定していないようです。モデルからエコーすると、設定しようとしているプロパティは、以前に作成したオブジェクトと同じままです。私のテストは、設定したアカウントが既に存在する例外をスローするため失敗します
Failed asserting that exception of type "Models\AccountAlreadyExistsException" matches expected exception "Models\UserAlreadyExistsException".
public function testCantInsertDuplicateUser ()
{
$user = new \Models\User();
$user->first_name = 'John';
$user->surname = 'Smith';
$user->email = 'myemail@gmail.com';
$user->password = 'password';
$user->password_confirmation = 'password';
$user->setCompanyName('Star');
$user->setPackageId(2);
$this->assertTrue($user->signUp());
$user2 = new \Models\User();
$user2->first_name = 'John';
$user2->surname = 'Smith';
$user2->email = 'myemail@gmail.com';
$user2->password = 'password';
$user2->password_confirmation = 'password';
$user2->setCompanyName('cross');
$user2->setPackageId(2);
$this->setExpectedException('Models\UserAlreadyExistsException');
$user2->signUp();
}
//user model
public function setCompanyName ($company_name)
{
$this->company_name = $company_name;
}
private function insertAccount ()
{
$account = new \Models\Account;
$account->setCompanyName($this->company_name);
$account->setPackageId($this->package_id);
$this->account_message_bag = new \Illuminate\Support\MessageBag();
if (!$account->insert()) {
$this->account_message_bag = $account->getValidationErrors();
}
return $account;
}
private function insertUser ()
{
$save_user = $this->save(self::$rules, array(), array(), function ($model)
{
//this is all performed before save
$existing_email = User::where('email', "=", $this->email)->count();
if ($existing_email) {
//delete account that was created in previous step
//as the signup hasn't been successful
$this->account->delete();
throw new UserAlreadyExistsException();
}
//are there any account validation errors?
if (count($this->account_message_bag->getMessages()>0)) {
return false;
}
});
return $save_user;
}
public function signUp ()
{
$this->account = $this->insertAccount();
$this->account_id = $this->account->getId();
if (!$this->insertUser()) {
//delete the company created in previous step
$this->account->delete();
$user_message_bag = Ardent::errors();
//combine user and account validation eerrors
$message_array = array_merge($user_message_bag->getMessages(), $this->account_message_bag->getMessages());
$this->validation_errors = new \Illuminate\Support\MessageBag($message_array);
throw new GenericValidationException();
}
//TODO - does this return false on failure?
$sent = $this->sendConfirmEmail();
if (!$sent) {
throw new WelcomeEmailNotSent();
}
//sende confirm email
return true;
}
//Account model
public function insert ()
{
$result = $this->save(self::$rules, array(), array(), function()
{
$existing_company_name = \Models\Account::where('company_name', '=', $this->company_name)->count();
if ($existing_company_name) {
throw new AccountAlreadyExistsException();
}
});
if (!$result) {
$this->validation_errors = Ardent::errors();
return false;
}
return true;
}