Expand test coverage and improve transaction management code.

This commit is contained in:
James Cole
2019-07-01 20:22:35 +02:00
parent 94acb50a6f
commit 5bbe1eab7c
63 changed files with 1251 additions and 812 deletions

View File

@@ -30,7 +30,6 @@ use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
@@ -81,10 +80,7 @@ trait PeriodOverview
protected function getAccountPeriodOverview(Account $account, Carbon $start, Carbon $end): array
{
$range = app('preferences')->get('viewRange', '1M')->data;
if ($end < $start) {
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
}
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
// properties for cache
$cache = new CacheProperties;
@@ -159,10 +155,7 @@ trait PeriodOverview
protected function getCategoryPeriodOverview(Category $category, Carbon $start, Carbon $end): array
{
$range = app('preferences')->get('viewRange', '1M')->data;
if ($end < $start) {
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
}
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
// properties for entries with their amounts.
$cache = new CacheProperties();
@@ -173,7 +166,7 @@ trait PeriodOverview
$cache->addProperty($category->id);
if ($cache->has()) {
//return $cache->get(); // @codeCoverageIgnore
return $cache->get(); // @codeCoverageIgnore
}
/** @var array $dates */
$dates = app('navigation')->blockPeriods($start, $end, $range);
@@ -240,9 +233,7 @@ trait PeriodOverview
{
$range = app('preferences')->get('viewRange', '1M')->data;
if ($end < $start) {
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
}
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
$cache = new CacheProperties;
$cache->addProperty($start);
@@ -250,7 +241,7 @@ trait PeriodOverview
$cache->addProperty('no-budget-period-entries');
if ($cache->has()) {
//return $cache->get(); // @codeCoverageIgnore
return $cache->get(); // @codeCoverageIgnore
}
/** @var array $dates */
@@ -284,6 +275,8 @@ trait PeriodOverview
}
/**
* TODO fix date.
*
* Show period overview for no category view.
*
* @param Carbon $theDate
@@ -309,7 +302,7 @@ trait PeriodOverview
$cache->addProperty('no-category-period-entries');
if ($cache->has()) {
//return $cache->get(); // @codeCoverageIgnore
return $cache->get(); // @codeCoverageIgnore
}
$dates = app('navigation')->blockPeriods($start, $end, $range);
@@ -379,11 +372,7 @@ trait PeriodOverview
/** @var Carbon $end */
$start = clone $date;
$end = $repository->firstUseDate($tag) ?? new Carbon;
if ($end < $start) {
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
}
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
// properties for entries with their amounts.
$cache = new CacheProperties;
@@ -431,63 +420,60 @@ trait PeriodOverview
* @param string $transactionType
* @param Carbon $endDate
*
* @return Collection
* @return array
*/
protected function getTransactionPeriodOverview(string $transactionType, Carbon $endDate): Collection
protected function getTransactionPeriodOverview(string $transactionType, Carbon $start, Carbon $end): array
{
die('not yet complete');
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
$range = app('preferences')->get('viewRange', '1M')->data;
$endJournal = $repository->firstNull();
$end = null === $endJournal ? new Carbon : $endJournal->date;
$start = clone $endDate;
$types = config('firefly.transactionTypesByType.' . $transactionType);
$range = app('preferences')->get('viewRange', '1M')->data;
$types = config(sprintf('firefly.transactionTypesByType.%s', $transactionType));
[$start, $end] = $end < $start ? [$end, $start] : [$start, $end];
if ($end < $start) {
[$start, $end] = [$end, $start]; // @codeCoverageIgnore
// properties for cache
$cache = new CacheProperties;
$cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('transactions-period-entries');
$cache->addProperty($transactionType);
if ($cache->has()) {
// return $cache->get(); // @codeCoverageIgnore
}
/** @var array $dates */
$dates = app('navigation')->blockPeriods($start, $end, $range);
$entries = new Collection;
$entries = [];
// collect all journals in this period (regardless of type)
$collector = app(GroupCollectorInterface::class);
$collector->setTypes($types)->setRange($start, $end);
$genericSet = $collector->getExtractedJournals();
foreach ($dates as $currentDate) {
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector->setTypes($types)->setRange($currentDate['start'], $currentDate['end']);
$journals = $collector->getExtractedJournals();
$amounts = $this->getJournalsSum($journals);
$spent = [];
$earned = [];
$transferred = [];
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
// set to correct array
if ('expenses' === $transactionType || 'withdrawal' === $transactionType) {
$spent = $amounts;
$spent = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
}
if ('revenue' === $transactionType || 'deposit' === $transactionType) {
$earned = $amounts;
$earned = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
}
if ('transfer' === $transactionType || 'transfers' === $transactionType) {
$transferred = $amounts;
$transferred = $this->filterJournalsByDate($genericSet, $currentDate['start'], $currentDate['end']);
}
$title = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);
$entries->push(
$entries[] =
[
'transactions' => $amounts['count'],
'title' => $title,
'spent' => $spent,
'earned' => $earned,
'transferred' => $transferred,
'route' => route(
'transactions.index', [$transactionType, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]
),
]
);
'title' => $title,
'route' =>
route('transactions.index', [$transactionType, $currentDate['start']->format('Y-m-d'), $currentDate['end']->format('Y-m-d')]),
'total_transactions' => count($spent) + count($earned) + count($transferred),
'spent' => $this->groupByCurrency($spent),
'earned' => $this->groupByCurrency($earned),
'transferred' => $this->groupByCurrency($transferred),
];
}
return $entries;

View File

@@ -178,7 +178,7 @@ trait RequestInformation
// both must be array and either must be > 0
if (count($intro) > 0 || count($specialIntro) > 0) {
$shownDemo = app('preferences')->get($key, false)->data;
Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %s', $key, var_export($shownDemo, true)));
//Log::debug(sprintf('Check if user has already seen intro with key "%s". Result is %s', $key, var_export($shownDemo, true)));
}
if (!is_bool($shownDemo)) {
$shownDemo = true;

View File

@@ -50,7 +50,7 @@ trait UserNavigation
*/
protected function getPreviousUri(string $identifier): string
{
Log::debug(sprintf('Trying to retrieve URL stored under "%s"', $identifier));
// Log::debug(sprintf('Trying to retrieve URL stored under "%s"', $identifier));
// "forbidden" words for specific identifiers:
// if these are in the previous URI, don't refer back there.
$array = [
@@ -67,24 +67,24 @@ trait UserNavigation
'transactions.mass-delete.uri' => '/transactions/show/',
];
$forbidden = $array[$identifier] ?? '/show/';
Log::debug(sprintf('The forbidden word for %s is "%s"', $identifier, $forbidden));
//Log::debug(sprintf('The forbidden word for %s is "%s"', $identifier, $forbidden));
$uri = (string)session($identifier);
Log::debug(sprintf('The URI is %s', $uri));
//Log::debug(sprintf('The URI is %s', $uri));
if (
!(false === strpos($identifier, 'delete'))
&& !(false === strpos($uri, $forbidden))) {
$uri = $this->redirectUri;
Log::debug(sprintf('URI is now %s (identifier contains "delete")', $uri));
//Log::debug(sprintf('URI is now %s (identifier contains "delete")', $uri));
}
if (!(false === strpos($uri, 'jscript'))) {
$uri = $this->redirectUri; // @codeCoverageIgnore
Log::debug(sprintf('URI is now %s (uri contains jscript)', $uri));
//Log::debug(sprintf('URI is now %s (uri contains jscript)', $uri));
}
// more debug notes:
Log::debug(sprintf('strpos($identifier, "delete"): %s', var_export(strpos($identifier, 'delete'), true)));
Log::debug(sprintf('strpos($uri, $forbidden): %s', var_export(strpos($uri, $forbidden), true)));
//Log::debug(sprintf('strpos($identifier, "delete"): %s', var_export(strpos($identifier, 'delete'), true)));
//Log::debug(sprintf('strpos($uri, $forbidden): %s', var_export(strpos($uri, $forbidden), true)));
return $uri;
}
@@ -154,10 +154,10 @@ trait UserNavigation
if (null === $errors || (null !== $errors && 0 === $errors->count())) {
$url = app('url')->previous();
session()->put($identifier, $url);
Log::debug(sprintf('Will put previous URI in cache under key %s: %s', $identifier, $url));
//Log::debug(sprintf('Will put previous URI in cache under key %s: %s', $identifier, $url));
return;
}
Log::debug(sprintf('The users session contains errors somehow so we will not remember the URI!: %s', var_export($errors, true)));
//Log::debug(sprintf('The users session contains errors somehow so we will not remember the URI!: %s', var_export($errors, true)));
}
}