Skip to content

Firefly III: a personal finances manager

Included full home controller tests.

James Cole8f9b1b866b9e
diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php
index 202cb7ea18..1824d01039 100644
--- a/app/Http/Controllers/HomeController.php
+++ b/app/Http/Controllers/HomeController.php
@@ -32,6 +32,9 @@ class HomeController extends Controller
Session::put('end', $end);
}
+ /**
+ * @return \Illuminate\Http\RedirectResponse
+ */
public function flush()
{
Session::clear();
@@ -58,7 +61,7 @@ class HomeController extends Controller
$savings = $repository->getSavingsAccounts();
// check if all books are correct.
- $sum = floatval(Auth::user()->transactions()->sum('amount'));
+ $sum = $repository->sumOfEverything();
if ($sum != 0) {
Session::flash(
'error', 'Your transactions are unbalanced. This means a'
diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php
index ff4474635c..50e4004f15 100644
--- a/app/Repositories/Account/AccountRepository.php
+++ b/app/Repositories/Account/AccountRepository.php
@@ -151,6 +151,13 @@ class AccountRepository implements AccountRepositoryInterface
->get(['transaction_journals.*', 'transaction_currencies.symbol', 'transaction_types.type']);
}
+ /**
+ * @return float
+ */
+ public function sumOfEverything() {
+ return floatval(Auth::user()->transactions()->sum('amount'));
+ }
+
/**
* @param Account $account
* @param int $page
diff --git a/app/Repositories/Account/AccountRepositoryInterface.php b/app/Repositories/Account/AccountRepositoryInterface.php
index 7040e9ff9b..a8dad3c2eb 100644
--- a/app/Repositories/Account/AccountRepositoryInterface.php
+++ b/app/Repositories/Account/AccountRepositoryInterface.php
@@ -94,6 +94,11 @@ interface AccountRepositoryInterface
*/
public function getLastActivity(Account $account);
+ /**
+ * @return float
+ */
+ public function sumOfEverything();
+
/**
* Get savings accounts and the balance difference in the period.
*
diff --git a/tests/controllers/HelpControllerTest.php b/tests/controllers/HelpControllerTest.php
index 1bacdbdcb0..849fb45180 100644
--- a/tests/controllers/HelpControllerTest.php
+++ b/tests/controllers/HelpControllerTest.php
@@ -2,6 +2,9 @@
use League\FactoryMuffin\Facade as FactoryMuffin;
+/**
+ * Class HelpControllerTest
+ */
class HelpControllerTest extends TestCase
{
/**
@@ -82,7 +85,6 @@ class HelpControllerTest extends TestCase
{
// login
$user = FactoryMuffin::create('FireflyIII\User');
- $content = ['title' => 'Bla', 'text' => 'Bla'];
$this->be($user);
// mock some stuff.
diff --git a/tests/controllers/HomeControllerTest.php b/tests/controllers/HomeControllerTest.php
index 3e98aa6f64..aff74fa2e2 100644
--- a/tests/controllers/HomeControllerTest.php
+++ b/tests/controllers/HomeControllerTest.php
@@ -1,7 +1,10 @@
<?php
+use Illuminate\Support\Collection;
+use League\FactoryMuffin\Facade as FactoryMuffin;
+
/**
- * Generated by PHPUnit_SkeletonGenerator on 2015-03-08 at 20:05:14.
+ * Class HomeControllerTest
*/
class HomeControllerTest extends TestCase
{
@@ -13,7 +16,6 @@ class HomeControllerTest extends TestCase
public function setUp()
{
parent::setUp();
- Artisan::call('migrate');
}
/**
@@ -32,7 +34,8 @@ class HomeControllerTest extends TestCase
{
$start = '2015-03-01';
$end = '2015-03-31';
- $this->be(new FireflyIII\User);
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $this->be($user);
$this->call('POST', '/daterange', ['end' => $end, 'start' => $start, '_token' => 'replaceme']);
@@ -43,6 +46,7 @@ class HomeControllerTest extends TestCase
}
+
/**
* @covers FireflyIII\Http\Controllers\HomeController::dateRange
*/
@@ -50,36 +54,60 @@ class HomeControllerTest extends TestCase
{
$start = '2014-03-01';
$end = '2015-03-31';
- $this->be(new FireflyIII\User);
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $this->be($user);
+
$this->call('POST', '/daterange', ['end' => $end, 'start' => $start, '_token' => 'replaceme']);
$this->assertResponseOk();
$this->assertSessionHas('start');
$this->assertSessionHas('end');
$this->assertSessionHas('warning');
-
}
/**
- * @covers FireflyIII\Http\Controllers\HomeController::index
+ *
*/
- public function testIndexLoggedIn()
+ public function testFlush()
{
- $this->be(new FireflyIII\User);
- Amount::shouldReceive('getCurrencyCode')->andReturn('EUR');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $this->be($user);
- $response = $this->call('GET', '/');
- $this->assertResponseOk();
+ $this->call('GET', '/flush');
+ $this->assertResponseStatus(302);
}
/**
* @covers FireflyIII\Http\Controllers\HomeController::index
*/
- public function testIndexNoLogin()
+ public function testIndex()
{
- $response = $this->call('GET', '/');
- $this->assertRedirectedTo('auth/login');
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $preference = FactoryMuffin::create('FireflyIII\Models\Preference');
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $journals = new Collection([$journal]);
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $accounts = new Collection([$account]);
+ $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
+
+ $this->be($user);
+
+ // mock ALL THE THINGS!
+ $repository->shouldReceive('countAccounts')->once()->andReturn(3);
+ Preferences::shouldReceive('get')->once()->withArgs(['frontPageAccounts', []])->andReturn($preference);
+ $repository->shouldReceive('getFrontpageAccounts')->once()->with($preference)->andReturn($accounts);
+ $repository->shouldReceive('getSavingsAccounts')->once()->andReturn($accounts);
+ $repository->shouldReceive('sumOfEverything')->once()->andReturn(1);
+ $repository->shouldReceive('getFrontpageTransactions')->once()->andReturn($journals);
+
+
+ Amount::shouldReceive('getCurrencyCode')->andReturn('EUR');
+ Amount::shouldReceive('format')->andReturn('xxx');
+ Amount::shouldReceive('formatJournal')->with($journal)->andReturn('xxx');
+
+ $this->call('GET', '/');
+ $this->assertResponseOk();
}