Skip to content

Firefly III: a personal finances manager

Covered the JSON controller.

James Colec37e9a446776
diff --git a/app/Http/Controllers/JsonController.php b/app/Http/Controllers/JsonController.php
index 2ec220a00d..abbc81b0b4 100644
--- a/app/Http/Controllers/JsonController.php
+++ b/app/Http/Controllers/JsonController.php
@@ -10,6 +10,8 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
+use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
+use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
use Preferences;
use Response;
@@ -32,8 +34,8 @@ class JsonController extends Controller
*/
public function boxBillsPaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{
- $start = Session::get('start');
- $end = Session::get('end');
+ $start = Session::get('start', Carbon::now()->startOfMonth());
+ $end = Session::get('end', Carbon::now()->endOfMonth());
$amount = 0;
// these two functions are the same as the chart TODO
@@ -77,12 +79,14 @@ class JsonController extends Controller
public function boxBillsUnpaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{
$amount = 0;
+ $start = Session::get('start', Carbon::now()->startOfMonth());
+ $end = Session::get('end', Carbon::now()->endOfMonth());
$bills = $repository->getActiveBills();
$unpaid = new Collection; // bills
/** @var Bill $bill */
foreach ($bills as $bill) {
- $ranges = $repository->getRanges($bill, clone Session::get('start'), clone Session::get('end'));
+ $ranges = $repository->getRanges($bill, $start, $end);
foreach ($ranges as $range) {
$journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']);
@@ -121,8 +125,8 @@ class JsonController extends Controller
*/
public function boxIn(ReportQueryInterface $reportQuery)
{
- $start = Session::get('start');
- $end = Session::get('end');
+ $start = Session::get('start', Carbon::now()->startOfMonth());
+ $end = Session::get('end', Carbon::now()->endOfMonth());
$amount = $reportQuery->incomeByPeriod($start, $end, true)->sum('queryAmount');
return Response::json(['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]);
@@ -135,8 +139,8 @@ class JsonController extends Controller
*/
public function boxOut(ReportQueryInterface $reportQuery)
{
- $start = Session::get('start');
- $end = Session::get('end');
+ $start = Session::get('start', Carbon::now()->startOfMonth());
+ $end = Session::get('end', Carbon::now()->endOfMonth());
$amount = $reportQuery->journalsByExpenseAccount($start, $end, true)->sum('queryAmount');
return Response::json(['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]);
@@ -147,13 +151,14 @@ class JsonController extends Controller
*
* @return \Illuminate\Http\JsonResponse
*/
- public function categories()
+ public function categories(CategoryRepositoryInterface $repository)
{
- $list = Auth::user()->categories()->orderBy('name', 'ASC')->get();
+ $list = $repository->getCategories();
$return = [];
foreach ($list as $entry) {
$return[] = $entry->name;
}
+ sort($return);
return Response::json($return);
}
@@ -163,9 +168,9 @@ class JsonController extends Controller
*
* @return \Illuminate\Http\JsonResponse
*/
- public function expenseAccounts()
+ public function expenseAccounts(AccountRepositoryInterface $accountRepository)
{
- $list = Auth::user()->accounts()->orderBy('accounts.name', 'ASC')->accountTypeIn(['Expense account', 'Beneficiary account'])->get();
+ $list = $accountRepository->getAccounts(['Expense account', 'Beneficiary account']);
$return = [];
foreach ($list as $entry) {
$return[] = $entry->name;
@@ -178,9 +183,9 @@ class JsonController extends Controller
/**
* @return \Illuminate\Http\JsonResponse
*/
- public function revenueAccounts()
+ public function revenueAccounts(AccountRepositoryInterface $accountRepository)
{
- $list = Auth::user()->accounts()->accountTypeIn(['Revenue account'])->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
+ $list = $accountRepository->getAccounts(['Revenue account']);
$return = [];
foreach ($list as $entry) {
$return[] = $entry->name;
@@ -218,13 +223,12 @@ class JsonController extends Controller
*
* @return \Symfony\Component\HttpFoundation\Response
*/
- public function transactionJournals($what)
+ public function transactionJournals($what, JournalRepositoryInterface $repository)
{
$descriptions = [];
- $dbType = TransactionType::whereType($what)->first();
- $journals = Auth::user()->transactionjournals()->where('transaction_type_id', $dbType->id)
- ->orderBy('id', 'DESC')->take(50)
- ->get();
+ $dbType = $repository->getTransactionType($what);
+
+ $journals = $repository->getJournalsOfType($dbType);
foreach ($journals as $j) {
$descriptions[] = $j->description;
}
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index eb1c8db19d..f8732d7867 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -62,6 +62,26 @@ class JournalRepository implements JournalRepositoryInterface
return $journal->transactions()->first()->account_id;
}
+ /**
+ * @param TransactionType $dbType
+ *
+ * @return Collection
+ */
+ public function getJournalsOfType(TransactionType $dbType)
+ {
+ return Auth::user()->transactionjournals()->where('transaction_type_id', $dbType->id)->orderBy('id', 'DESC')->take(50)->get();
+ }
+
+ /**
+ * @param $type
+ *
+ * @return TransactionType
+ */
+ public function getTransactionType($type)
+ {
+ return TransactionType::whereType($type)->first();
+ }
+
/**
* @param string $query
* @param TransactionJournal $journal
@@ -289,5 +309,4 @@ class JournalRepository implements JournalRepositoryInterface
return [$from, $to];
}
-
}
diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php
index cbe44671a5..02febb7b96 100644
--- a/app/Repositories/Journal/JournalRepositoryInterface.php
+++ b/app/Repositories/Journal/JournalRepositoryInterface.php
@@ -4,6 +4,7 @@ namespace FireflyIII\Repositories\Journal;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection;
/**
@@ -13,6 +14,13 @@ use Illuminate\Support\Collection;
*/
interface JournalRepositoryInterface
{
+ /**
+ * Get users first transaction journal
+ *
+ * @return TransactionJournal
+ */
+ public function first();
+
/**
*
* Get the account_id, which is the asset account that paid for the transaction.
@@ -23,6 +31,13 @@ interface JournalRepositoryInterface
*/
public function getAssetAccount(TransactionJournal $journal);
+ /**
+ * @param TransactionType $dbType
+ *
+ * @return Collection
+ */
+ public function getJournalsOfType(TransactionType $dbType);
+
/**
* @param string $query
* @param TransactionJournal $journal
@@ -31,6 +46,13 @@ interface JournalRepositoryInterface
*/
public function searchRelated($query, TransactionJournal $journal);
+
+ /**
+ * @param $type
+ *
+ * @return TransactionType
+ */
+ public function getTransactionType($type);
/**
* @param array $data
*
@@ -45,11 +67,4 @@ interface JournalRepositoryInterface
* @return mixed
*/
public function update(TransactionJournal $journal, array $data);
-
- /**
- * Get users first transaction journal
- *
- * @return TransactionJournal
- */
- public function first();
}
diff --git a/tests/controllers/JsonControllerTest.php b/tests/controllers/JsonControllerTest.php
new file mode 100644
index 0000000000..4a902079f6
--- /dev/null
+++ b/tests/controllers/JsonControllerTest.php
@@ -0,0 +1,206 @@
+<?php
+
+use Carbon\Carbon;
+use Illuminate\Support\Collection;
+use League\FactoryMuffin\Facade as FactoryMuffin;
+
+/**
+ * Class JsonControllerTest
+ */
+class JsonControllerTest extends TestCase
+{
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ */
+ public function setUp()
+ {
+ parent::setUp();
+ }
+
+ /**
+ * This method is called before the first test of this test class is run.
+ *
+ * @since Method available since Release 3.4.0
+ */
+ public static function setUpBeforeClass()
+ {
+ parent::setUpBeforeClass();
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ */
+ public function tearDown()
+ {
+ parent::tearDown();
+
+ }
+
+ public function testBoxBillsPaid()
+ {
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $cc = FactoryMuffin::create('FireflyIII\Models\Account');
+ $ccs = new Collection([$cc]);
+ $collection = new Collection([$bill]);
+ $ranges = [['start' => new Carbon, 'end' => new Carbon]];
+ $this->be($bill->user);
+
+ $bills = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface');
+ $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
+
+ // mock!
+ $bills->shouldReceive('getActiveBills')->andReturn($collection);
+ $bills->shouldReceive('getRanges')->andReturn($ranges);
+ $bills->shouldReceive('getJournalsInRange')->andReturn(new Collection);
+ $accounts->shouldReceive('getCreditCards')->andReturn($ccs);
+ $accounts->shouldReceive('getTransfersInRange')->andReturn(new Collection);
+ Amount::shouldReceive('format')->andReturn('xx');
+ Steam::shouldReceive('balance')->andReturn(0);
+
+
+ $this->call('GET', '/json/box/bills-paid');
+ $this->assertResponseOk();
+
+ }
+
+ public function testBoxBillsUnpaid()
+ {
+ $bill = FactoryMuffin::create('FireflyIII\Models\Bill');
+ $cc = FactoryMuffin::create('FireflyIII\Models\Account');
+ $ccs = new Collection([$cc]);
+ $collection = new Collection([$bill]);
+ $ranges = [['start' => new Carbon, 'end' => new Carbon]];
+ $this->be($bill->user);
+
+ $bills = $this->mock('FireflyIII\Repositories\Bill\BillRepositoryInterface');
+ $accounts = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
+
+ // mock!
+ $bills->shouldReceive('getActiveBills')->andReturn($collection);
+ $bills->shouldReceive('getRanges')->andReturn($ranges);
+ $bills->shouldReceive('getJournalsInRange')->andReturn(new Collection);
+ $bills->shouldReceive('createFakeBill')->andReturn($bill);
+ $accounts->shouldReceive('getCreditCards')->andReturn($ccs);
+ Amount::shouldReceive('format')->andReturn('xx');
+ Steam::shouldReceive('balance')->andReturn(-1);
+
+ $this->call('GET', '/json/box/bills-unpaid');
+ $this->assertResponseOk();
+ }
+
+ public function testBoxIn()
+ {
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $this->be($user);
+
+ $repository = $this->mock('FireflyIII\Helpers\Report\ReportQueryInterface');
+ $repository->shouldReceive('incomeByPeriod')->andReturn(new Collection);
+ Amount::shouldReceive('format')->andReturn('xx');
+
+ $this->call('GET', '/json/box/in');
+ $this->assertResponseOk();
+ }
+
+ public function testBoxOut()
+ {
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $this->be($user);
+
+ $repository = $this->mock('FireflyIII\Helpers\Report\ReportQueryInterface');
+ $repository->shouldReceive('journalsByExpenseAccount')->andReturn(new Collection);
+ Amount::shouldReceive('format')->andReturn('xx');
+
+ $this->call('GET', '/json/box/out');
+ $this->assertResponseOk();
+ }
+
+ public function testCategories()
+ {
+ $category = FactoryMuffin::create('FireflyIII\Models\Category');
+ $this->be($category->user);
+ $categories = new Collection([$category]);
+
+ $repository = $this->mock('FireflyIII\Repositories\Category\CategoryRepositoryInterface');
+ $repository->shouldReceive('getCategories')->andReturn($categories);
+
+ $this->call('GET', '/json/categories');
+ $this->assertResponseOk();
+ }
+
+ public function testExpenseAccounts()
+ {
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $this->be($account->user);
+ $accounts = new Collection([$account]);
+
+ $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
+ $repository->shouldReceive('getAccounts')->with(['Expense account', 'Beneficiary account'])->andReturn($accounts);
+
+ $this->call('GET', '/json/expense-accounts');
+ $this->assertResponseOk();
+ }
+
+ public function testRevenueAccounts()
+ {
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $this->be($account->user);
+ $accounts = new Collection([$account]);
+
+ $repository = $this->mock('FireflyIII\Repositories\Account\AccountRepositoryInterface');
+ $repository->shouldReceive('getAccounts')->with(['Revenue account'])->andReturn($accounts);
+
+ $this->call('GET', '/json/revenue-accounts');
+ $this->assertResponseOk();
+ }
+
+ public function testSetSharedReports()
+ {
+ $pref = FactoryMuffin::create('FireflyIII\Models\Preference');
+ $pref->data = false;
+ $pref->save();
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $this->be($user);
+
+ Preferences::shouldReceive('get')->withArgs(['showSharedReports', false])->andReturn($pref);
+ Preferences::shouldReceive('set')->withArgs(['showSharedReports', true]);
+
+ $this->call('GET', '/json/show-shared-reports/set');
+ $this->assertResponseOk();
+ }
+
+
+ public function testShowSharedReports()
+ {
+ $pref = FactoryMuffin::create('FireflyIII\Models\Preference');
+ $pref->data = false;
+ $pref->save();
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $this->be($user);
+
+ Preferences::shouldReceive('get')->withArgs(['showSharedReports', false])->andReturn($pref);
+
+
+ $this->call('GET', '/json/show-shared-reports');
+ $this->assertResponseOk();
+ }
+
+ public function testTransactionJournals()
+ {
+ $journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
+ $type = FactoryMuffin::create('FireflyIII\Models\TransactionType');
+ $collection = new Collection([$journal]);
+ $user = FactoryMuffin::create('FireflyIII\User');
+ $this->be($user);
+
+ $repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface');
+ $repository->shouldReceive('getTransactionType')->with('withdrawal')->andReturn($type);
+ $repository->shouldReceive('getJournalsOfType')->with($type)->andReturn($collection);
+
+
+ $this->call('GET', '/json/transaction-journals/withdrawal');
+ $this->assertResponseOk();
+ }
+
+}
\ No newline at end of file