Skip to content

Firefly III: a personal finances manager

New budget repository tests.

James Cole49a65ebff451
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index 42133ba6fb..d66ebb30f4 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -25,21 +25,6 @@ class BudgetRepository implements BudgetRepositoryInterface
*/
public function cleanupBudgets()
{
- $limits = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')->get(['budget_limits.*']);
-
- // loop budget limits:
- $found = [];
- /** @var BudgetLimit $limit */
- foreach ($limits as $limit) {
- $key = $limit->budget_id . '-' . $limit->startdate;
- if (isset($found[$key])) {
- $limit->delete();
- } else {
- $found[$key] = true;
- }
- unset($key);
- }
-
// delete limits with amount 0:
BudgetLimit::where('amount', 0)->delete();
diff --git a/tests/repositories/BudgetRepositoryTest.php b/tests/repositories/BudgetRepositoryTest.php
index 5eb3ce86d3..76d2adf87e 100644
--- a/tests/repositories/BudgetRepositoryTest.php
+++ b/tests/repositories/BudgetRepositoryTest.php
@@ -1,5 +1,9 @@
<?php
+use Carbon\Carbon;
+use FireflyIII\Models\Budget;
+use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetRepository;
+use League\FactoryMuffin\Facade as FactoryMuffin;
/**
* Generated by PHPUnit_SkeletonGenerator on 2015-05-05 at 19:16:07.
@@ -32,14 +36,23 @@ class BudgetRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::cleanupBudgets
- * @todo Implement testCleanupBudgets().
*/
public function testCleanupBudgets()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ // create some budgets:
+ for ($i = 0; $i < 3; $i++) {
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $limit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit');
+ $limit->budget_id = $budget->id;
+ $limit->amount = 0;
+ $limit->save();
+ }
+
+
+ $this->object->cleanupBudgets();
+
+ $this->assertCount(0, BudgetLimit::get());
+
}
/**
@@ -48,10 +61,11 @@ class BudgetRepositoryTest extends TestCase
*/
public function testDestroy()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+
+ $this->object->destroy($budget);
+
+ $this->assertCount(0, Budget::where('id', $budget->id)->whereNull('deleted_at')->get());
}
/**
@@ -60,94 +74,134 @@ class BudgetRepositoryTest extends TestCase
*/
public function testExpensesOnDay()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+
+ $result = $this->object->expensesOnDay($budget, new Carbon);
+
+ $this->assertEquals(0, $result);
}
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::getActiveBudgets
- * @todo Implement testGetActiveBudgets().
*/
public function testGetActiveBudgets()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $budget1 = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $budget2 = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $budget1->active = 1;
+ $budget2->active = 0;
+ $budget2->user_id = $budget1->user_id;
+ $budget1->save();
+ $budget2->save();
+ $this->be($budget1->user);
+
+ $set = $this->object->getActiveBudgets();
+
+ $this->assertCount(1, $set);
+ $this->assertEquals($set->first()->id, $budget1->id);
+
}
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::getBudgetLimitRepetitions
- * @todo Implement testGetBudgetLimitRepetitions().
*/
public function testGetBudgetLimitRepetitions()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $rep = FactoryMuffin::create('FireflyIII\Models\LimitRepetition');
+ $limit = $rep->budgetlimit;
+ $limit->startdate = new Carbon('2015-02-02');
+ $rep->startdate = new Carbon('2015-02-02');
+ $rep->enddate = new Carbon('2015-02-28');
+ $limit->save();
+ $rep->save();
+
+ $set = $this->object->getBudgetLimitRepetitions($rep->budgetlimit->budget, new Carbon('2015-02-01'), new Carbon('2015-02-28'));
+ $this->assertCount(2, $set);
}
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::getBudgetLimits
- * @todo Implement testGetBudgetLimits().
*/
public function testGetBudgetLimits()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ /** @var Budget $budget */
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $set = $this->object->getBudgetLimits($budget);
+
+ $this->assertCount(0, $set);
}
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::getBudgets
- * @todo Implement testGetBudgets().
*/
public function testGetBudgets()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $budget1 = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $budget2 = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $budget1->active = 1;
+ $budget2->active = 0;
+ $budget2->user_id = $budget1->user_id;
+ $budget1->save();
+ $budget2->save();
+ $this->be($budget1->user);
+
+ $set = $this->object->getBudgets();
+
+ $this->assertCount(2, $set);
}
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::getCurrentRepetition
- * @todo Implement testGetCurrentRepetition().
*/
public function testGetCurrentRepetition()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ /** @var Budget $budget */
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $rep = $this->object->getCurrentRepetition($budget, new Carbon);
+ $this->assertNull($rep);
+
}
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::getFirstBudgetLimitDate
- * @todo Implement testGetFirstBudgetLimitDate().
*/
public function testGetFirstBudgetLimitDate()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ /** @var BudgetLimit $budget */
+ $limit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit');
+ $date = $this->object->getFirstBudgetLimitDate($limit->budget);
+ $this->assertEquals($date->format('Y-m-d'), $limit->startdate->format('Y-m-d'));
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Budget\BudgetRepository::getFirstBudgetLimitDate
+ */
+ public function testGetFirstBudgetLimitDateNull()
+ {
+ /** @var BudgetLimit $budget */
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $date = $this->object->getFirstBudgetLimitDate($budget);
+ $this->assertNull($date);
}
/**
* @covers FireflyIII\Repositories\Budget\BudgetRepository::getInactiveBudgets
- * @todo Implement testGetInactiveBudgets().
*/
public function testGetInactiveBudgets()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $budget1 = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $budget2 = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $budget1->active = 1;
+ $budget2->active = 0;
+ $budget2->user_id = $budget1->user_id;
+ $budget1->save();
+ $budget2->save();
+ $this->be($budget1->user);
+
+ $set = $this->object->getInactiveBudgets();
+
+ $this->assertCount(1, $set);
+ $this->assertEquals($set->first()->id, $budget2->id);
}
/**