Fixed tests

This commit is contained in:
James Cole 2015-05-16 09:25:14 +02:00
parent 0a372b0daf
commit e155d3311c
7 changed files with 41 additions and 10 deletions

View File

@ -359,7 +359,7 @@ class GoogleChartController extends Controller
while ($start <= $end) {
$currentEnd = Navigation::endOfPeriod($start, $range->data);
$spent = $repository->spentInPeriodSum($category, $start, $currentEnd);
$spent = $repository->spentInPeriod($category, $start, $currentEnd);
$chart->addRow(clone $start, $spent);
$start = Navigation::addPeriod($start, $range->data, 0);

View File

@ -5,6 +5,7 @@ namespace FireflyIII\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Helpers\Report\ReportQueryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Grumpydictator\Gchart\GChart;
use Response;
@ -52,11 +53,35 @@ class ReportChartController extends Controller
return Response::json($chart->getData());
}
public function yearCategories(GChart $chart, $year, $shared = false)
public function yearCategories(GChart $chart, CategoryRepositoryInterface $repository, $year, $shared = false)
{
$start = new Carbon($year . '-01-01');
$end = new Carbon($year . '-12-31');
$shared = $shared == 'shared' ? true : false;
$categories = $repository->getCategories();
// add columns:
$chart->addColumn(trans('firefly.month'), 'date');
foreach ($categories as $category) {
$chart->addColumn($category->name, 'number');
}
while ($start < $end) {
// month is the current end of the period:
$month = clone $start;
$month->endOfMonth();
// make a row:
$row = [clone $start];
// each budget, fill the row:
foreach ($categories as $category) {
$spent = $repository->spentInPeriod($category, $start, $month, $shared);
$row[] = $spent;
}
$chart->addRowArray($row);
$start->addMonth();
}
$chart->generate();

View File

@ -170,9 +170,15 @@ class CategoryRepository implements CategoryRepositoryInterface
*
* @return float
*/
public function spentInPeriodSum(Category $category, Carbon $start, Carbon $end)
public function spentInPeriod(Category $category, Carbon $start, Carbon $end, $shared = false)
{
return floatval($category->transactionjournals()->before($end)->after($start)->lessThan(0)->sum('amount')) * -1;
if($shared === false) {
// do something else, SEE budgets.
$sum = floatval($category->transactionjournals()->before($end)->after($start)->lessThan(0)->sum('amount')) * -1;
} else {
$sum = floatval($category->transactionjournals()->before($end)->after($start)->lessThan(0)->sum('amount')) * -1;
}
return $sum;
}
/**

View File

@ -77,7 +77,7 @@ interface CategoryRepositoryInterface
*
* @return float
*/
public function spentInPeriodSum(Category $category, Carbon $start, Carbon $end);
public function spentInPeriod(Category $category, Carbon $start, Carbon $end, $shared = false);
/**
* @param Category $category

View File

@ -212,7 +212,7 @@ class GoogleChartControllerTest extends TestCase
// mock!
$repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
$repository->shouldReceive('getFirstActivityDate')->andReturn($start);
$repository->shouldReceive('spentInPeriodSum')->andReturn(rand(1, 100));
$repository->shouldReceive('spentInPeriod')->andReturn(rand(1, 100));
Preferences::shouldReceive('get')->andReturn($pref);
Navigation::shouldReceive('startOfPeriod')->andReturn($start);

View File

@ -292,11 +292,11 @@ class BudgetRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::spentInMonth
*/
public function testSpentInMonth()
public function testSpentInPeriod()
{
$budget = FactoryMuffin::create('FireflyIII\Models\Budget');
$amount = $this->object->spentInMonth($budget, new Carbon);
$amount = $this->object->spentInPeriod($budget, new Carbon, new Carbon);
$this->assertEquals(0, $amount);
}

View File

@ -189,12 +189,12 @@ class CategoryRepositoryTest extends TestCase
}
/**
* @covers FireflyIII\Repositories\Category\CategoryRepository::spentInPeriodSum
* @covers FireflyIII\Repositories\Category\CategoryRepository::spentInPeriod
*/
public function testSpentInPeriodSum()
{
$category = FactoryMuffin::create('FireflyIII\Models\Category');
$sum = $this->object->spentInPeriodSum($category, new Carbon, new Carbon);
$sum = $this->object->spentInPeriod($category, new Carbon, new Carbon);
$this->assertEquals(0, $sum);