From 98c1fcc68fb4dc519bf841ebedabbe7d26215682 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 1 Jan 2015 20:53:36 +0100 Subject: [PATCH] New content and tests ensure the coverage of all code. --- app/controllers/GoogleChartController.php | 158 +++++++++--------- app/database/seeds/TestContentSeeder.php | 22 ++- .../functional/GoogleChartControllerCest.php | 6 +- 3 files changed, 100 insertions(+), 86 deletions(-) diff --git a/app/controllers/GoogleChartController.php b/app/controllers/GoogleChartController.php index d9760aab5c..eb6bfab8e8 100644 --- a/app/controllers/GoogleChartController.php +++ b/app/controllers/GoogleChartController.php @@ -132,18 +132,18 @@ class GoogleChartController extends BaseController /** @var Budget $budget */ foreach ($budgets as $budget) { - Log::debug('Now working budget #'.$budget->id.', '.$budget->name); + Log::debug('Now working budget #' . $budget->id . ', ' . $budget->name); /** @var \LimitRepetition $repetition */ $repetition = $bdt->repetitionOnStartingOnDate($budget, $this->_start); if (is_null($repetition)) { - \Log::debug('Budget #'.$budget->id.' has no repetition on ' . $this->_start->format('Y-m-d')); + \Log::debug('Budget #' . $budget->id . ' has no repetition on ' . $this->_start->format('Y-m-d')); // use the session start and end for our search query $searchStart = $this->_start; $searchEnd = $this->_end; $limit = 0; // the limit is zero: } else { - \Log::debug('Budget #'.$budget->id.' has a repetition on ' . $this->_start->format('Y-m-d').'!'); + \Log::debug('Budget #' . $budget->id . ' has a repetition on ' . $this->_start->format('Y-m-d') . '!'); // use the limit's start and end for our search query $searchStart = $repetition->startdate; $searchEnd = $repetition->enddate; @@ -151,6 +151,7 @@ class GoogleChartController extends BaseController } $expenses = floatval($budget->transactionjournals()->before($searchEnd)->after($searchStart)->lessThan(0)->sum('amount')) * -1; + \Log::debug('Expenses in budget ' . $budget->name . ' before ' . $searchEnd->format('Y-m-d') . ' and after ' . $searchStart . ' are: ' . $expenses); if ($expenses > 0) { $this->_chart->addRow($budget->name, $limit, $expenses); } @@ -186,6 +187,76 @@ class GoogleChartController extends BaseController } + /** + * @param Bill $bill + * + * @return \Illuminate\Http\JsonResponse + */ + public function billOverview(Bill $bill) + { + + $this->_chart->addColumn('Date', 'date'); + $this->_chart->addColumn('Max amount', 'number'); + $this->_chart->addColumn('Min amount', 'number'); + $this->_chart->addColumn('Current entry', 'number'); + + // get first transaction or today for start: + $first = $bill->transactionjournals()->orderBy('date', 'ASC')->first(); + if ($first) { + $start = $first->date; + } else { + $start = new Carbon; + } + $end = new Carbon; + while ($start <= $end) { + $result = $bill->transactionjournals()->before($end)->after($start)->first(); + if ($result) { + $amount = $result->getAmount(); + } else { + $amount = 0; + } + unset($result); + $this->_chart->addRow(clone $start, $bill->amount_max, $bill->amount_min, $amount); + $start = DateKit::addPeriod($start, $bill->repeat_freq, 0); + } + + $this->_chart->generate(); + + return Response::json($this->_chart->getData()); + + } + + /** + * TODO query move to helper. + * + * @return \Illuminate\Http\JsonResponse + * @throws \FireflyIII\Exception\FireflyException + */ + public function billsOverview() + { + $paid = ['items' => [], 'amount' => 0]; + $unpaid = ['items' => [], 'amount' => 0]; + $this->_chart->addColumn('Name', 'string'); + $this->_chart->addColumn('Amount', 'number'); + + $set = $this->_repository->getBillsSummary($this->_start, $this->_end); + + foreach ($set as $entry) { + if (intval($entry->journalId) == 0) { + $unpaid['items'][] = $entry->name; + $unpaid['amount'] += floatval($entry->averageAmount); + } else { + $paid['items'][] = $entry->description; + $paid['amount'] += floatval($entry->actualAmount); + } + } + $this->_chart->addRow('Unpaid: ' . join(', ', $unpaid['items']), $unpaid['amount']); + $this->_chart->addRow('Paid: ' . join(', ', $paid['items']), $paid['amount']); + $this->_chart->generate(); + + return Response::json($this->_chart->getData()); + } + /** * TODO still in use? * @@ -223,12 +294,12 @@ class GoogleChartController extends BaseController /** * TODO still in use? * - * @param Budget $component + * @param Budget $budget * @param $year * * @return \Illuminate\Http\JsonResponse */ - public function budgetsAndSpending(Budget $component, $year) + public function budgetsAndSpending(Budget $budget, $year) { try { new Carbon('01-01-' . $year); @@ -247,11 +318,14 @@ class GoogleChartController extends BaseController $end = clone $start; $end->endOfYear(); while ($start <= $end) { - $spent = $budgetRepository->spentInMonth($component, $start); - $repetition = $budgetRepository->repetitionOnStartingOnDate($component, $start); + $spent = $budgetRepository->spentInMonth($budget, $start); + $repetition = $budgetRepository->repetitionOnStartingOnDate($budget, $start); + if ($repetition) { $budgeted = floatval($repetition->amount); + \Log::debug('Found a repetition on ' . $start->format('Y-m-d'). ' for budget ' . $budget->name.'!'); } else { + \Log::debug('No repetition on ' . $start->format('Y-m-d'). ' for budget ' . $budget->name); $budgeted = null; } @@ -334,76 +408,6 @@ class GoogleChartController extends BaseController } - /** - * @param Bill $bill - * - * @return \Illuminate\Http\JsonResponse - */ - public function billOverview(Bill $bill) - { - - $this->_chart->addColumn('Date', 'date'); - $this->_chart->addColumn('Max amount', 'number'); - $this->_chart->addColumn('Min amount', 'number'); - $this->_chart->addColumn('Current entry', 'number'); - - // get first transaction or today for start: - $first = $bill->transactionjournals()->orderBy('date', 'ASC')->first(); - if ($first) { - $start = $first->date; - } else { - $start = new Carbon; - } - $end = new Carbon; - while ($start <= $end) { - $result = $bill->transactionjournals()->before($end)->after($start)->first(); - if ($result) { - $amount = $result->getAmount(); - } else { - $amount = 0; - } - unset($result); - $this->_chart->addRow(clone $start, $bill->amount_max, $bill->amount_min, $amount); - $start = DateKit::addPeriod($start, $bill->repeat_freq, 0); - } - - $this->_chart->generate(); - - return Response::json($this->_chart->getData()); - - } - - /** - * TODO query move to helper. - * - * @return \Illuminate\Http\JsonResponse - * @throws \FireflyIII\Exception\FireflyException - */ - public function billsOverview() - { - $paid = ['items' => [], 'amount' => 0]; - $unpaid = ['items' => [], 'amount' => 0]; - $this->_chart->addColumn('Name', 'string'); - $this->_chart->addColumn('Amount', 'number'); - - $set = $this->_repository->getBillsSummary($this->_start, $this->_end); - - foreach ($set as $entry) { - if (intval($entry->journalId) == 0) { - $unpaid['items'][] = $entry->name; - $unpaid['amount'] += floatval($entry->averageAmount); - } else { - $paid['items'][] = $entry->description; - $paid['amount'] += floatval($entry->actualAmount); - } - } - $this->_chart->addRow('Unpaid: ' . join(', ', $unpaid['items']), $unpaid['amount']); - $this->_chart->addRow('Paid: ' . join(', ', $paid['items']), $paid['amount']); - $this->_chart->generate(); - - return Response::json($this->_chart->getData()); - } - /** * TODO see reports for better way to do this. * diff --git a/app/database/seeds/TestContentSeeder.php b/app/database/seeds/TestContentSeeder.php index 3f5468e7ec..8520a98043 100644 --- a/app/database/seeds/TestContentSeeder.php +++ b/app/database/seeds/TestContentSeeder.php @@ -109,18 +109,24 @@ class TestContentSeeder extends Seeder $euro = TransactionCurrency::whereCode('EUR')->first(); + $rentBill = Bill::where('name', 'Rent')->first(); + $current = clone $this->_yearAgoStartOfMonth; - while ($current < $this->_startOfMonth) { + while ($current <= $this->_startOfMonth) { $cur = $current->format('Y-m-d'); $formatted = $current->format('F Y'); // create expenses for rent, utilities, TV, phone on the 1st of the month. - $this->createTransaction($checking, $landLord, 800, $withdrawal, 'Rent for ' . $formatted, $cur, $euro, $bills, $house); + $this->createTransaction($checking, $landLord, 800, $withdrawal, 'Rent for ' . $formatted, $cur, $euro, $bills, $house, $rentBill); $this->createTransaction($checking, $utilities, 150, $withdrawal, 'Utilities for ' . $formatted, $cur, $euro, $bills, $house); $this->createTransaction($checking, $television, 50, $withdrawal, 'TV for ' . $formatted, $cur, $euro, $bills, $house); $this->createTransaction($checking, $phone, 50, $withdrawal, 'Phone bill for ' . $formatted, $cur, $euro, $bills, $house); + // two transactions. One without a budget, one without a category. + $this->createTransaction($checking, $phone, 10, $withdrawal, 'Extra charges on phone bill for ' . $formatted, $cur, $euro, null, $house); + $this->createTransaction($checking, $television, 5, $withdrawal, 'Extra charges on TV bill for ' . $formatted, $cur, $euro, $bills, null); + // income from job: $this->createTransaction($employer, $checking, rand(3500, 4000), $deposit, 'Salary for ' . $formatted, $cur, $euro); $this->createTransaction($checking, $savings, 2000, $transfer, 'Salary to savings account in ' . $formatted, $cur, $euro); @@ -225,9 +231,10 @@ class TestContentSeeder extends Seeder public function createBudgets(User $user) { - $groceries = Budget::create(['user_id' => $user->id, 'name' => 'Groceries']); - $bills = Budget::create(['user_id' => $user->id, 'name' => 'Bills']); - $deleteMe = Budget::create(['user_id' => $user->id, 'name' => 'Delete me']); + $groceries = Budget::create(['user_id' => $user->id, 'name' => 'Groceries']); + $bills = Budget::create(['user_id' => $user->id, 'name' => 'Bills']); + $deleteMe = Budget::create(['user_id' => $user->id, 'name' => 'Delete me']); + Budget::create(['user_id' => $user->id, 'name' => 'Budget without repetition']); $groceriesLimit = BudgetLimit::create( ['startdate' => $this->som, 'amount' => 201, 'repeats' => 0, 'repeat_freq' => 'monthly', 'budget_id' => $groceries->id] ); @@ -405,8 +412,9 @@ class TestContentSeeder extends Seeder // bill Bill::create( [ - 'user_id' => $user->id, 'name' => 'Huur', 'match' => 'huur,portaal', 'amount_min' => 500, - 'amount_max' => 700, + 'user_id' => $user->id, 'name' => 'Rent', 'match' => 'rent,landlord', + 'amount_min' => 700, + 'amount_max' => 900, 'date' => $this->som, 'active' => 1, 'automatch' => 1, diff --git a/tests/functional/GoogleChartControllerCest.php b/tests/functional/GoogleChartControllerCest.php index b0697dcc42..274da2e0de 100644 --- a/tests/functional/GoogleChartControllerCest.php +++ b/tests/functional/GoogleChartControllerCest.php @@ -108,8 +108,9 @@ class GoogleChartControllerCest */ public function budgetsAndSpending(FunctionalTester $I) { + $year = date('Y'); $I->wantTo('see the chart for a budget in a specific year'); - $I->amOnPage('/chart/budget/1/spending/2014'); + $I->amOnPage('/chart/budget/1/spending/'.$year); $I->seeResponseCodeIs(200); } @@ -129,8 +130,9 @@ class GoogleChartControllerCest */ public function categoriesAndSpending(FunctionalTester $I) { + $year = date('Y'); $I->wantTo('see the chart for a category in a specific year'); - $I->amOnPage('/chart/category/1/spending/2014'); + $I->amOnPage('/chart/category/1/spending/'.$year); $I->seeResponseCodeIs(200); }