Improve test coverage.

This commit is contained in:
James Cole
2019-07-31 16:53:09 +02:00
parent 5524941c90
commit 9b574ce7ad
155 changed files with 1890 additions and 2263 deletions

View File

@@ -105,60 +105,11 @@ trait AccountServiceTrait
}
}
// /**
// * @param User $user
// * @param string $name
// *
// * @return Account
// * @throws \FireflyIII\Exceptions\FireflyException
// */
// public function storeOpposingAccount(User $user, string $name): Account
// {
// $opposingAccountName = (string)trans('firefly.initial_balance_account', ['name' => $name]);
// Log::debug('Going to create an opening balance opposing account.');
// /** @var AccountFactory $factory */
// $factory = app(AccountFactory::class);
// $factory->setUser($user);
//
// return $factory->findOrCreate($opposingAccountName, AccountType::INITIAL_BALANCE);
// }
// /**
// * @param Account $account
// * @param array $data
// *
// * @return bool
// * @throws \FireflyIII\Exceptions\FireflyException
// */
// public function updateOB(Account $account, array $data): bool
// {
// Log::debug(sprintf('updateIB() for account #%d', $account->id));
//
// $openingBalanceGroup = $this->getOBGroup($account);
//
// // no opening balance journal? create it:
// if (null === $openingBalanceGroup) {
// Log::debug('No opening balance journal yet, create group.');
// $this->storeOBGroup($account, $data);
//
// return true;
// }
//
// // opening balance data? update it!
// if (null !== $openingBalanceGroup->id) {
// Log::debug('Opening balance group found, update group.');
// $this->updateOBGroup($account, $openingBalanceGroup, $data);
//
// return true;
// }
//
// return true; // @codeCoverageIgnore
// }
/**
* @param Account $account
* @param string $note
*
* @codeCoverageIgnore
* @return bool
*/
public function updateNote(Account $account, string $note): bool
@@ -186,117 +137,6 @@ trait AccountServiceTrait
return true;
}
// /**
// * Find existing opening balance.
// *
// * @param Account $account
// *
// * @return TransactionJournal|null
// */
// public function getIBJournal(Account $account): ?TransactionJournal
// {
// $journal = TransactionJournal::leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
// ->where('transactions.account_id', $account->id)
// ->transactionTypes([TransactionType::OPENING_BALANCE])
// ->first(['transaction_journals.*']);
// if (null === $journal) {
// Log::debug('Could not find a opening balance journal, return NULL.');
//
// return null;
// }
// Log::debug(sprintf('Found opening balance: journal #%d.', $journal->id));
//
// return $journal;
// }
// /**
// * @param Account $account
// * @param array $data
// *
// * @return TransactionJournal|null
// * @throws \FireflyIII\Exceptions\FireflyException
// * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
// */
// public function storeIBJournal(Account $account, array $data): ?TransactionJournal
// {
// $amount = (string)$data['openingBalance'];
// Log::debug(sprintf('Submitted amount is %s', $amount));
//
// if (0 === bccomp($amount, '0')) {
// return null;
// }
//
// // store journal, without transactions:
// $name = $data['name'];
// $currencyId = $data['currency_id'];
// $journalData = [
// 'type' => TransactionType::OPENING_BALANCE,
// 'user' => $account->user->id,
// 'transaction_currency_id' => $currencyId,
// 'description' => (string)trans('firefly.initial_balance_description', ['account' => $account->name]),
// 'completed' => true,
// 'date' => $data['openingBalanceDate'],
// 'bill_id' => null,
// 'bill_name' => null,
// 'piggy_bank_id' => null,
// 'piggy_bank_name' => null,
// 'tags' => null,
// 'notes' => null,
// 'transactions' => [],
//
// ];
// /** @var TransactionJournalFactory $factory */
// $factory = app(TransactionJournalFactory::class);
// $factory->setUser($account->user);
// $journal = $factory->create($journalData);
// $opposing = $this->storeOpposingAccount($account->user, $name);
// Log::notice(sprintf('Created new opening balance journal: #%d', $journal->id));
//
// $firstAccount = $account;
// $secondAccount = $opposing;
// $firstAmount = $amount;
// $secondAmount = bcmul($amount, '-1');
// Log::notice(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount));
//
// if (bccomp($amount, '0') === -1) {
// Log::debug(sprintf('%s is a negative number.', $amount));
// $firstAccount = $opposing;
// $secondAccount = $account;
// $firstAmount = bcmul($amount, '-1');
// $secondAmount = $amount;
// Log::notice(sprintf('First amount is %s, second amount is %s', $firstAmount, $secondAmount));
// }
// /** @var TransactionFactory $factory */
// $factory = app(TransactionFactory::class);
// $factory->setUser($account->user);
// $factory->create(
// [
// 'account' => $firstAccount,
// 'transaction_journal' => $journal,
// 'amount' => $firstAmount,
// 'currency_id' => $currencyId,
// 'description' => null,
// 'identifier' => 0,
// 'foreign_amount' => null,
// 'reconciled' => false,
// ]
// );
// $factory->create(
// [
// 'account' => $secondAccount,
// 'transaction_journal' => $journal,
// 'amount' => $secondAmount,
// 'currency_id' => $currencyId,
// 'description' => null,
// 'identifier' => 0,
// 'foreign_amount' => null,
// 'reconciled' => false,
// ]
// );
//
// return $journal;
// }
/**
* Verify if array contains valid data to possibly store or update the opening balance.
*
@@ -340,52 +180,6 @@ trait AccountServiceTrait
return $currency;
}
// /**
// * @param Account $account
// * @param TransactionJournal $journal
// * @param array $data
// *
// * @return bool
// * @SuppressWarnings(PHPMD.CyclomaticComplexity)
// */
// protected function updateIBJournal(Account $account, TransactionJournal $journal, array $data): bool
// {
// $date = $data['openingBalanceDate'];
// $amount = (string)$data['openingBalance'];
// $negativeAmount = bcmul($amount, '-1');
// $currencyId = (int)$data['currency_id'];
// Log::debug(sprintf('Submitted amount for opening balance to update is "%s"', $amount));
// if (0 === bccomp($amount, '0')) {
// Log::notice(sprintf('Amount "%s" is zero, delete opening balance.', $amount));
// /** @var JournalDestroyService $service */
// $service = app(JournalDestroyService::class);
// $service->destroy($journal);
//
// return true;
// }
// $journal->date = $date;
// $journal->transaction_currency_id = $currencyId;
// $journal->save();
// /** @var Transaction $transaction */
// foreach ($journal->transactions()->get() as $transaction) {
// if ((int)$account->id === (int)$transaction->account_id) {
// Log::debug(sprintf('Will (eq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $amount));
// $transaction->amount = $amount;
// $transaction->transaction_currency_id = $currencyId;
// $transaction->save();
// }
// if (!((int)$account->id === (int)$transaction->account_id)) {
// Log::debug(sprintf('Will (neq) change transaction #%d amount from "%s" to "%s"', $transaction->id, $transaction->amount, $negativeAmount));
// $transaction->amount = $negativeAmount;
// $transaction->transaction_currency_id = $currencyId;
// $transaction->save();
// }
// }
// Log::debug('Updated opening balance journal.');
//
// return true;
// }
/**
* Delete TransactionGroup with opening balance in it.
*
@@ -486,7 +280,9 @@ trait AccountServiceTrait
*
* @param Account $account
* @param array $data
*
* @return TransactionGroup|null
* @codeCoverageIgnore
*/
protected function updateOBGroup(Account $account, array $data): ?TransactionGroup
{

View File

@@ -32,7 +32,7 @@ use Log;
/**
* Trait BillServiceTrait
*
* @codeCoverageIgnore
*/
trait BillServiceTrait
{

View File

@@ -57,6 +57,7 @@ trait JournalServiceTrait
* @param string|null $amount
*
* @return string
* @codeCoverageIgnore
*/
protected function getForeignAmount(?string $amount): ?string
{
@@ -87,6 +88,7 @@ trait JournalServiceTrait
* @param string|null $accountName
*
* @return Account
* @codeCoverageIgnore
*/
protected function getAccount(string $transactionType, string $direction, ?int $accountId, ?string $accountName): Account
{
@@ -166,6 +168,7 @@ trait JournalServiceTrait
*
* @return string
* @throws FireflyException
* @codeCoverageIgnore
*/
protected function getAmount(string $amount): string
{
@@ -182,6 +185,8 @@ trait JournalServiceTrait
/**
* @param TransactionJournal $journal
* @param NullArrayObject $data
*
* @codeCoverageIgnore
*/
protected function storeBudget(TransactionJournal $journal, NullArrayObject $data): void
{
@@ -204,6 +209,8 @@ trait JournalServiceTrait
/**
* @param TransactionJournal $journal
* @param NullArrayObject $data
*
* @codeCoverageIgnore
*/
protected function storeCategory(TransactionJournal $journal, NullArrayObject $data): void
{
@@ -221,6 +228,8 @@ trait JournalServiceTrait
/**
* @param TransactionJournal $journal
* @param string $notes
*
* @codeCoverageIgnore
*/
protected function storeNotes(TransactionJournal $journal, ?string $notes): void
{
@@ -255,6 +264,8 @@ trait JournalServiceTrait
* @param TransactionJournal $journal
* @param array $tags
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
* @codeCoverageIgnore
*/
protected function storeTags(TransactionJournal $journal, ?array $tags): void
{
@@ -276,147 +287,4 @@ trait JournalServiceTrait
}
//
// /**
// * Link tags to journal.
// *
// * @param TransactionJournal $journal
// * @param array $data
// * @SuppressWarnings(PHPMD.CyclomaticComplexity)
// */
// public function connectTags(TransactionJournal $journal, array $data): void
// {
// /** @var TagFactory $factory */
// $factory = app(TagFactory::class);
// $factory->setUser($journal->user);
// $set = [];
// if (!is_array($data['tags'])) {
// return; // @codeCoverageIgnore
// }
// foreach ($data['tags'] as $string) {
// if ('' !== $string) {
// $tag = $factory->findOrCreate($string);
// if (null !== $tag) {
// $set[] = $tag->id;
// }
// }
// }
// $journal->tags()->sync($set);
// }
//
//
// /**
// * @param int|null $budgetId
// * @param null|string $budgetName
// *
// * @return Budget|null
// */
// protected function findBudget(?int $budgetId, ?string $budgetName): ?Budget
// {
// /** @var BudgetFactory $factory */
// $factory = app(BudgetFactory::class);
// $factory->setUser($this->user);
//
// return $factory->find($budgetId, $budgetName);
// }
//
// /**
// * @param int|null $categoryId
// * @param null|string $categoryName
// *
// * @return Category|null
// */
// protected function findCategory(?int $categoryId, ?string $categoryName): ?Category
// {
// Log::debug(sprintf('Going to find or create category #%d, with name "%s"', $categoryId, $categoryName));
// /** @var CategoryFactory $factory */
// $factory = app(CategoryFactory::class);
// $factory->setUser($this->user);
//
// return $factory->findOrCreate($categoryId, $categoryName);
// }
//
//
// /**
// * @param TransactionJournal $journal
// * @param Budget|null $budget
// */
// protected function setBudget(TransactionJournal $journal, ?Budget $budget): void
// {
// if (null === $budget) {
// $journal->budgets()->sync([]);
//
// return;
// }
// $journal->budgets()->sync([$budget->id]);
//
// }
//
//
// /**
// * @param TransactionJournal $journal
// * @param Category|null $category
// */
// protected function setCategory(TransactionJournal $journal, ?Category $category): void
// {
// if (null === $category) {
// $journal->categories()->sync([]);
//
// return;
// }
// $journal->categories()->sync([$category->id]);
//
// }
//
//
// /**
// * @param TransactionJournal $journal
// * @param array $data
// * @param string $field
// */
// protected function storeMeta(TransactionJournal $journal, array $data, string $field): void
// {
// $set = [
// 'journal' => $journal,
// 'name' => $field,
// 'data' => (string)($data[$field] ?? ''),
// ];
//
// Log::debug(sprintf('Going to store meta-field "%s", with value "%s".', $set['name'], $set['data']));
//
// /** @var TransactionJournalMetaFactory $factory */
// $factory = app(TransactionJournalMetaFactory::class);
// $factory->updateOrCreate($set);
// }
//
// /**
// * @param TransactionJournal $journal
// * @param string $notes
// */
// protected function storeNote(TransactionJournal $journal, ?string $notes): void
// {
// $notes = (string)$notes;
// if ('' !== $notes) {
// $note = $journal->notes()->first();
// if (null === $note) {
// $note = new Note;
// $note->noteable()->associate($journal);
// }
// $note->text = $notes;
// $note->save();
//
// return;
// }
// $note = $journal->notes()->first();
// if (null !== $note) {
// try {
// $note->delete();
// } catch (Exception $e) {
// Log::debug(sprintf('Journal service trait could not delete note: %s', $e->getMessage()));
// }
// }
//
//
// }
}

View File

@@ -205,6 +205,8 @@ trait RecurringTransactionTrait
/**
* @param Recurrence $recurrence
*
* @codeCoverageIgnore
*/
protected function deleteRepetitions(Recurrence $recurrence): void
{
@@ -213,6 +215,8 @@ trait RecurringTransactionTrait
/**
* @param Recurrence $recurrence
*
* @codeCoverageIgnore
*/
protected function deleteTransactions(Recurrence $recurrence): void
{

View File

@@ -1,171 +0,0 @@
<?php
/**
* TransactionServiceTrait.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\Services\Internal\Support;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AccountFactory;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use Log;
/**
* Trait TransactionServiceTrait
*
*/
trait TransactionServiceTrait
{
// /**
// * @param TransactionJournal $journal
// * @param string $direction
// *
// * @return string|null
// * @SuppressWarnings(PHPMD.CyclomaticComplexity)
// */
// public function accountType(TransactionJournal $journal, string $direction): ?string
// {
// $types = [];
// $type = $journal->transactionType->type;
// if (TransactionType::WITHDRAWAL === $type) {
// $types['source'] = AccountType::ASSET;
// $types['destination'] = AccountType::EXPENSE;
// }
// if (TransactionType::DEPOSIT === $type) {
// $types['source'] = AccountType::REVENUE;
// $types['destination'] = AccountType::ASSET;
// }
// if (TransactionType::TRANSFER === $type) {
// $types['source'] = AccountType::ASSET;
// $types['destination'] = AccountType::ASSET;
// }
// if (TransactionType::RECONCILIATION === $type) {
// $types['source'] = null;
// $types['destination'] = null;
// }
//
// return $types[$direction] ?? null;
// }
// /**
// * @param string|null $expectedType
// * @param Account|null $account
// * @param int|null $accountId
// * @param string|null $accountName
// *
// * @return Account|null
// * @throws FireflyException
// * @SuppressWarnings(PHPMD.CyclomaticComplexity)
// */
// public function findAccount(?string $expectedType, ?Account $account, ?int $accountId, ?string $accountName): ?Account
// {
// $result = null;
//
// if (null !== $account && $account->user_id === $this->user->id) {
// return $account;
// }
//
// $accountId = (int)$accountId;
// $accountName = (string)$accountName;
// $repository = app(AccountRepositoryInterface::class);
// $repository->setUser($this->user);
//
// if (null === $expectedType) {
// return $repository->findNull($accountId);
// }
//
// if ($accountId > 0) {
// // must be able to find it based on ID. Validator should catch invalid ID's.
// return $repository->findNull($accountId);
// }
// if (AccountType::ASSET === $expectedType) {
// return $repository->findByName($accountName, [AccountType::ASSET]);
// }
// // for revenue and expense:
// if ('' !== $accountName) {
// /** @var AccountFactory $factory */
// $factory = app(AccountFactory::class);
// $factory->setUser($this->user);
//
// return $factory->findOrCreate($accountName, $expectedType);
// }
//
// return $repository->getCashAccount();
// }
// /**
// * @param int|null $currencyId
// * @param null|string $currencyCode
// *
// * @return TransactionCurrency|null
// */
// protected function findCurrency(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
// {
// $factory = app(TransactionCurrencyFactory::class);
//
// return $factory->find($currencyId, $currencyCode);
// }
//
// /**
// * @param Transaction $transaction
// * @param string|null $amount
// */
// protected function setForeignAmount(Transaction $transaction, ?string $amount): void
// {
// $amount = '' === (string)$amount ? null : $amount;
// $transaction->foreign_amount = $amount;
// $transaction->save();
// }
//
// /**
// * @param Transaction $transaction
// * @param TransactionCurrency|null $currency
// */
// protected function setForeignCurrency(Transaction $transaction, ?TransactionCurrency $currency): void
// {
// if (null === $currency) {
// $transaction->foreign_currency_id = null;
// $transaction->save();
//
// return;
// }
// // enable currency if not enabled:
// if (false === $currency->enabled) {
// $currency->enabled = true;
// $currency->save();
// }
//
// $transaction->foreign_currency_id = $currency->id;
// $transaction->save();
//
// }
}