Expand test cases for transaction creation through the API.

This commit is contained in:
James Cole
2018-02-18 19:55:35 +01:00
parent 77aced6734
commit 0b61c16eb0
4 changed files with 689 additions and 11 deletions

View File

@@ -0,0 +1,95 @@
<?php
/**
* AccountFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\User;
/**
* Class AccountFactory
*/
class AccountFactory
{
/** @var User */
private $user;
/**
* TagFactory constructor.
*/
public function __construct()
{
}
/**
* @param array $data
*
* @return Account
*/
public function create(array $data): Account
{
return Account::create($data);
}
/**
* @param string $accountName
* @param string $accountType
*
* @return Account
*/
public function findOrCreate(string $accountName, string $accountType): Account
{
$type = AccountType::whereType($accountType)->first();
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']);
/** @var Account $object */
foreach ($accounts as $object) {
if ($object->name === $accountName) {
return $object;
}
}
$newAccount = $this->create(
[
'user_id' => $this->user->id,
'name' => $accountName,
'account_type_id' => $type->id,
'virtual_balance' => '0',
'iban' => null,
'active' => true,
]
);
return $newAccount;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
}

View File

@@ -241,8 +241,10 @@ class TransactionFactory
return $this->accountRepository->findNull($accountId);
}
if (strlen($accountName) > 0) {
// alternatively, return by name. Validator should catch invalid names.
return $this->accountRepository->findByName($accountName, [AccountType::EXPENSE]);
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
return $factory->findOrCreate($accountName, AccountType::EXPENSE);
}
// return cash account:
@@ -254,8 +256,11 @@ class TransactionFactory
return $this->accountRepository->findNull($accountId);
}
if (strlen($accountName) > 0) {
// alternatively, return by name. Validator should catch invalid names.
return $this->accountRepository->findByName($accountName, [AccountType::REVENUE]);
// alternatively, return by name.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user);
return $factory->findOrCreate($accountName, AccountType::REVENUE);
}
// return cash account: