Covered the JSON controller.

This commit is contained in:
James Cole
2015-04-11 19:59:41 +02:00
parent 6157d82a0b
commit c37e9a4467
4 changed files with 270 additions and 26 deletions

View File

@@ -10,6 +10,8 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Preferences; use Preferences;
use Response; use Response;
@@ -32,8 +34,8 @@ class JsonController extends Controller
*/ */
public function boxBillsPaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository) public function boxBillsPaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{ {
$start = Session::get('start'); $start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end'); $end = Session::get('end', Carbon::now()->endOfMonth());
$amount = 0; $amount = 0;
// these two functions are the same as the chart TODO // 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) public function boxBillsUnpaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
{ {
$amount = 0; $amount = 0;
$start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end', Carbon::now()->endOfMonth());
$bills = $repository->getActiveBills(); $bills = $repository->getActiveBills();
$unpaid = new Collection; // bills $unpaid = new Collection; // bills
/** @var Bill $bill */ /** @var Bill $bill */
foreach ($bills as $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) { foreach ($ranges as $range) {
$journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']); $journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']);
@@ -121,8 +125,8 @@ class JsonController extends Controller
*/ */
public function boxIn(ReportQueryInterface $reportQuery) public function boxIn(ReportQueryInterface $reportQuery)
{ {
$start = Session::get('start'); $start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end'); $end = Session::get('end', Carbon::now()->endOfMonth());
$amount = $reportQuery->incomeByPeriod($start, $end, true)->sum('queryAmount'); $amount = $reportQuery->incomeByPeriod($start, $end, true)->sum('queryAmount');
return Response::json(['box' => 'in', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]); 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) public function boxOut(ReportQueryInterface $reportQuery)
{ {
$start = Session::get('start'); $start = Session::get('start', Carbon::now()->startOfMonth());
$end = Session::get('end'); $end = Session::get('end', Carbon::now()->endOfMonth());
$amount = $reportQuery->journalsByExpenseAccount($start, $end, true)->sum('queryAmount'); $amount = $reportQuery->journalsByExpenseAccount($start, $end, true)->sum('queryAmount');
return Response::json(['box' => 'out', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]); 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 * @return \Illuminate\Http\JsonResponse
*/ */
public function categories() public function categories(CategoryRepositoryInterface $repository)
{ {
$list = Auth::user()->categories()->orderBy('name', 'ASC')->get(); $list = $repository->getCategories();
$return = []; $return = [];
foreach ($list as $entry) { foreach ($list as $entry) {
$return[] = $entry->name; $return[] = $entry->name;
} }
sort($return);
return Response::json($return); return Response::json($return);
} }
@@ -163,9 +168,9 @@ class JsonController extends Controller
* *
* @return \Illuminate\Http\JsonResponse * @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 = []; $return = [];
foreach ($list as $entry) { foreach ($list as $entry) {
$return[] = $entry->name; $return[] = $entry->name;
@@ -178,9 +183,9 @@ class JsonController extends Controller
/** /**
* @return \Illuminate\Http\JsonResponse * @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 = []; $return = [];
foreach ($list as $entry) { foreach ($list as $entry) {
$return[] = $entry->name; $return[] = $entry->name;
@@ -218,13 +223,12 @@ class JsonController extends Controller
* *
* @return \Symfony\Component\HttpFoundation\Response * @return \Symfony\Component\HttpFoundation\Response
*/ */
public function transactionJournals($what) public function transactionJournals($what, JournalRepositoryInterface $repository)
{ {
$descriptions = []; $descriptions = [];
$dbType = TransactionType::whereType($what)->first(); $dbType = $repository->getTransactionType($what);
$journals = Auth::user()->transactionjournals()->where('transaction_type_id', $dbType->id)
->orderBy('id', 'DESC')->take(50) $journals = $repository->getJournalsOfType($dbType);
->get();
foreach ($journals as $j) { foreach ($journals as $j) {
$descriptions[] = $j->description; $descriptions[] = $j->description;
} }

View File

@@ -62,6 +62,26 @@ class JournalRepository implements JournalRepositoryInterface
return $journal->transactions()->first()->account_id; 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 string $query
* @param TransactionJournal $journal * @param TransactionJournal $journal
@@ -289,5 +309,4 @@ class JournalRepository implements JournalRepositoryInterface
return [$from, $to]; return [$from, $to];
} }
} }

View File

@@ -4,6 +4,7 @@ namespace FireflyIII\Repositories\Journal;
use FireflyIII\Models\Transaction; use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
/** /**
@@ -13,6 +14,13 @@ use Illuminate\Support\Collection;
*/ */
interface JournalRepositoryInterface 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. * 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); public function getAssetAccount(TransactionJournal $journal);
/**
* @param TransactionType $dbType
*
* @return Collection
*/
public function getJournalsOfType(TransactionType $dbType);
/** /**
* @param string $query * @param string $query
* @param TransactionJournal $journal * @param TransactionJournal $journal
@@ -31,6 +46,13 @@ interface JournalRepositoryInterface
*/ */
public function searchRelated($query, TransactionJournal $journal); public function searchRelated($query, TransactionJournal $journal);
/**
* @param $type
*
* @return TransactionType
*/
public function getTransactionType($type);
/** /**
* @param array $data * @param array $data
* *
@@ -45,11 +67,4 @@ interface JournalRepositoryInterface
* @return mixed * @return mixed
*/ */
public function update(TransactionJournal $journal, array $data); public function update(TransactionJournal $journal, array $data);
/**
* Get users first transaction journal
*
* @return TransactionJournal
*/
public function first();
} }

View File

@@ -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();
}
}