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

@@ -57,17 +57,6 @@ class AccountRepository implements AccountRepositoryInterface
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
/**
* @param array $types
*
* @return int
*/
public function count(array $types): int
{
return $this->user->accounts()->accountTypeIn($types)->count();
}
/**
* Moved here from account CRUD.
*
@@ -87,6 +76,17 @@ class AccountRepository implements AccountRepositoryInterface
return true;
}
/**
* @param array $types
*
* @return int
*/
public function count(array $types): int
{
return $this->user->accounts()->accountTypeIn($types)->count();
}
/**
* @param string $number
* @param array $types
@@ -97,7 +97,7 @@ class AccountRepository implements AccountRepositoryInterface
{
$query = $this->user->accounts()
->leftJoin('account_meta', 'account_meta.account_id', '=', 'accounts.id')
->where('account_meta.name', 'accountNumber')
->where('account_meta.name', 'account_number')
->where('account_meta.data', json_encode($number));
if (count($types) > 0) {
@@ -298,30 +298,6 @@ class AccountRepository implements AccountRepositoryInterface
return $factory->findOrCreate('Cash account', $type->type);
}
/**
* @param $account
*
* @return string
*/
public function getInterestPerDay(Account $account): string
{
$interest = $this->getMetaValue($account, 'interest');
$interestPeriod = $this->getMetaValue($account, 'interest_period');
Log::debug(sprintf('Start with interest of %s percent', $interest));
// calculate
if ('monthly' === $interestPeriod) {
$interest = bcdiv(bcmul($interest, '12'), '365'); // per year
Log::debug(sprintf('Interest is now (monthly to daily) %s percent', $interest));
}
if ('yearly' === $interestPeriod) {
$interest = bcdiv($interest, '365'); // per year
Log::debug(sprintf('Interest is now (yearly to daily) %s percent', $interest));
}
return $interest;
}
/**
* Return meta value for account. Null if not found.
*
@@ -444,18 +420,6 @@ class AccountRepository implements AccountRepositoryInterface
return $account;
}
/**
* @param Account $account
*
* @return bool
*/
public function isAsset(Account $account): bool
{
$type = $account->accountType->type;
return AccountType::ASSET === $type || AccountType::DEFAULT === $type;
}
/**
* @param Account $account
*
@@ -466,47 +430,6 @@ class AccountRepository implements AccountRepositoryInterface
return in_array($account->accountType->type, [AccountType::CREDITCARD, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE], true);
}
/**
* Returns the date of the very last transaction in this account.
*
* @param Account $account
*
* @return TransactionJournal|null
*/
public function latestJournal(Account $account): ?TransactionJournal
{
$first = $account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->where('transaction_journals.user_id', $this->user->id)
->orderBy('transaction_journals.id', 'DESC')
->first(['transaction_journals.id']);
if (null !== $first) {
return TransactionJournal::find((int)$first->id);
}
return null;
}
/**
* Returns the date of the very last transaction in this account.
*
* @param Account $account
*
* @return Carbon|null
*/
public function latestJournalDate(Account $account): ?Carbon
{
$result = null;
$journal = $this->latestJournal($account);
if (null !== $journal) {
$result = $journal->date;
}
return $result;
}
/**
* Returns the date of the very first transaction in this account.
*
@@ -581,7 +504,7 @@ class AccountRepository implements AccountRepositoryInterface
* @param array $data
*
* @return Account
* @throws \FireflyIII\Exceptions\FireflyException
* @throws FireflyException
*/
public function store(array $data): Account
{
@@ -597,7 +520,7 @@ class AccountRepository implements AccountRepositoryInterface
* @param array $data
*
* @return Account
* @throws \FireflyIII\Exceptions\FireflyException
* @throws FireflyException
* @throws FireflyException
* @throws FireflyException
*/

View File

@@ -40,7 +40,9 @@ interface AccountRepositoryInterface
/**
* @param Account $account
*
* @return TransactionJournal|null
*
*/
public function getOpeningBalance(Account $account): ?TransactionJournal;
@@ -151,13 +153,6 @@ interface AccountRepositoryInterface
*/
public function getCashAccount(): Account;
/**
* @param $account
*
* @return string
*/
public function getInterestPerDay(Account $account): string;
/**
* Return meta value for account. Null if not found.
*
@@ -211,12 +206,6 @@ interface AccountRepositoryInterface
*/
public function getReconciliation(Account $account): ?Account;
/**
* @param Account $account
*
* @return bool
*/
public function isAsset(Account $account): bool;
/**
* @param Account $account
@@ -225,23 +214,6 @@ interface AccountRepositoryInterface
*/
public function isLiability(Account $account): bool;
/**
* Returns the date of the very first transaction in this account.
*
* @param Account $account
*
* @return TransactionJournal|null
*/
public function latestJournal(Account $account): ?TransactionJournal;
/**
* Returns the date of the very last transaction in this account.
*
* @param Account $account
*
* @return Carbon|null
*/
public function latestJournalDate(Account $account): ?Carbon;
/**
* Returns the date of the very first transaction in this account.

View File

@@ -151,6 +151,47 @@ class AccountTasker implements AccountTaskerInterface
return $expenses;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts): array
{
// get all expenses for the given accounts in the given period!
// also transfers!
// get all transactions:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end);
$collector->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->withAccountInformation();
$income = $this->groupByDestination($collector->getExtractedJournals());
// sort the result
// Obtain a list of columns
$sum = [];
foreach ($income as $accountId => $row) {
$sum[$accountId] = (float)$row['sum'];
}
array_multisort($sum, SORT_DESC, $income);
return $income;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @param array $array
*
@@ -208,45 +249,4 @@ class AccountTasker implements AccountTaskerInterface
return $expenses;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts): array
{
// get all expenses for the given accounts in the given period!
// also transfers!
// get all transactions:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end);
$collector->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
->withAccountInformation();
$income = $this->groupByDestination($collector->getExtractedJournals());
// sort the result
// Obtain a list of columns
$sum = [];
foreach ($income as $accountId => $row) {
$sum[$accountId] = (float)$row['sum'];
}
array_multisort($sum, SORT_DESC, $income);
return $income;
}
/**
* @param User $user
*/
public function setUser(User $user): void
{
$this->user = $user;
}
}

View File

@@ -33,16 +33,16 @@ interface AccountTaskerInterface
{
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function getAccountReport(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
@@ -50,8 +50,8 @@ interface AccountTaskerInterface
public function getExpenseReport(Carbon $start, Carbon $end, Collection $accounts): array;
/**
* @param Carbon $start
* @param Carbon $end
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array

View File

@@ -90,17 +90,6 @@ class AttachmentRepository implements AttachmentRepositoryInterface
return $disk->exists($attachment->fileName());
}
/**
* @param int $attachmentId
*
* @return Attachment|null
*/
public function findWithoutUser(int $attachmentId): ?Attachment
{
return Attachment::find($attachmentId);
}
/**
* @return Collection
*/
@@ -109,24 +98,6 @@ class AttachmentRepository implements AttachmentRepositoryInterface
return $this->user->attachments()->get();
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getBetween(Carbon $start, Carbon $end): Collection
{
$query = $this->user
->attachments()
->leftJoin('transaction_journals', 'attachments.attachable_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->get(['attachments.*']);
return $query;
}
/**
* @param Attachment $attachment
*

View File

@@ -48,26 +48,11 @@ interface AttachmentRepositoryInterface
*/
public function exists(Attachment $attachment): bool;
/**
* @param int $attachmentId
*
* @return Attachment|null
*/
public function findWithoutUser(int $attachmentId): ?Attachment;
/**
* @return Collection
*/
public function get(): Collection;
/**
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getBetween(Carbon $start, Carbon $end): Collection;
/**
* @param Attachment $attachment
*

View File

@@ -489,36 +489,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $return;
}
/**
* Calculate the average amount in the budgets available in this period.
* Grouped by day.
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function getAverageAvailable(Carbon $start, Carbon $end): string
{
/** @var Collection $list */
$list = $this->user->availableBudgets()
->where('start_date', '>=', $start->format('Y-m-d 00:00:00'))
->where('end_date', '<=', $end->format('Y-m-d 00:00:00'))
->get();
if (0 === $list->count()) {
return '0';
}
$total = '0';
$days = 0;
/** @var AvailableBudget $availableBudget */
foreach ($list as $availableBudget) {
$total = bcadd($availableBudget->amount, $total);
$days += $availableBudget->start_date->diffInDays($availableBudget->end_date);
}
return bcdiv($total, (string)$days);
}
/**
* This method is being used to generate the budget overview in the year/multi-year report. Its used
* in both the year/multi-year budget overview AND in the accompanying chart.

View File

@@ -172,17 +172,6 @@ interface BudgetRepositoryInterface
*/
public function getAvailableBudgetsByCurrency(TransactionCurrency $currency): Collection;
/**
* Calculate the average amount in the budgets available in this period.
* Grouped by day.
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function getAverageAvailable(Carbon $start, Carbon $end): string;
/**
* @param Budget $budget
* @param Carbon $start