mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Expand test cases for transaction creation through the API.
This commit is contained in:
95
app/Factory/AccountFactory.php
Normal file
95
app/Factory/AccountFactory.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@@ -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:
|
||||
|
Reference in New Issue
Block a user