Skip to content

Firefly III: a personal finances manager

Covered transaction controller.

James Cole59d2bf3f7962
diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php
index 08685fdee9..2006dfa2f0 100644
--- a/app/Http/Controllers/TransactionController.php
+++ b/app/Http/Controllers/TransactionController.php
@@ -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
diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php
index 1bae744518..47db30f2b9 100644
--- a/app/Http/Requests/JournalFormRequest.php
+++ b/app/Http/Requests/JournalFormRequest.php
@@ -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',
diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php
index f1291545ae..74d0c5b88a 100644
--- a/app/Repositories/Journal/JournalRepository.php
+++ b/app/Repositories/Journal/JournalRepository.php
@@ -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')
-);
+ );
}
/**
diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php
index 6bb340283b..5a7776ccdf 100644
--- a/app/Repositories/Journal/JournalRepositoryInterface.php
+++ b/app/Repositories/Journal/JournalRepositoryInterface.php
@@ -17,26 +17,25 @@ use Illuminate\Support\Collection;
interface JournalRepositoryInterface
{
/**
- * @param TransactionJournal $journal
+ * @param int $id
*
* @return bool
*/
- public function delete(TransactionJournal $journal);
+ public function deactivateReminder($id);
/**
- * Get users first transaction journal
+ * @param TransactionJournal $journal
*
- * @return TransactionJournal
+ * @return bool
*/
- public function first();
+ public function delete(TransactionJournal $journal);
/**
- * @param $id
- * @param Carbon $date
+ * Get users first transaction journal
*
* @return TransactionJournal
*/
- public function getWithDate($id, Carbon $date);
+ public function first();
/**
* @param TransactionJournal $journal
@@ -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
diff --git a/tests/controllers/TransactionControllerTest.php b/tests/controllers/TransactionControllerTest.php
index aa79e1dc49..b1780446f1 100644
--- a/tests/controllers/TransactionControllerTest.php
+++ b/tests/controllers/TransactionControllerTest.php
@@ -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');
+
+
}
}
\ No newline at end of file