From 1adb0f2f0e3185a3914b8c67d4eb09618b4f4be9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 18 Mar 2017 20:53:44 +0100 Subject: [PATCH] Improve test coverage. --- app/Http/Controllers/BudgetController.php | 6 ++- app/Http/Controllers/CategoryController.php | 14 ++++-- app/Http/Controllers/Controller.php | 2 + app/Http/Controllers/TagController.php | 2 +- .../Controllers/AccountControllerTest.php | 17 +++++-- .../Controllers/AttachmentControllerTest.php | 4 +- .../Controllers/BillControllerTest.php | 6 +-- .../Controllers/BudgetControllerTest.php | 40 ++++++++++++--- .../Controllers/CategoryControllerTest.php | 49 +++++++++++++++++-- .../Controllers/CurrencyControllerTest.php | 6 +-- .../Controllers/PiggyBankControllerTest.php | 6 +-- .../Controllers/RuleControllerTest.php | 6 +-- .../Controllers/RuleGroupControllerTest.php | 6 +-- .../Feature/Controllers/TagControllerTest.php | 4 +- .../Transaction/MassControllerTest.php | 4 +- .../Transaction/SingleControllerTest.php | 8 +-- .../Transaction/SplitControllerTest.php | 4 +- .../Controllers/TransactionControllerTest.php | 1 + 18 files changed, 136 insertions(+), 49 deletions(-) diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index 41ad31e2f3..79cbfd9580 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -370,10 +370,11 @@ class BudgetController extends Controller Preferences::mark(); if (intval($request->get('create_another')) === 1) { - // set value so create routine will not overwrite URL: + // @codeCoverageIgnoreStart $request->session()->put('budgets.create.fromStore', true); return redirect(route('budgets.create'))->withInput(); + // @codeCoverageIgnoreEnd } return redirect($this->getPreviousUri('budgets.create.uri')); @@ -394,10 +395,11 @@ class BudgetController extends Controller Preferences::mark(); if (intval($request->get('return_to_edit')) === 1) { - // set value so edit routine will not overwrite URL: + // @codeCoverageIgnoreStart $request->session()->put('budgets.edit.fromUpdate', true); return redirect(route('budgets.edit', [$budget->id]))->withInput(['return_to_edit' => 1]); + // @codeCoverageIgnoreEnd } return redirect($this->getPreviousUri('budgets.edit.uri')); diff --git a/app/Http/Controllers/CategoryController.php b/app/Http/Controllers/CategoryController.php index 2eedbf128c..4fba001c4b 100644 --- a/app/Http/Controllers/CategoryController.php +++ b/app/Http/Controllers/CategoryController.php @@ -265,8 +265,8 @@ class CategoryController extends Controller $end = Navigation::endOfPeriod($start, $range); $subTitle = trans( 'firefly.journals_in_period_for_category', - ['name' => $category->name, - 'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] + ['name' => $category->name, + 'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] ); $periods = $this->getPeriodOverview($category); } @@ -278,7 +278,8 @@ class CategoryController extends Controller $periods = $this->getPeriodOverview($category); $subTitle = trans( 'firefly.journals_in_period_for_category', - ['name' => $category->name,'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] + ['name' => $category->name, 'start' => $start->formatLocalized($this->monthAndDayFormat), + 'end' => $end->formatLocalized($this->monthAndDayFormat)] ); } // grab journals, but be prepared to jump a period back to get the right ones: @@ -304,7 +305,8 @@ class CategoryController extends Controller if ($moment != 'all' && $loop > 1) { $subTitle = trans( 'firefly.journals_in_period_for_category', - ['name' => $category->name,'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] + ['name' => $category->name, 'start' => $start->formatLocalized($this->monthAndDayFormat), + 'end' => $end->formatLocalized($this->monthAndDayFormat)] ); } @@ -327,9 +329,11 @@ class CategoryController extends Controller Preferences::mark(); if (intval($request->get('create_another')) === 1) { + // @codeCoverageIgnoreStart $request->session()->put('categories.create.fromStore', true); return redirect(route('categories.create'))->withInput(); + // @codeCoverageIgnoreEnd } return redirect(route('categories.index')); @@ -352,9 +356,11 @@ class CategoryController extends Controller Preferences::mark(); if (intval($request->get('return_to_edit')) === 1) { + // @codeCoverageIgnoreStart $request->session()->put('categories.edit.fromUpdate', true); return redirect(route('categories.edit', [$category->id])); + // @codeCoverageIgnoreEnd } return redirect($this->getPreviousUri('categories.edit.uri')); diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 9773032f9b..203628861f 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -120,9 +120,11 @@ class Controller extends BaseController } } + // @codeCoverageIgnoreStart Session::flash('error', strval(trans('firefly.cannot_redirect_to_account'))); return redirect(route('index')); + // @codeCoverageIgnoreEnd } /** diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index 67615e167a..813183d1cb 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -291,7 +291,7 @@ class TagController extends Controller Log::info(sprintf('Count is still zero, go back in time to "%s" and "%s"!', $start->format('Y-m-d'), $end->format('Y-m-d'))); } } - + if ($moment != 'all' && $loop > 1) { $subTitle = trans( 'firefly.journals_in_period_for_tag', diff --git a/tests/Feature/Controllers/AccountControllerTest.php b/tests/Feature/Controllers/AccountControllerTest.php index e866173605..e5956cb305 100644 --- a/tests/Feature/Controllers/AccountControllerTest.php +++ b/tests/Feature/Controllers/AccountControllerTest.php @@ -53,6 +53,7 @@ class AccountControllerTest extends TestCase /** * @covers \FireflyIII\Http\Controllers\AccountController::delete + * @covers \FireflyIII\Http\Controllers\Controller::rememberPreviousUri */ public function testDelete() { @@ -73,6 +74,8 @@ class AccountControllerTest extends TestCase /** * @covers \FireflyIII\Http\Controllers\AccountController::destroy + * @covers \FireflyIII\Http\Controllers\Controller::__construct + * @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri */ public function testDestroy() { @@ -83,7 +86,7 @@ class AccountControllerTest extends TestCase $repository->shouldReceive('destroy')->andReturn(true); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); - $this->session(['accounts.delete.url' => 'http://localhost/accounts/show/1']); + $this->session(['accounts.delete.uri' => 'http://localhost/accounts/show/1']); $account = $this->user()->accounts()->where('account_type_id', 3)->whereNull('deleted_at')->first(); $this->be($this->user()); @@ -316,6 +319,7 @@ class AccountControllerTest extends TestCase /** * @covers \FireflyIII\Http\Controllers\AccountController::store + * @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri */ public function testStore() { @@ -326,7 +330,7 @@ class AccountControllerTest extends TestCase $repository->shouldReceive('store')->once()->andReturn(factory(Account::class)->make()); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); - $this->session(['accounts.create.url' => 'http://localhost']); + $this->session(['accounts.create.uri' => 'http://localhost']); $this->be($this->user()); $data = [ 'name' => 'new account ' . rand(1000, 9999), @@ -340,6 +344,7 @@ class AccountControllerTest extends TestCase /** * @covers \FireflyIII\Http\Controllers\AccountController::store + * @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri */ public function testStoreAnother() { @@ -350,7 +355,7 @@ class AccountControllerTest extends TestCase $repository->shouldReceive('store')->once()->andReturn(factory(Account::class)->make()); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); - $this->session(['accounts.create.url' => 'http://localhost']); + $this->session(['accounts.create.uri' => 'http://localhost']); $this->be($this->user()); $data = [ 'name' => 'new account ' . rand(1000, 9999), @@ -365,6 +370,7 @@ class AccountControllerTest extends TestCase /** * @covers \FireflyIII\Http\Controllers\AccountController::update + * @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri */ public function testUpdate() { @@ -375,7 +381,7 @@ class AccountControllerTest extends TestCase $repository->shouldReceive('update')->once(); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); - $this->session(['accounts.edit.url' => 'http://localhost']); + $this->session(['accounts.edit.uri' => 'http://localhost/javascript/account']); $this->be($this->user()); $data = [ 'name' => 'updated account ' . rand(1000, 9999), @@ -390,6 +396,7 @@ class AccountControllerTest extends TestCase /** * @covers \FireflyIII\Http\Controllers\AccountController::update + * @covers \FireflyIII\Http\Controllers\Controller::getPreviousUri */ public function testUpdateAgain() { @@ -400,7 +407,7 @@ class AccountControllerTest extends TestCase $repository->shouldReceive('update')->once(); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); - $this->session(['accounts.edit.url' => 'http://localhost']); + $this->session(['accounts.edit.uri' => 'http://localhost']); $this->be($this->user()); $data = [ 'name' => 'updated account ' . rand(1000, 9999), diff --git a/tests/Feature/Controllers/AttachmentControllerTest.php b/tests/Feature/Controllers/AttachmentControllerTest.php index 1dda189fe5..271767907a 100644 --- a/tests/Feature/Controllers/AttachmentControllerTest.php +++ b/tests/Feature/Controllers/AttachmentControllerTest.php @@ -45,7 +45,7 @@ class AttachmentControllerTest extends TestCase $repository->shouldReceive('destroy')->andReturn(true); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); - $this->session(['attachments.delete.url' => 'http://localhost']); + $this->session(['attachments.delete.uri' => 'http://localhost']); $this->be($this->user()); $response = $this->post(route('attachments.destroy', [1])); $response->assertStatus(302); @@ -127,7 +127,7 @@ class AttachmentControllerTest extends TestCase $repository->shouldReceive('update')->once(); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); - $this->session(['attachments.edit.url' => 'http://localhost']); + $this->session(['attachments.edit.uri' => 'http://localhost']); $data = [ 'title' => 'Some updated title ' . rand(1000, 9999), 'notes' => '', diff --git a/tests/Feature/Controllers/BillControllerTest.php b/tests/Feature/Controllers/BillControllerTest.php index 1ed607ed76..123ae4301e 100644 --- a/tests/Feature/Controllers/BillControllerTest.php +++ b/tests/Feature/Controllers/BillControllerTest.php @@ -67,7 +67,7 @@ class BillControllerTest extends TestCase $repository->shouldReceive('destroy')->andReturn(true); $journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal); - $this->session(['bills.delete.url' => 'http://localhost']); + $this->session(['bills.delete.uri' => 'http://localhost']); $this->be($this->user()); $response = $this->post(route('bills.destroy', [1])); $response->assertStatus(302); @@ -199,7 +199,7 @@ class BillControllerTest extends TestCase 'date' => '2016-01-01', 'repeat_freq' => 'monthly', ]; - $this->session(['bills.create.url' => 'http://localhost']); + $this->session(['bills.create.uri' => 'http://localhost']); $this->be($this->user()); $response = $this->post(route('bills.store'), $data); $response->assertStatus(302); @@ -228,7 +228,7 @@ class BillControllerTest extends TestCase 'date' => '2016-01-01', 'repeat_freq' => 'monthly', ]; - $this->session(['bills.edit.url' => 'http://localhost']); + $this->session(['bills.edit.uri' => 'http://localhost']); $this->be($this->user()); $response = $this->post(route('bills.update', [1]), $data); $response->assertStatus(302); diff --git a/tests/Feature/Controllers/BudgetControllerTest.php b/tests/Feature/Controllers/BudgetControllerTest.php index 5149bea555..b79a537f54 100644 --- a/tests/Feature/Controllers/BudgetControllerTest.php +++ b/tests/Feature/Controllers/BudgetControllerTest.php @@ -103,7 +103,7 @@ class BudgetControllerTest extends TestCase $repository->shouldReceive('destroy')->andReturn(true); - $this->session(['budgets.delete.url' => 'http://localhost']); + $this->session(['budgets.delete.uri' => 'http://localhost']); $this->be($this->user()); $response = $this->post(route('budgets.destroy', [1])); $response->assertStatus(302); @@ -137,6 +137,13 @@ class BudgetControllerTest extends TestCase public function testIndex(string $range) { // mock stuff + $budget = factory(Budget::class)->make(); + $budgetLimit = factory(BudgetLimit::class)->make(); + + // set budget limit to current month: + $budgetLimit->start_date = Carbon::now()->startOfMonth(); + $budgetLimit->end_date = Carbon::now()->endOfMonth(); + $accountRepos = $this->mock(AccountRepositoryInterface::class); $repository = $this->mock(BudgetRepositoryInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); @@ -144,9 +151,11 @@ class BudgetControllerTest extends TestCase $accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection); $repository->shouldReceive('cleanupBudgets'); - $repository->shouldReceive('getActiveBudgets')->andReturn(new Collection); + $repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget])); $repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection); $repository->shouldReceive('getAvailableBudget')->andReturn('100.123'); + $repository->shouldReceive('spentInPeriod')->andReturn('-1'); + $repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit])); $this->be($this->user()); @@ -292,6 +301,9 @@ class BudgetControllerTest extends TestCase public function testShow(string $range) { // mock stuff + + $budgetLimit = factory(BudgetLimit::class)->make(); + $journalRepos = $this->mock(JournalRepositoryInterface::class); $journalRepos->shouldReceive('first')->andReturn(new TransactionJournal); @@ -308,8 +320,8 @@ class BudgetControllerTest extends TestCase $accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection); $repository = $this->mock(BudgetRepositoryInterface::class); - $repository->shouldReceive('getBudgetLimits')->andReturn(new Collection); - $repository->shouldReceive('spentInPeriod')->andReturn('1'); + $repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit])); + $repository->shouldReceive('spentInPeriod')->andReturn('-1'); $date = new Carbon(); $date->subDay(); @@ -322,6 +334,22 @@ class BudgetControllerTest extends TestCase $response->assertSee('