public function test_should_lock_optimistically() { $Account1 = new BankAccount(array('balance' => 2000)); $this->assertEqual($Account1->lock_version, 1, 'Version attribute initially starts at 1.'); $Account1->save(); $this->assertEqual($Account1->lock_version, 1, 'We are now on Version 1.'); $Account2 = new BankAccount($Account1->getId()); $this->assertEqual($Account2->lock_version, 1, 'We reloaded Version 1.'); $Account1->balance = 5; $Account1->save(); $this->assertEqual($Account1->lock_version, 2, 'We are now on Version 2.'); $Account2->balance = 3000000; $this->assertUpcomingError('Attempted to update a stale object'); $this->assertFalse($Account2->save(), 'We cant save because version number is wrong.'); $Account1->balance = 1000; $this->assertTrue($Account1->save()); $Account3 = new BankAccount($Account1->getId()); $this->assertEqual($Account3->balance, 1000); $this->assertEqual($Account3->lock_version, 3, 'We are now on Version 3.'); }
public function actionAddBankAccount() { Yii::import('application.extensions.vendor.autoload', true); Httpful\Bootstrap::init(); Balanced\Bootstrap::init(); Balanced\Settings::$api_key = Yii::app()->params['balancedAPISecret']; $outcome = array('success' => 0); $transaction = Yii::app()->db->beginTransaction(); try { if (isset($_POST['data']) && isset($_POST['merchantData'])) { $data = json_decode($_POST['data']); $merchantData = json_decode($_POST['merchantData']); $uri = $data->uri; $user = $this->getUser(); if ($user->bankAccount != null) { $bankAccount = $user->bankAccount; $balancedBankAccount = Balanced\BankAccount::get($bankAccount->URI); $balancedBankAccount->delete(); $bankAccount->Active = 0; $bankAccount->save(); } $account = new BankAccount(); $account->User_ID = $user->User_ID; $account->URI = $uri; if ($account->save()) { $balancedAccount = $this->getUserAccount(); $balancedAccount->addBankAccount($uri); if (!in_array('merchant', $balancedAccount->roles)) { $balancedAccount->promoteToMerchant($merchantData); } $outcome = array('success' => 1, 'BankAccount_ID' => $account->BankAccount_ID); } else { $outcome = array('success' => 0, 'Errors' => $account->getErrors()); } } $transaction->commit(); } catch (Balanced\Errors\IdentityVerificationFailed $error) { $transaction->rollback(); /* could not identify this account. */ $outcome = array('success' => 0, 'Errors' => "redirect merchant to:" . $error->redirect_uri . "\n"); } catch (Balanced\Errors\HTTPError $error) { $transaction->rollback(); /* TODO: handle 400 and 409 exceptions as required */ $outcome = array('success' => 0, 'Errors' => print_r($error, true)); } catch (Exception $error) { $transaction->rollback(); $outcome = array('success' => 0, 'Errors' => print_r($error, true)); } echo CJSON::encode($outcome); }