Skip to content

Firefly III: a personal finances manager

New tests.

James Colea3a25db2302f
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index a267f4a0bb..0c3419f7b9 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -262,11 +262,8 @@ class BillRepository implements BillRepositoryInterface
/*
* Attach expense account to description for more narrow matching.
*/
- if (count($journal->transactions) < 2) {
- $transactions = $journal->transactions()->get();
- } else {
- $transactions = $journal->transactions;
- }
+ $transactions = $journal->transactions()->get();
+
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
/** @var Account $account */
diff --git a/tests/repositories/BillRepositoryTest.php b/tests/repositories/BillRepositoryTest.php
index e1a579dc59..803c509d46 100644
--- a/tests/repositories/BillRepositoryTest.php
+++ b/tests/repositories/BillRepositoryTest.php
@@ -1,6 +1,8 @@
<?php
use Carbon\Carbon;
use FireflyIII\Models\Bill;
+use FireflyIII\Models\Transaction;
+use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepository;
use League\FactoryMuffin\Facade as FactoryMuffin;
@@ -146,70 +148,293 @@ class BillRepositoryTest extends TestCase
*/
public function testGetPossiblyRelatedJournals()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $bill1 = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $bill1->amount_min = 100;
+ $bill1->amount_max = 1000;
+ $account->user_id = $bill1->user_id;
+ $bill1->save();
+ $account->save();
+
+ // create some transactions to match our bill:
+ for ($i = 0; $i < 8; $i++) {
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $journal->user_id = $bill1->user_id;
+ $journal->save();
+ Transaction::create(
+ [
+ 'account_id' => $account->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => rand(101, 999),
+ ]
+ );
+ }
+ $this->be($bill1->user);
+
+ $set = $this->object->getPossiblyRelatedJournals($bill1);
+ $this->assertCount(8, $set);
}
/**
* @covers FireflyIII\Repositories\Bill\BillRepository::getRanges
- * @todo Implement testGetRanges().
*/
public function testGetRanges()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $bill1 = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $bill1->date = new Carbon('2012-01-01');
+ $bill1->repeat_freq = 'monthly';
+ $bill1->save();
+
+ $set = $this->object->getRanges($bill1, new Carbon('2012-01-01'), new Carbon('2012-12-31'));
+
+ $this->assertCount(12, $set);
}
/**
* @covers FireflyIII\Repositories\Bill\BillRepository::lastFoundMatch
- * @todo Implement testLastFoundMatch().
*/
public function testLastFoundMatch()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $journal->bill_id = $bill->id;
+ $journal->user_id = $bill->user_id;
+ $journal->save();
+
+ $this->be($bill->user);
+
+ $date = $this->object->lastFoundMatch($bill);
+
+ $this->assertEquals($journal->date->format('Y-m-d'), $date->format('Y-m-d'));
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Bill\BillRepository::lastFoundMatch
+ */
+ public function testLastFoundMatchNull()
+ {
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+
+ $this->be($bill->user);
+
+ $date = $this->object->lastFoundMatch($bill);
+
+ $this->assertNull($date);
}
/**
* @covers FireflyIII\Repositories\Bill\BillRepository::nextExpectedMatch
- * @todo Implement testNextExpectedMatch().
*/
public function testNextExpectedMatch()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $bill->date = new Carbon('2012-01-07');
+ $bill->repeat_freq = 'monthly';
+ $bill->save();
+ $this->be($bill->user);
+
+ // journal:
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $journal->date = Carbon::now()->format('Y-m-d');
+ $journal->user_id = $bill->user_id;
+ $journal->bill_id = $bill->id;
+ $journal->save();
+
+ $next = $this->object->nextExpectedMatch($bill);
+ $today = Carbon::now()->endOfMonth()->addDay();
+ $this->assertEquals($today->format('Y-m-d'), $next->format('Y-m-d'));
+
+
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Bill\BillRepository::nextExpectedMatch
+ */
+ public function testNextExpectedMatchInactive()
+ {
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $bill->active = 0;
+ $bill->save();
+ $this->be($bill->user);
+
+ $this->assertNull($this->object->nextExpectedMatch($bill));
+
+
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Bill\BillRepository::nextExpectedMatch
+ */
+ public function testNextExpectedMatchNoJournals()
+ {
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $bill->date = new Carbon('2012-01-07');
+ $bill->repeat_freq = 'monthly';
+ $bill->save();
+
+ $this->be($bill->user);
+
+ $next = $this->object->nextExpectedMatch($bill);
+ $today = Carbon::now()->startOfMonth();
+ $this->assertEquals($today->format('Y-m-d'), $next->format('Y-m-d'));
+
+
+ }
+
+
+ /**
+ * One
+ *
+ * @covers FireflyIII\Repositories\Bill\BillRepository::scan
+ */
+ public function testScanMatch()
+ {
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $bill->date = new Carbon('2012-01-07');
+ $bill->repeat_freq = 'monthly';
+ $bill->match = 'jemoeder';
+ $bill->amount_min = 90;
+ $bill->amount_max = 110;
+ $bill->save();
+ $this->be($bill->user);
+
+ // journal:
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $journal->date = Carbon::now()->format('Y-m-d');
+ $journal->description = 'jemoeder';
+ $journal->user_id = $bill->user_id;
+ $journal->save();
+
+ // two transactions:
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');
+ Transaction::create(
+ [
+ 'account_id' => $account1->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => 100,
+ ]
+ );
+ Transaction::create(
+ [
+ 'account_id' => $account2->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => 100,
+ ]
+ );
+
+ $this->object->scan($bill, $journal);
+ $newJournal = TransactionJournal::find($journal->id);
+
+ $this->assertEquals($bill->id, $newJournal->bill_id);
+ }
+
+ /**
+ * @covers FireflyIII\Repositories\Bill\BillRepository::scan
+ */
+ public function testScanNoMatch()
+ {
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $bill->date = new Carbon('2012-01-07');
+ $bill->repeat_freq = 'monthly';
+ $bill->save();
+ $this->be($bill->user);
+
+ // journal:
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $journal->date = Carbon::now()->format('Y-m-d');
+ $journal->user_id = $bill->user_id;
+ $journal->save();
+
+ // two transactions:
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');
+ Transaction::create(
+ [
+ 'account_id' => $account1->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => 100,
+ ]
+ );
+ Transaction::create(
+ [
+ 'account_id' => $account2->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => 100,
+ ]
);
+
+ $this->object->scan($bill, $journal);
+ $newJournal = TransactionJournal::find($journal->id);
+
+ $this->assertNull($newJournal->bill_id);
}
/**
* @covers FireflyIII\Repositories\Bill\BillRepository::scan
- * @todo Implement testScan().
*/
- public function testScan()
+ public function testScanNoMatchButAttached()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $bill->date = new Carbon('2012-01-07');
+ $bill->match = 'blablabla';
+ $bill->repeat_freq = 'monthly';
+ $bill->save();
+ $this->be($bill->user);
+
+ // journal:
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $journal->date = Carbon::now()->format('Y-m-d');
+ $journal->user_id = $bill->user_id;
+ $journal->bill_id = $bill->id;
+ $journal->save();
+
+ // two transactions:
+ $account1 = FactoryMuffin::create('FireflyIII\Models\Account');
+ $account2 = FactoryMuffin::create('FireflyIII\Models\Account');
+ Transaction::create(
+ [
+ 'account_id' => $account1->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => 100,
+ ]
+ );
+ Transaction::create(
+ [
+ 'account_id' => $account2->id,
+ 'transaction_journal_id' => $journal->id,
+ 'amount' => 100,
+ ]
);
+
+ $this->object->scan($bill, $journal);
+ $newJournal = TransactionJournal::find($journal->id);
+
+ $this->assertNull($newJournal->bill_id);
}
/**
* @covers FireflyIII\Repositories\Bill\BillRepository::store
- * @todo Implement testStore().
*/
public function testStore()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $data = [
+ 'name' => 'Something',
+ 'match' => 'Something',
+ 'amount_min' => 100,
+ 'user' => $user->id,
+ 'amount_max' => 110,
+ 'date' => new Carbon,
+ 'repeat_freq' => 'monthly',
+ 'skip' => 0,
+ 'automatch' => 1,
+ 'active' => 1,
+
+ ];
+ $bill = $this->object->store($data);
+
+ $this->assertEquals($data['name'], $bill->name);
}
/**
@@ -218,9 +443,22 @@ class BillRepositoryTest extends TestCase
*/
public function testUpdate()
{
- // Remove the following lines when you implement this test.
- $this->markTestIncomplete(
- 'This test has not been implemented yet.'
- );
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $data = [
+ 'name' => 'new Name',
+ 'match' => $bill->match,
+ 'amount_min' => 100,
+ 'amount_max' => 110,
+ 'date' => new Carbon,
+ 'repeat_freq' => 'monthly',
+ 'skip' => 0,
+ 'automatch' => 1,
+ 'active' => 1,
+
+ ];
+ $newBill = $this->object->update($bill, $data);
+
+ $this->assertEquals($data['name'], $newBill->name);
+ $this->assertEquals($bill->match, $newBill->match);
}
}