Covered transaction controller.

This commit is contained in:
James Cole 2015-05-05 07:28:04 +02:00
parent 3176e54614
commit 59d2bf3f79
5 changed files with 119 additions and 20 deletions

View File

@ -12,12 +12,12 @@ use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Input;
use Log;
use Redirect;
use Response;
use Session;
use URL;
use View;
use Log;
/**
* Class TransactionController
@ -263,7 +263,6 @@ class TransactionController extends Controller
public function store(JournalFormRequest $request, JournalRepositoryInterface $repository)
{
$journalData = $request->getJournalData();
$journal = $repository->store($journalData);
@ -272,11 +271,7 @@ class TransactionController extends Controller
// ConnectJournalToPiggyBank
event(new JournalCreated($journal, intval($request->get('piggy_bank_id'))));
if (intval($request->get('reminder_id')) > 0) {
$reminder = Auth::user()->reminders()->find($request->get('reminder_id'));
$reminder->active = 0;
$reminder->save();
}
$repository->deactivateReminder($request->get('reminder_id'));
Session::flash('success', 'New transaction "' . $journal->description . '" stored!');
@ -292,6 +287,8 @@ class TransactionController extends Controller
}
/**
* @param JournalFormRequest $request
* @param JournalRepositoryInterface $repository

View File

@ -57,7 +57,7 @@ class JournalFormRequest extends Request
$rules = [
'description' => 'required|min:1,max:255',
'what' => 'required|in:withdrawal,deposit,transfer|exists:transaction_types,type',
'what' => 'required|in:withdrawal,deposit,transfer',
'amount' => 'numeric|required|min:0.01',
'date' => 'required|date',
'reminder_id' => 'numeric|exists:reminders,id',

View File

@ -26,6 +26,21 @@ use Log;
class JournalRepository implements JournalRepositoryInterface
{
/**
* @param int $id
*
* @return bool
*/
public function deactivateReminder($id)
{
$reminder = Auth::user()->reminders()->find($id);
if ($reminder) {
$reminder->active = 0;
$reminder->save();
}
}
/**
* @param TransactionJournal $journal
*
@ -69,7 +84,7 @@ class JournalRepository implements JournalRepositoryInterface
->where('transaction_journals.order', '>=', $journal->order)
->where('transaction_journals.id', '!=', $journal->id)
->sum('transactions.amount')
);
);
}
/**

View File

@ -16,6 +16,13 @@ use Illuminate\Support\Collection;
*/
interface JournalRepositoryInterface
{
/**
* @param int $id
*
* @return bool
*/
public function deactivateReminder($id);
/**
* @param TransactionJournal $journal
*
@ -30,14 +37,6 @@ interface JournalRepositoryInterface
*/
public function first();
/**
* @param $id
* @param Carbon $date
*
* @return TransactionJournal
*/
public function getWithDate($id, Carbon $date);
/**
* @param TransactionJournal $journal
* @param Transaction $transaction
@ -69,6 +68,14 @@ interface JournalRepositoryInterface
*/
public function getTransactionType($type);
/**
* @param $id
* @param Carbon $date
*
* @return TransactionJournal
*/
public function getWithDate($id, Carbon $date);
/**
* @param TransactionJournal $journal
* @param array $array

View File

@ -15,7 +15,7 @@ class TransactionControllerTest extends TestCase
public function setUp()
{
parent::setUp();
FactoryMuffin::create('FireflyIII\User');
}
/**
@ -227,12 +227,92 @@ class TransactionControllerTest extends TestCase
public function testStore()
{
$this->markTestIncomplete();
$account = FactoryMuffin::create('FireflyIII\Models\Account');
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
$this->be($account->user);
$data = [
'reminder_id' => '',
'what' => 'withdrawal',
'description' => 'Bla bla bla',
'account_id' => $account->id,
'expense_account' => 'Bla bla',
'amount' => '100',
'amount_currency_id' => $currency->id,
'date' => '2015-05-05',
'budget_id' => '0',
'create_another' => '1',
'category' => '',
'tags' => '',
'piggy_bank_id' => '0',
'_token' => 'replaceMe',
];
// mock!
$repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface');
// fake!
$repository->shouldReceive('store')->andReturn($journal);
$repository->shouldReceive('deactivateReminder')->andReturnNull();
$this->call('POST', '/transactions/store/withdrawal', $data);
//$this->assertSessionHas('errors','bla');
$this->assertResponseStatus(302);
$this->assertSessionHas('success');
}
public function testUpdate()
{
$this->markTestIncomplete();
$account = FactoryMuffin::create('FireflyIII\Models\Account');
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
$journal = FactoryMuffin::create('FireflyIII\Models\TransactionJournal');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
FactoryMuffin::create('FireflyIII\Models\TransactionType');
$this->be($journal->user);
$account->user_id = $journal->user_id;
$account->save();
$data = [
'_token' => 'replaceMe',
'id' => $journal->id,
'what' => 'withdrawal',
'description' => 'LunchX',
'account_id' => $account->id,
'expense_account' => 'Lunch House',
'amount' => '4.72',
'amount_currency_id' => '1',
'date' => '2015-05-31',
'budget_id' => '0',
'category' => 'Lunch',
'return_to_edit' => 1,
'tags' => '',
'piggy_bank_id' => '0',
];
$this->call('POST', '/transactions/store/withdrawal', $data);
// mock!
$repository = $this->mock('FireflyIII\Repositories\Journal\JournalRepositoryInterface');
// fake!
$repository->shouldReceive('update')->andReturn($journal);
$this->call('POST', '/transaction/update/' . $journal->id, $data);
//$this->assertSessionHas('errors','bla');
$this->assertResponseStatus(302);
$this->assertSessionHas('success');
}
}