Skip to content

Firefly III: a personal finances manager

Clean up and tests.

James Cole36c8171d0f31
diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php
index 9efbd64e74..b06c6237e9 100644
--- a/app/Helpers/Report/ReportHelper.php
+++ b/app/Helpers/Report/ReportHelper.php
@@ -18,25 +18,6 @@ use Steam;
class ReportHelper implements ReportHelperInterface
{
- /**
- * This methods fails to take in account transfers FROM shared accounts.
- *
- * @param Carbon $start
- * @param Carbon $end
- * @param int $limit
- *
- * @return Collection
- */
- public function expensesGroupedByAccount(Carbon $start, Carbon $end, $limit = 15)
- {
- $result = $this->_queries->journalsByExpenseAccount($start, $end);
- $array = $this->_helper->makeArray($result);
- $limited = $this->_helper->limitArray($array, $limit);
-
- return $limited;
-
- }
-
/**
* This method gets some kind of list for a monthly overview.
*
diff --git a/app/Helpers/Report/ReportHelperInterface.php b/app/Helpers/Report/ReportHelperInterface.php
index 2bbbefec75..8b66f48f15 100644
--- a/app/Helpers/Report/ReportHelperInterface.php
+++ b/app/Helpers/Report/ReportHelperInterface.php
@@ -13,18 +13,6 @@ use Illuminate\Support\Collection;
interface ReportHelperInterface
{
-
- /**
- * This methods fails to take in account transfers FROM shared accounts.
- *
- * @param Carbon $start
- * @param Carbon $end
- * @param int $limit
- *
- * @return Collection
- */
- public function expensesGroupedByAccount(Carbon $start, Carbon $end, $limit = 15);
-
/**
* This method gets some kind of list for a monthly overview.
*
diff --git a/tests/helpers/ReportHelperTest.php b/tests/helpers/ReportHelperTest.php
new file mode 100644
index 0000000000..d11b0416ee
--- /dev/null
+++ b/tests/helpers/ReportHelperTest.php
@@ -0,0 +1,234 @@
+<?php
+
+use Carbon\Carbon;
+use FireflyIII\Helpers\Report\ReportHelper;
+use FireflyIII\Models\AccountMeta;
+use FireflyIII\Models\PiggyBankRepetition;
+use FireflyIII\Models\Transaction;
+use League\FactoryMuffin\Facade as FactoryMuffin;
+
+/**
+ * Class ReportHelperTest
+ */
+class ReportHelperTest extends TestCase
+{
+ /**
+ * @var ReportHelper
+ */
+ protected $object;
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ */
+ public function setUp()
+ {
+ parent::setUp();
+ FactoryMuffin::create('FireflyIII\User');
+ $this->object = new ReportHelper;
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ */
+ public function tearDown()
+ {
+ parent::tearDown();
+ }
+
+ /**
+ * @covers FireflyIII\Helpers\Report\ReportHelper::getBudgetsForMonth
+ * @covers FireflyIII\Helpers\Report\ReportQuery::journalsByBudget
+ * @covers FireflyIII\Helpers\Report\ReportQuery::sharedExpenses
+ */
+ public function testGetBudgetsForMonthWithShared()
+ {
+ $date = new Carbon('2015-01-01');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $budgets = [];
+
+ // three budget limits starting on the $date:
+ for ($i = 0; $i < 3; $i++) {
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $budgetLimit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit');
+ $budgetLimit->startdate = $date;
+ $budget->user_id = $user->id;
+ $budget->save();
+ $budgetLimit->save();
+ $budgets[] = $budget;
+ }
+
+ $this->be($user);
+
+ $result = $this->object->getBudgetsForMonth($date, true);
+
+ // assert each budget is in the array:
+ foreach ($budgets as $budget) {
+ $id = $budget->id;
+ $this->assertEquals($budget->name, $result[$id]['name']);
+ }
+ $this->assertEquals(0, $result[0]['queryAmount']);
+ $this->assertEquals('No budget', $result[0]['name']);
+ }
+
+ /**
+ * @covers FireflyIII\Helpers\Report\ReportHelper::getBudgetsForMonth
+ * @covers FireflyIII\Helpers\Report\ReportQuery::journalsByBudget
+ */
+ public function testGetBudgetsForMonthWithoutShared()
+ {
+ $date = new Carbon('2015-01-01');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $budgets = [];
+
+ // three budget limits starting on the $date:
+ for ($i = 0; $i < 3; $i++) {
+ $budget = FactoryMuffin::create('FireflyIII\Models\Budget');
+ $budgetLimit = FactoryMuffin::create('FireflyIII\Models\BudgetLimit');
+ $budgetLimit->startdate = $date;
+ $budget->user_id = $user->id;
+ $budget->save();
+ $budgetLimit->save();
+ $budgets[] = $budget;
+ }
+
+ $this->be($user);
+
+ $result = $this->object->getBudgetsForMonth($date, false);
+
+ // assert each budget is in the array:
+ foreach ($budgets as $budget) {
+ $id = $budget->id;
+ $this->assertEquals($budget->name, $result[$id]['name']);
+ }
+ $this->assertEquals(0, $result[0]['queryAmount']);
+ $this->assertEquals('No budget', $result[0]['name']);
+ }
+
+ public function testListOfMonths()
+ {
+ // start of year up until now
+ $date = new Carbon('2015-01-01');
+ $now = new Carbon;
+ $diff = $now->diffInMonths($date) + 1; // the month itself.
+ $result = $this->object->listOfMonths($date);
+
+ $this->assertCount($diff, $result[2015]);
+
+ }
+
+ public function testListOfYears()
+ {
+
+ $date = new Carbon('2015-01-01');
+ $now = new Carbon;
+ $diff = $now->diffInYears($date) + 1; // the year itself.
+ $result = $this->object->listOfYears($date);
+ $this->assertCount($diff, $result);
+ }
+
+ public function testYearBalanceReport()
+ {
+ $date = new Carbon('2015-01-01');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $setShared = [];
+ $setNormal = [];
+
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ $assetType = FactoryMuffin::create('FireflyIII\Models\AccountType');
+
+ // need some shared accounts:
+ for ($i = 0; $i < 3; $i++) {
+ $shared = FactoryMuffin::create('FireflyIII\Models\Account');
+ $shared->user_id = $user->id;
+ $shared->account_type_id = $assetType->id;
+ $shared->save();
+ // meta for shared:
+ AccountMeta::create(
+ [
+ 'account_id' => $shared->id,
+ 'name' => 'accountRole',
+ 'data' => 'sharedAsset',
+ ]
+ );
+ $setShared[] = $shared;
+ }
+
+ // need some normal accounts:
+ for ($i = 0; $i < 3; $i++) {
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $account->user_id = $user->id;
+ $account->account_type_id = $assetType->id;
+ $account->save();
+ $setNormal[] = $account;
+ }
+
+ // mock stuff:
+ Steam::shouldReceive('balance')->withAnyArgs()->andReturn(0);
+
+ $this->be($user);
+
+ $result = $this->object->yearBalanceReport($date, false);
+ foreach($result as $entry) {
+ // everything is hidden:
+ $this->assertTrue($entry['hide']);
+ // nothing is shared:
+ $this->assertFalse($entry['shared']);
+ }
+
+ }
+
+ public function testYearBalanceReportWithShared()
+ {
+ $date = new Carbon('2015-01-01');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $setShared = [];
+ $setNormal = [];
+
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ FactoryMuffin::create('FireflyIII\Models\AccountType');
+ $assetType = FactoryMuffin::create('FireflyIII\Models\AccountType');
+
+ // need some shared accounts:
+ for ($i = 0; $i < 3; $i++) {
+ $shared = FactoryMuffin::create('FireflyIII\Models\Account');
+ $shared->user_id = $user->id;
+ $shared->account_type_id = $assetType->id;
+ $shared->save();
+ // meta for shared:
+ AccountMeta::create(
+ [
+ 'account_id' => $shared->id,
+ 'name' => 'accountRole',
+ 'data' => 'sharedAsset',
+ ]
+ );
+ $setShared[] = $shared;
+ }
+
+ // need some normal accounts:
+ for ($i = 0; $i < 3; $i++) {
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $account->user_id = $user->id;
+ $account->account_type_id = $assetType->id;
+ $account->save();
+ $setNormal[] = $account;
+ }
+
+ // mock stuff:
+ Steam::shouldReceive('balance')->withAnyArgs()->andReturn(0);
+
+ $this->be($user);
+
+ $result = $this->object->yearBalanceReport($date, true);
+ foreach($result as $entry) {
+ // everything is hidden:
+ $this->assertTrue($entry['hide']);
+ // nothing is shared:
+ $this->assertFalse($entry['shared']);
+ }
+
+ }
+}
\ No newline at end of file