Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Speed up category tests.
James Coled872484607c5
diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.phpindex 426609bc1c..037faa8575 100644--- a/app/Http/Controllers/CategoryController.php+++ b/app/Http/Controllers/CategoryController.php@@ -181,19 +181,20 @@ class CategoryController extends Controller$periods = new Collection;$page = (int)$request->get('page');$pageSize = (int)Preferences::get('listPageSize', 50)->data;-+ Log::debug('Start of noCategory()');// prep for "all" view.if ('all' === $moment) {$subTitle = trans('firefly.all_journals_without_category');$first = $this->journalRepos->firstNull();$start = null === $first ? new Carbon : $first->date;$end = new Carbon;+ Log::debug('Moment is all()');}// prep for "specific date" view.if ('all' !== $moment && \strlen($moment) > 0) {/** @var Carbon $start */- $start = app('navigation')->startOfPeriod(new Carbon($moment), $range);+ $start = app('navigation')->startOfPeriod(new Carbon($moment), $range);/** @var Carbon $end */$end = app('navigation')->endOfPeriod($start, $range);$subTitle = trans(@@ -201,6 +202,7 @@ class CategoryController extends Controller['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]);$periods = $this->getNoCategoryPeriodOverview($start);+}// prep for current period@@ -213,6 +215,8 @@ class CategoryController extends Controller['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]);}+ Log::debug(sprintf('Start for noCategory() is %s', $start->format('Y-m-d')));+ Log::debug(sprintf('End for noCategory() is %s', $end->format('Y-m-d')));/** @var JournalCollectorInterface $collector */$collector = app(JournalCollectorInterface::class);@@ -258,7 +262,7 @@ class CategoryController extends Controller// prep for "specific date" view.if ('all' !== $moment && \strlen($moment) > 0) {- $start = app('navigation')->startOfPeriod(new Carbon($moment), $range);+ $start = app('navigation')->startOfPeriod(new Carbon($moment), $range);/** @var Carbon $end */$end = app('navigation')->endOfPeriod($start, $range);$subTitle = trans(@@ -358,11 +362,15 @@ class CategoryController extends Controller*/private function getNoCategoryPeriodOverview(Carbon $theDate): Collection{+ Log::debug(sprintf('Now in getNoCategoryPeriodOverview(%s)', $theDate->format('Y-m-d')));$range = Preferences::get('viewRange', '1M')->data;$first = $this->journalRepos->firstNull();$start = null === $first ? new Carbon : $first->date;$end = $theDate ?? new Carbon;+ Log::debug(sprintf('Start for getNoCategoryPeriodOverview() is %s', $start->format('Y-m-d')));+ Log::debug(sprintf('End for getNoCategoryPeriodOverview() is %s', $end->format('Y-m-d')));+// properties for cache$cache = new CacheProperties;$cache->addProperty($start);@@ -408,7 +416,7 @@ class CategoryController extends Controller$collector->setAllAssetAccounts()->setRange($date['start'], $date['end'])->withoutCategory()->withOpposingAccount()->setTypes([TransactionType::DEPOSIT]);- $earned = $collector->getJournals()->sum('transaction_amount');+ $earned = $collector->getJournals()->sum('transaction_amount');/** @noinspection PhpUndefinedMethodInspection */$dateStr = $date['end']->format('Y-m-d');$dateName = app('navigation')->periodShow($date['end'], $date['period']);@@ -462,8 +470,8 @@ class CategoryController extends Controller$entries = new Collection;foreach ($dates as $currentDate) {- $spent = $this->repository->spentInPeriod(new Collection([$category]), $accounts, $currentDate['start'], $currentDate['end']);- $earned = $this->repository->earnedInPeriod(new Collection([$category]), $accounts, $currentDate['start'], $currentDate['end']);+ $spent = $this->repository->spentInPeriod(new Collection([$category]), $accounts, $currentDate['start'], $currentDate['end']);+ $earned = $this->repository->earnedInPeriod(new Collection([$category]), $accounts, $currentDate['start'], $currentDate['end']);/** @noinspection PhpUndefinedMethodInspection */$dateStr = $currentDate['end']->format('Y-m-d');$dateName = app('navigation')->periodShow($currentDate['end'], $currentDate['period']);diff --git a/app/Support/Navigation.php b/app/Support/Navigation.phpindex c2bfa7bf81..af6392144a 100644--- a/app/Support/Navigation.php+++ b/app/Support/Navigation.php@@ -509,7 +509,7 @@ class Navigation{$date = clone $theDate;// 1D 1W 1M 3M 6M 1Y- Log::debug(sprintf('subtractPeriod: date is %s', $date->format('Y-m-d')));+ Log::debug(sprintf('subtractPeriod: date is %s, repeat frequency is %s and subtract is %d', $date->format('Y-m-d'), $repeatFreq, $subtract));$functionMap = ['1D' => 'subDays','daily' => 'subDays',diff --git a/tests/Feature/Controllers/CategoryControllerTest.php b/tests/Feature/Controllers/CategoryControllerTest.phpindex cbd8229807..3f6122bd90 100644--- a/tests/Feature/Controllers/CategoryControllerTest.php+++ b/tests/Feature/Controllers/CategoryControllerTest.php@@ -60,6 +60,7 @@ class CategoryControllerTest extends TestCase*/public function testCreate(): void{+ Log::debug('TestCreate()');// mock stuff$categoryRepos = $this->mock(CategoryRepositoryInterface::class);$accountRepos = $this->mock(AccountRepositoryInterface::class);@@ -78,6 +79,7 @@ class CategoryControllerTest extends TestCase*/public function testDelete(): void{+ Log::debug('Test Delete()');// mock stuff$categoryRepos = $this->mock(CategoryRepositoryInterface::class);$accountRepos = $this->mock(AccountRepositoryInterface::class);@@ -96,6 +98,7 @@ class CategoryControllerTest extends TestCase*/public function testDestroy(): void{+ Log::debug('Test destroy()');// mock stuff$categoryRepos = $this->mock(CategoryRepositoryInterface::class);$accountRepos = $this->mock(AccountRepositoryInterface::class);@@ -116,6 +119,7 @@ class CategoryControllerTest extends TestCase*/public function testEdit(): void{+ Log::debug('Test edit()');// mock stuff$categoryRepos = $this->mock(CategoryRepositoryInterface::class);$accountRepos = $this->mock(AccountRepositoryInterface::class);@@ -135,6 +139,7 @@ class CategoryControllerTest extends TestCase*/public function testIndex(): void{+ Log::debug('Test index()');// mock stuff$category = factory(Category::class)->make();$categoryRepos = $this->mock(CategoryRepositoryInterface::class);@@ -160,12 +165,17 @@ class CategoryControllerTest extends TestCase*/public function testNoCategory(string $range): void{+ Log::debug(sprintf('Test noCategory(%s)', $range));// mock stuff$collector = $this->mock(JournalCollectorInterface::class);$categoryRepos = $this->mock(CategoryRepositoryInterface::class);$accountRepos = $this->mock(AccountRepositoryInterface::class);$journalRepos = $this->mock(JournalRepositoryInterface::class);- $journalRepos->shouldReceive('firstNull')->twice()->andReturn(TransactionJournal::first());+++ // get the journal with the most recent date for firstNull:+ $journal = $this->user()->transactionJournals()->orderBy('date','DESC')->first();+ $journalRepos->shouldReceive('firstNull')->twice()->andReturn($journal);$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();$collector->shouldReceive('setTypes')->andReturnSelf();@@ -197,6 +207,7 @@ class CategoryControllerTest extends TestCase*/public function testNoCategoryAll(string $range): void{+ Log::debug('Test nocategoryAll()');// mock stuff$collector = $this->mock(JournalCollectorInterface::class);$categoryRepos = $this->mock(CategoryRepositoryInterface::class);@@ -233,6 +244,7 @@ class CategoryControllerTest extends TestCase*/public function testNoCategoryDate(string $range): void{+ Log::debug('Test nocategorydate()');// mock stuff$collector = $this->mock(JournalCollectorInterface::class);$categoryRepos = $this->mock(CategoryRepositoryInterface::class);@@ -277,6 +289,7 @@ class CategoryControllerTest extends TestCase*/public function testShow(string $range): void{+ Log::debug('Test show()');$transaction = factory(Transaction::class)->make();$categoryRepos = $this->mock(CategoryRepositoryInterface::class);$accountRepos = $this->mock(AccountRepositoryInterface::class);@@ -326,6 +339,7 @@ class CategoryControllerTest extends TestCase*/public function testShowAll(string $range): void{+ Log::debug('Test showAll()');// mock stuff$transaction = factory(Transaction::class)->make();$journalRepos = $this->mock(JournalRepositoryInterface::class);@@ -364,6 +378,7 @@ class CategoryControllerTest extends TestCase*/public function testShowByDate(string $range): void{+ Log::debug('Test showbydate()');// mock stuff$transaction = factory(Transaction::class)->make();$repository = $this->mock(CategoryRepositoryInterface::class);@@ -412,6 +427,7 @@ class CategoryControllerTest extends TestCase*/public function testShowEmpty(string $range): void{+ Log::debug('Test showempty()');$journalRepos = $this->mock(JournalRepositoryInterface::class);$journalRepos->shouldReceive('firstNull')->twice()->andReturn(TransactionJournal::first());@@ -451,6 +467,7 @@ class CategoryControllerTest extends TestCase*/public function testStore(): void{+ Log::debug('Test store()');$accountRepos = $this->mock(AccountRepositoryInterface::class);$repository = $this->mock(CategoryRepositoryInterface::class);$journalRepos = $this->mock(JournalRepositoryInterface::class);@@ -475,6 +492,7 @@ class CategoryControllerTest extends TestCase*/public function testUpdate(): void{+ Log::debug('Test update()');$category = Category::first();$repository = $this->mock(CategoryRepositoryInterface::class);$journalRepos = $this->mock(JournalRepositoryInterface::class);diff --git a/tests/Unit/TransactionRules/Triggers/ToAccountIsTest.php b/tests/Unit/TransactionRules/Triggers/ToAccountIsTest.phpindex 6c697f628f..5f06021fd1 100644--- a/tests/Unit/TransactionRules/Triggers/ToAccountIsTest.php+++ b/tests/Unit/TransactionRules/Triggers/ToAccountIsTest.php@@ -25,6 +25,7 @@ namespace Tests\Unit\TransactionRules\Triggers;use FireflyIII\Models\TransactionJournal;use FireflyIII\TransactionRules\Triggers\ToAccountIs;use Tests\TestCase;+use Log;/*** Class ToAccountIsTest@@ -36,14 +37,22 @@ class ToAccountIsTest extends TestCase*/public function testTriggered(): void{- $count = 0;+ $loops = 0; // FINAL LOOP METHOD.do {- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();- $transaction = $journal->transactions()->where('amount', '>', 0)->first();- $transactionCount = $journal->transactions()->count();- $account = null === $transaction ? null : $transaction->account;- $count++;- } while ($account === null && $count < 30 && $transactionCount !== 2);+ /** @var TransactionJournal $journal */+ $journal = $this->user()->transactionJournals()->inRandomOrder()->whereNull('deleted_at')->first();+ $transaction = $journal->transactions()->where('amount', '>', 0)->first();+ $account = $transaction->account;+ $count = $journal->transactions()->count();++ Log::debug(sprintf('Loop: %d, transaction count: %d, account is null: %d', $loops, $count, (int)null===$account));++ $loops++;++ // do this until: account is not null, journal has two transactions, loops is below 30+ } while (!(null !== $account && 2 === $count && $loops < 30));++$trigger = ToAccountIs::makeFromStrings($account->name, false);$result = $trigger->triggered($journal);@@ -55,14 +64,20 @@ class ToAccountIsTest extends TestCase*/public function testTriggeredNot(): void{- $count = 0;+ $loops = 0; // FINAL LOOP METHOD.do {- $journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();- $transaction = $journal->transactions()->where('amount', '>', 0)->first();- $transactionCount = $journal->transactions()->count();- $account = null === $transaction ? null : $transaction->account;- $count++;- } while ($account === null && $count < 30 && $transactionCount !== 2);+ /** @var TransactionJournal $journal */+ $journal = $this->user()->transactionJournals()->inRandomOrder()->whereNull('deleted_at')->first();+ $transaction = $journal->transactions()->where('amount', '>', 0)->first();+ $account = $transaction->account;+ $count = $journal->transactions()->count();++ Log::debug(sprintf('Loop: %d, transaction count: %d, account is null: %d', $loops, $count, (int)null===$account));++ $loops++;++ // do this until: account is not null, journal has two transactions, loops is below 30+ } while (!(null !== $account && 2 === $count && $loops < 30));$trigger = ToAccountIs::makeFromStrings('some name' . random_int(1, 234), false);$result = $trigger->triggered($journal);