Fixed some tests.

This commit is contained in:
James Cole 2015-05-08 07:27:29 +02:00
parent cc01592085
commit cefbbcd1df
4 changed files with 153 additions and 6 deletions

View File

@ -60,6 +60,31 @@ class Account extends Model
}
/**
* @param array $fields
*
* @return Account|null
*/
public static function firstOrNullEncrypted(array $fields)
{
// everything but the name:
$query = Account::orderBy('id');
foreach ($fields as $name => $value) {
if ($name != 'name') {
$query->where($name, $value);
}
}
$set = $query->get(['accounts.*']);
/** @var Account $account */
foreach ($set as $account) {
if ($account->name == $fields['name']) {
return $account;
}
}
return null;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/

View File

@ -443,11 +443,15 @@ class AccountRepository implements AccountRepositoryInterface
if (!$newAccount->isValid()) {
// does the account already exist?
$existingAccount = Account::where('user_id', $data['user'])->where('account_type_id', $accountType->id)->where('name', $data['name'])->first();
$searchData = [
'user_id' => $data['user'],
'account_type_id' => $accountType->id,
'name' => $data['name']
];
$existingAccount = Account::firstOrNullEncrypted($searchData);
if (!$existingAccount) {
Log::error('Account create error: ' . $newAccount->getErrors()->toJson());
App::abort(500);
}
$newAccount = $existingAccount;
}

View File

@ -61,7 +61,9 @@ FactoryMuffin::define(
'account_type_id' => 'factory|FireflyIII\Models\AccountType',
'name' => 'word',
'active' => 'boolean',
'encrypted' => 'boolean',
'encrypted' => function () {
return true;
},
'virtual_balance' => 0
]
);

View File

@ -606,6 +606,9 @@ class AccountRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Account\AccountRepository::store
* @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount
* @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata
* @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance
*/
public function testStore()
{
@ -638,14 +641,127 @@ class AccountRepositoryTest extends TestCase
}
/**
* @covers FireflyIII\Repositories\Account\AccountRepository::store
* @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount
* @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata
* @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance
*/
public function testStoreWithNegativeInitialBalance()
{
$user = FactoryMuffin::create('FireflyIII\User');
FactoryMuffin::create('FireflyIII\Models\AccountType');
FactoryMuffin::create('FireflyIII\Models\AccountType');
FactoryMuffin::create('FireflyIII\Models\AccountType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
$this->be($user);
$data = [
'accountType' => 'expense',
'user' => $user->id,
'name' => 'Test account #' . rand(1, 100),
'active' => true,
'accountRole' => 'testAccount',
'openingBalance' => -100,
'virtualBalance' => 0,
'openingBalanceCurrency' => $currency->id,
'openingBalanceDate' => '2015-01-01',
];
$account = $this->object->store($data);
$this->assertEquals($data['name'], $account->name);
}
/**
* @covers FireflyIII\Repositories\Account\AccountRepository::store
* @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount
* @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata
* @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance
*/
public function testStoreWithExistingAccount()
{
$account = FactoryMuffin::create('FireflyIII\Models\Account');
FactoryMuffin::create('FireflyIII\Models\AccountType');
FactoryMuffin::create('FireflyIII\Models\AccountType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
$this->be($account->user);
$data = [
'accountType' => 'expense',
'user' => $account->user->id,
'name' => $account->name,
'active' => $account->active,
'accountRole' => 'testAccount',
'openingBalance' => 0,
'virtualBalance' => 0,
'openingBalanceCurrency' => $currency->id,
'openingBalanceDate' => '2015-01-01',
];
$newAccount = $this->object->store($data);
$this->assertEquals($account->name, $newAccount->name);
$this->assertEquals($account->id, $newAccount->id);
}
/**
* @covers FireflyIII\Repositories\Account\AccountRepository::store
* @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount
* @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata
* @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance
*/
public function testStoreWithInvalidAccountData()
{
$account = FactoryMuffin::create('FireflyIII\Models\Account');
FactoryMuffin::create('FireflyIII\Models\AccountType');
FactoryMuffin::create('FireflyIII\Models\AccountType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
$this->be($account->user);
$data = [
'accountType' => 'expense',
'user' => $account->user->id,
'name' => $account->name,
'active' => $account->active,
'accountRole' => 'testAccount',
'openingBalance' => 0,
'virtualBalance' => 0,
'openingBalanceCurrency' => 12,
'openingBalanceDate' => '2015-01-01',
];
$newAccount = $this->object->store($data);
$this->assertEquals($account->name, $newAccount->name);
$this->assertEquals($account->id, $newAccount->id);
}
/**
* @covers FireflyIII\Repositories\Account\AccountRepository::sumOfEverything
* @todo Implement testSumOfEverything().
*/
public function testSumOfEverything()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete('This test has not been implemented yet.');
$user = FactoryMuffin::create('FireflyIII\User');
$this->be($user);
$this->assertEquals(0, $this->object->sumOfEverything());
}
/**