Skip to content

Firefly III: a personal finances manager

Some more coverage.

James Cole2b6b896c2ec9
diff --git a/app/Models/PiggyBankRepetition.php b/app/Models/PiggyBankRepetition.php
index 6c5c939b16..07202b672e 100644
--- a/app/Models/PiggyBankRepetition.php
+++ b/app/Models/PiggyBankRepetition.php
@@ -12,6 +12,8 @@ use Illuminate\Database\Eloquent\Model;
class PiggyBankRepetition extends Model
{
+ protected $fillable = ['piggy_bank_id', 'startdate', 'targetdate', 'currentamount'];
+
/**
* @return array
*/
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index a6ab2ab782..9825b5cd0c 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -226,7 +226,7 @@ class AccountRepository implements AccountRepositoryInterface
// then, percentage.
$difference = $account->endBalance - $account->piggyBalance;
$account->difference = $difference;
- $account->percentage = $difference != 0 ? round((($difference / $account->endBalance) * 100)) : 100;
+ $account->percentage = $difference != 0 && $account->endBalance != 0 ? round((($difference / $account->endBalance) * 100)) : 100;
}
);
diff --git a/tests/factories/all.php b/tests/factories/all.php
index 07a1c1f529..33c2577c03 100644
--- a/tests/factories/all.php
+++ b/tests/factories/all.php
@@ -227,6 +227,19 @@ FactoryMuffin::define(
]
);
+FactoryMuffin::define(
+ 'FireflyIII\Models\PiggyBankRepetition',
+ [
+ 'piggy_bank_id' => 'factory|FireflyIII\Models\PiggyBank',
+ 'startdate' => 'date',
+ 'targetdate' => 'date',
+ 'currentamount' => function () {
+ return rand(1, 100);
+ },
+ ]
+);
+
+
FactoryMuffin::define(
'FireflyIII\Models\PiggyBankEvent',
[
diff --git a/tests/repositories/AccountRepositoryTest.php b/tests/repositories/AccountRepositoryTest.php
index 3407892ae6..e739da0d63 100644
--- a/tests/repositories/AccountRepositoryTest.php
+++ b/tests/repositories/AccountRepositoryTest.php
@@ -2,6 +2,7 @@
use Carbon\Carbon;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
+use FireflyIII\Models\PiggyBankRepetition;
use FireflyIII\Models\Preference;
use FireflyIII\Models\Transaction;
use FireflyIII\Repositories\Account\AccountRepository;
@@ -169,7 +170,6 @@ class AccountRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Account\AccountRepository::getFrontpageAccounts
- * @todo Implement testGetFrontpageAccounts().
*/
public function testGetFrontpageAccountsWithPreference()
{
@@ -316,12 +316,49 @@ class AccountRepositoryTest extends TestCase
/**
* @covers FireflyIII\Repositories\Account\AccountRepository::getLastActivity
- * @todo Implement testGetLastActivity().
*/
public function testGetLastActivity()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete('This test has not been implemented yet.');
+ $date = new Carbon('2012-02-02');
+ // one journals
+ /** @var Account $account */
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $journal->date = $date;
+ $journal->user_id = $account->user_id;
+ $journal->save();
+
+ // transaction to match the date (one will do)
+ Transaction::create(
+ [
+ 'account_id' => $account->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => 100
+ ]
+ );
+
+ // be user
+ $this->be($journal->user);
+
+ $latest = $this->object->getLastActivity($account);
+ $this->assertEquals($date->format('Y-m-d'), $latest->format('Y-m-d'));
+
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Account\AccountRepository::getLastActivity
+ */
+ public function testGetLastActivityNoActivity()
+ {
+ $date = new Carbon('2012-02-02');
+ // one journals
+ /** @var Account $account */
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $this->be($account->user);
+
+ $latest = $this->object->getLastActivity($account);
+ $this->assertnull($latest);
+
}
/**
@@ -330,8 +367,39 @@ class AccountRepositoryTest extends TestCase
*/
public function testGetPiggyBankAccounts()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete('This test has not been implemented yet.');
+ $date = Carbon::now()->startOfMonth()->addDays(3);
+ /*
+ * Quite the collection of objects for this one.
+ */
+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $piggyBankRepetition = $piggyBank->piggybankRepetitions()->first();
+ /*
+ * Update id's to match each other:
+ */
+ $piggyBankRepetition->currentamount = rand(1,100);
+ $piggyBankRepetition->startdate = $date;
+ $piggyBankRepetition->targetdate = $date;
+ $piggyBank->account_id = $account->id;
+ $piggyBankRepetition->save();
+ $piggyBank->save();
+
+ /*
+ * Put dates in session:
+ */
+ $this->session(['start' => Carbon::now()->startOfMonth(), 'end' => Carbon::now()->endOfMonth()]);
+
+ /*
+ * Run method:
+ */
+ $this->be($account->user);
+ $collection = $this->object->getPiggyBankAccounts();
+
+ $this->assertCount(1, $collection);
+ $this->assertEquals($collection->first()->id, $account->id);
+ $this->assertEquals($collection->first()->piggyBalance, $piggyBankRepetition->currentamount);
+ $this->assertEquals(0, $collection->first()->startBalance);
+ $this->assertEquals(0, $collection->first()->endBalance);
}
/**
diff --git a/tests/repositories/BudgetRepositoryTest.php b/tests/repositories/BudgetRepositoryTest.php
index 5ae8641b39..5eb3ce86d3 100644
--- a/tests/repositories/BudgetRepositoryTest.php
+++ b/tests/repositories/BudgetRepositoryTest.php
@@ -27,6 +27,7 @@ class BudgetRepositoryTest extends TestCase
*/
public function tearDown()
{
+ parent::tearDown();
}
/**
diff --git a/tests/repositories/CategoryRepositoryTest.php b/tests/repositories/CategoryRepositoryTest.php
index b17a838fdc..e5288656c4 100644
--- a/tests/repositories/CategoryRepositoryTest.php
+++ b/tests/repositories/CategoryRepositoryTest.php
@@ -27,6 +27,7 @@ class CategoryRepositoryTest extends TestCase
*/
public function tearDown()
{
+ parent::tearDown();
}
/**
diff --git a/tests/repositories/CurrencyRepositoryTest.php b/tests/repositories/CurrencyRepositoryTest.php
index 650b1a16b0..756991dd3d 100644
--- a/tests/repositories/CurrencyRepositoryTest.php
+++ b/tests/repositories/CurrencyRepositoryTest.php
@@ -27,6 +27,7 @@ class CurrencyRepositoryTest extends TestCase
*/
public function tearDown()
{
+ parent::tearDown();
}
/**
diff --git a/tests/repositories/JournalRepositoryTest.php b/tests/repositories/JournalRepositoryTest.php
index 0db59abc60..69840beaef 100644
--- a/tests/repositories/JournalRepositoryTest.php
+++ b/tests/repositories/JournalRepositoryTest.php
@@ -27,6 +27,7 @@ class JournalRepositoryTest extends TestCase
*/
public function tearDown()
{
+ parent::tearDown();
}
/**
diff --git a/tests/repositories/PiggyBankRepositoryTest.php b/tests/repositories/PiggyBankRepositoryTest.php
index 8dcf8e0864..acbb997467 100644
--- a/tests/repositories/PiggyBankRepositoryTest.php
+++ b/tests/repositories/PiggyBankRepositoryTest.php
@@ -27,6 +27,7 @@ class PiggyBankRepositoryTest extends TestCase
*/
public function tearDown()
{
+ parent::tearDown();
}
/**
diff --git a/tests/repositories/ReminderRepositoryTest.php b/tests/repositories/ReminderRepositoryTest.php
index 2a7e96652c..c0a5a2a39d 100644
--- a/tests/repositories/ReminderRepositoryTest.php
+++ b/tests/repositories/ReminderRepositoryTest.php
@@ -27,6 +27,7 @@ class ReminderRepositoryTest extends TestCase
*/
public function tearDown()
{
+ parent::tearDown();
}
/**
diff --git a/tests/repositories/TagRepositoryTest.php b/tests/repositories/TagRepositoryTest.php
index 36b023ba7b..fb1b57cd0c 100644
--- a/tests/repositories/TagRepositoryTest.php
+++ b/tests/repositories/TagRepositoryTest.php
@@ -27,6 +27,7 @@ class TagRepositoryTest extends TestCase
*/
public function tearDown()
{
+ parent::tearDown();
}
/**