From ad1c61d959c61b6f0462d105389bb5a2d41eb9b3 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 4 Jun 2015 21:35:36 +0200 Subject: [PATCH] Improved test coverage. --- .../Controllers/Chart/AccountController.php | 6 +- app/Http/Controllers/Chart/BillController.php | 4 +- .../Controllers/Chart/BudgetController.php | 8 +- .../Controllers/Chart/CategoryController.php | 2 +- app/Http/Controllers/HomeController.php | 75 ------------ app/Http/Controllers/JsonController.php | 8 +- app/Http/Controllers/NewUserController.php | 2 +- app/Http/Controllers/ReportController.php | 11 +- app/Http/routes.php | 2 - app/Models/TransactionJournal.php | 2 +- .../Account/AccountRepository.php | 6 +- app/Support/Amount.php | 2 +- app/Support/Steam.php | 2 +- app/Support/Twig/Journal.php | 2 +- tests/controllers/HomeControllerTest.php | 28 +++++ tests/controllers/NewUserControllerTest.php | 108 ++++++++++++++++++ 16 files changed, 159 insertions(+), 109 deletions(-) create mode 100644 tests/controllers/NewUserControllerTest.php diff --git a/app/Http/Controllers/Chart/AccountController.php b/app/Http/Controllers/Chart/AccountController.php index af8aef6b57..9681fd7621 100644 --- a/app/Http/Controllers/Chart/AccountController.php +++ b/app/Http/Controllers/Chart/AccountController.php @@ -46,7 +46,7 @@ class AccountController extends Controller $cache->addProperty('all'); $cache->addProperty('accounts'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } @@ -116,7 +116,7 @@ class AccountController extends Controller $cache->addProperty('frontpage'); $cache->addProperty('accounts'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } @@ -176,7 +176,7 @@ class AccountController extends Controller $cache->addProperty('single'); $cache->addProperty($account->id); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } while ($end >= $current) { diff --git a/app/Http/Controllers/Chart/BillController.php b/app/Http/Controllers/Chart/BillController.php index a1b8985ee5..7a4c2670bb 100644 --- a/app/Http/Controllers/Chart/BillController.php +++ b/app/Http/Controllers/Chart/BillController.php @@ -44,7 +44,7 @@ class BillController extends Controller $cache->addProperty('bill'); $cache->addProperty($bill->id); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } // get first transaction or today for start: @@ -88,7 +88,7 @@ class BillController extends Controller $cache->addProperty('bills'); $cache->addProperty('frontpage'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } $bills = $repository->getActiveBills(); diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index dbd569d682..abcd2ce2a0 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -49,7 +49,7 @@ class BudgetController extends Controller $cache->addProperty('budget'); $cache->addProperty('budget'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } @@ -95,7 +95,7 @@ class BudgetController extends Controller $cache->addProperty($budget->id); $cache->addProperty($repetition->id); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } $chart->addColumn(trans('firefly.day'), 'date'); @@ -149,7 +149,7 @@ class BudgetController extends Controller $cache->addProperty('budget'); $cache->addProperty('all'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } @@ -219,7 +219,7 @@ class BudgetController extends Controller $cache->addProperty('budget'); $cache->addProperty('year'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } // add columns: diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index fed7840398..b7c4bdb800 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -84,7 +84,7 @@ class CategoryController extends Controller $cache->addProperty('category'); $cache->addProperty('frontpage'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } $set = $repository->getCategoriesAndExpensesCorrected($start, $end); diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 16a9e120b4..81e4dacc43 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -99,79 +99,4 @@ class HomeController extends Controller return view('index', compact('count', 'title', 'savings', 'subTitle', 'mainTitleIcon', 'transactions', 'savingsTotal', 'piggyBankAccounts')); } - - /** - * @codeCoverageIgnore - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - */ - public function routes() - { - $directory = '/vagrant_data/Sites/firefly-iii-help'; - $languages = array_keys(Config::get('firefly.lang')); - $routes = []; - $ignored = [ - 'debugbar.openhandler', 'debugbar.assets.css', 'debugbar.assets.js', 'register', 'routes', 'daterange', - 'flush', 'delete-account-post', 'change-password-post', 'logout', 'login', 'tags.hideTagHelp', - 'budgets.postIncome', 'flush' - ]; - - $ignoreMatch = ['.store', '.update', '.destroy', 'json.']; - - $routeCollection = Route::getRoutes(); - /** @var \Illuminate\Routing\Route $object */ - foreach ($routeCollection as $object) { - // get name: - $name = $object->getName(); - // has name and not in ignore list? - if (strlen($name) > 0 && !in_array($name, $ignored)) { - - // not in ignoreMatch? - $continue = true; - foreach ($ignoreMatch as $ignore) { - $match = strpos($name, $ignore); - if (!($match === false)) { - $continue = false; - } - } - unset($ignore, $match); - - if ($continue) { - - $routes[] = $name; - - // check all languages: - foreach ($languages as $lang) { - $file = $directory . '/' . $lang . '/' . $name . '.md'; - if (!file_exists($file)) { - touch($file); - echo $name . '
'; - } - } - } - - - } - - } - - // loop directories with language file. - // tag the ones not in the list of approved routes. - foreach ($languages as $lang) { - $dir = $directory . '/' . $lang; - $set = scandir($dir); - foreach ($set as $entry) { - if ($entry != '.' && $entry != '..') { - $name = str_replace('.md', '', $entry); - if (!in_array($name, $routes)) { - $file = $dir . '/' . $entry; - unlink($file); - } - } - } - } - echo 'Done!'; - } - - } diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php index 37ce8f34f5..6dafae3266 100644 --- a/app/Http/Controllers/JsonController.php +++ b/app/Http/Controllers/JsonController.php @@ -43,7 +43,7 @@ class JsonController extends Controller $cache->addProperty($end); $cache->addProperty('box-bills-paid'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } $amount = 0; @@ -97,7 +97,7 @@ class JsonController extends Controller $cache->addProperty($end); $cache->addProperty('box-bills-unpaid'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } $bills = $repository->getActiveBills(); @@ -156,7 +156,7 @@ class JsonController extends Controller $cache->addProperty($end); $cache->addProperty('box-in'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } $amount = $reportQuery->incomeInPeriodCorrected($start, $end, true)->sum('amount'); @@ -184,7 +184,7 @@ class JsonController extends Controller $cache->addProperty($end); $cache->addProperty('box-out'); if ($cache->has()) { - return Response::json($cache->get()); + return Response::json($cache->get()); // @codeCoverageIgnore } $amount = $reportQuery->expenseInPeriodCorrected($start, $end, true)->sum('amount'); diff --git a/app/Http/Controllers/NewUserController.php b/app/Http/Controllers/NewUserController.php index 29734168c2..569d19115b 100644 --- a/app/Http/Controllers/NewUserController.php +++ b/app/Http/Controllers/NewUserController.php @@ -116,6 +116,6 @@ class NewUserController extends Controller Session::flash('success', 'New account(s) created!'); Preferences::mark(); - return Redirect::route('home'); + return Redirect::route('index'); } } \ No newline at end of file diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index d776a58e1a..f806cd0328 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -141,18 +141,9 @@ class ReportController extends Controller Session::flash('gaEventAction', 'year'); Session::flash('gaEventLabel', $start->format('Y')); - return view( 'reports.year', - compact( - 'start', // the date for this report. - 'shared', // is a shared report? - 'accounts', // all accounts - 'incomes', 'expenses', // expenses and incomes. - 'subTitle', 'subTitleIcon', // subtitle and subtitle icon. - 'incomeTopLength', // length of income top X - 'expenseTopLength' // length of expense top X. - ) + compact('start', 'shared', 'accounts', 'incomes', 'expenses', 'subTitle', 'subTitleIcon', 'incomeTopLength', 'expenseTopLength') ); } diff --git a/app/Http/routes.php b/app/Http/routes.php index b813ee389c..cdff0a70a4 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -183,8 +183,6 @@ Route::controllers( ] ); -Route::get('/routes', ['uses' => 'HomeController@routes', 'as' => 'routes']); - /** * Home Controller */ diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 9c577b68f9..8b00c9b9da 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -140,7 +140,7 @@ class TransactionJournal extends Model $cache->addProperty($this->id); $cache->addProperty('amount'); if ($cache->has()) { - return $cache->get(); + return $cache->get(); // @codeCoverageIgnore } diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 15889ff5d7..23682dccd8 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -114,7 +114,7 @@ class AccountRepository implements AccountRepositoryInterface $cache->addProperty($preference->data); $cache->addProperty('frontPageaccounts'); if ($cache->has()) { - return $cache->get(); + return $cache->get(); // @codeCoverageIgnore } @@ -147,7 +147,7 @@ class AccountRepository implements AccountRepositoryInterface $cache->addProperty($start); $cache->addProperty($end); if ($cache->has()) { - return $cache->get(); + return $cache->get(); // @codeCoverageIgnore } $set = Auth::user() @@ -233,7 +233,7 @@ class AccountRepository implements AccountRepositoryInterface $cache->addProperty($ids); $cache->addProperty('piggyAccounts'); if ($cache->has()) { - return $cache->get(); + return $cache->get(); // @codeCoverageIgnore } $ids = array_unique($ids); diff --git a/app/Support/Amount.php b/app/Support/Amount.php index 101ff86d41..c492e5471a 100644 --- a/app/Support/Amount.php +++ b/app/Support/Amount.php @@ -89,7 +89,7 @@ class Amount $cache->addProperty('formatJournal'); if ($cache->has()) { - return $cache->get(); + return $cache->get(); // @codeCoverageIgnore } diff --git a/app/Support/Steam.php b/app/Support/Steam.php index 234559844b..627faba6d9 100644 --- a/app/Support/Steam.php +++ b/app/Support/Steam.php @@ -30,7 +30,7 @@ class Steam $cache->addProperty($date); $cache->addProperty($ignoreVirtualBalance); if ($cache->has()) { - return $cache->get(); + return $cache->get(); // @codeCoverageIgnore } diff --git a/app/Support/Twig/Journal.php b/app/Support/Twig/Journal.php index 293a2a5a34..36ef577e7a 100644 --- a/app/Support/Twig/Journal.php +++ b/app/Support/Twig/Journal.php @@ -33,7 +33,7 @@ class Journal extends Twig_Extension $cache->addProperty($journal->id); $cache->addProperty('typeIcon'); if ($cache->has()) { - return $cache->get(); + return $cache->get(); // @codeCoverageIgnore } $type = $journal->transactionType->type; diff --git a/tests/controllers/HomeControllerTest.php b/tests/controllers/HomeControllerTest.php index 4ec8a289d1..c4872d64bb 100644 --- a/tests/controllers/HomeControllerTest.php +++ b/tests/controllers/HomeControllerTest.php @@ -122,4 +122,32 @@ class HomeControllerTest extends TestCase } + /** + * @covers FireflyIII\Http\Controllers\HomeController::index + */ + public function testIndexEmpty() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + $this->be($user); + + // mock ALL THE THINGS! + $repository->shouldReceive('countAccounts')->once()->andReturn(0); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + $this->call('GET', '/'); + $this->assertResponseStatus(302); + $this->assertRedirectedToRoute('new-user.index'); + } + } diff --git a/tests/controllers/NewUserControllerTest.php b/tests/controllers/NewUserControllerTest.php new file mode 100644 index 0000000000..e27ddb0d72 --- /dev/null +++ b/tests/controllers/NewUserControllerTest.php @@ -0,0 +1,108 @@ +mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + $this->be($user); + + // mock ALL THE THINGS! + $repository->shouldReceive('countAccounts')->once()->andReturn(0); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + $this->call('GET', '/new-user'); + $this->assertResponseStatus(200); + } + + public function testIndexNoAccounts() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + + + $this->be($user); + + // mock ALL THE THINGS! + $repository->shouldReceive('countAccounts')->once()->andReturn(3); + + // language preference: + $language = FactoryMuffin::create('FireflyIII\Models\Preference'); + $language->data = 'en'; + $language->save(); + Preferences::shouldReceive('get')->withAnyArgs()->andReturn($language); + + $lastActivity = FactoryMuffin::create('FireflyIII\Models\Preference'); + $lastActivity->data = microtime(); + Preferences::shouldReceive('lastActivity')->andReturn($lastActivity); + + $this->call('GET', '/new-user'); + $this->assertResponseStatus(302); + $this->assertRedirectedToRoute('index'); + + } + + public function testPostIndex() + { + $user = FactoryMuffin::create('FireflyIII\User'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + $this->be($user); + + $data = [ + '_token' => 'replaceMe', + 'bank_name' => 'Some Bank', + 'bank_balance' => '100', + 'balance_currency_id' => $currency->id, + 'savings_balance' => '100', + 'credit_card_limit' => '100', + + ]; + + $repository->shouldReceive('store')->andReturn($account); + + $this->call('POST', '/new-user/submit', $data); + $this->assertResponseStatus(302); + $this->assertRedirectedToRoute('index'); + + } + +}