mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Add currency tests.
This commit is contained in:
parent
22ab9ebb2f
commit
293cd72001
@ -5,12 +5,13 @@ use FireflyIII\Http\Requests;
|
||||
use FireflyIII\Http\Requests\CurrencyFormRequest;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use Input;
|
||||
use Preferences;
|
||||
use Redirect;
|
||||
use Session;
|
||||
use View;
|
||||
use URL;
|
||||
use View;
|
||||
|
||||
/**
|
||||
* Class CurrencyController
|
||||
@ -56,9 +57,7 @@ class CurrencyController extends Controller
|
||||
public function defaultCurrency(TransactionCurrency $currency)
|
||||
{
|
||||
|
||||
$currencyPreference = Preferences::get('currencyPreference', 'EUR');
|
||||
$currencyPreference->data = $currency->code;
|
||||
$currencyPreference->save();
|
||||
Preferences::set('currencyPreference', $currency->code);
|
||||
|
||||
Session::flash('success', $currency->name . ' is now the default currency.');
|
||||
Cache::forget('FFCURRENCYSYMBOL');
|
||||
@ -73,17 +72,18 @@ class CurrencyController extends Controller
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
|
||||
*/
|
||||
public function delete(TransactionCurrency $currency)
|
||||
public function delete(TransactionCurrency $currency, CurrencyRepositoryInterface $repository)
|
||||
{
|
||||
if ($currency->transactionJournals()->count() > 0) {
|
||||
Session::flash('error', 'Cannot delete ' . e($currency->name) . ' because there are still transactions attached to it.');
|
||||
|
||||
// put previous url in session
|
||||
Session::put('currency.delete.url', URL::previous());
|
||||
if ($repository->countJournals($currency) > 0) {
|
||||
Session::flash('error', 'Cannot delete ' . e($currency->name) . ' because there are still transactions attached to it.');
|
||||
|
||||
return Redirect::route('currency.index');
|
||||
}
|
||||
|
||||
// put previous url in session
|
||||
Session::put('currency.delete.url', URL::previous());
|
||||
|
||||
|
||||
return view('currency.delete', compact('currency'));
|
||||
}
|
||||
@ -93,10 +93,10 @@ class CurrencyController extends Controller
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function destroy(TransactionCurrency $currency)
|
||||
public function destroy(TransactionCurrency $currency, CurrencyRepositoryInterface $repository)
|
||||
{
|
||||
if ($currency->transactionJournals()->count() > 0) {
|
||||
Session::flash('error', 'Cannot delete ' . e($currency->name) . ' because there are still transactions attached to it.');
|
||||
if ($repository->countJournals($currency) > 0) {
|
||||
Session::flash('error', 'Cannot destroy ' . e($currency->name) . ' because there are still transactions attached to it.');
|
||||
|
||||
return Redirect::route('currency.index');
|
||||
}
|
||||
@ -132,12 +132,10 @@ class CurrencyController extends Controller
|
||||
/**
|
||||
* @return \Illuminate\View\View
|
||||
*/
|
||||
public function index()
|
||||
public function index(CurrencyRepositoryInterface $repository)
|
||||
{
|
||||
$currencies = TransactionCurrency::get();
|
||||
$currencyPreference = Preferences::get('currencyPreference', 'EUR');
|
||||
$defaultCurrency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
||||
|
||||
$currencies = $repository->get();
|
||||
$defaultCurrency = $repository->getCurrencyByPreference(Preferences::get('currencyPreference', 'EUR'));
|
||||
|
||||
return view('currency.index', compact('currencies', 'defaultCurrency'));
|
||||
}
|
||||
@ -147,28 +145,22 @@ class CurrencyController extends Controller
|
||||
*
|
||||
* @return $this|\Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function store(CurrencyFormRequest $request)
|
||||
public function store(CurrencyFormRequest $request, CurrencyRepositoryInterface $repository)
|
||||
{
|
||||
$data = $request->getCurrencyData();
|
||||
$currency = $repository->store($data);
|
||||
|
||||
|
||||
// no repository, because the currency controller is relatively simple.
|
||||
$currency = TransactionCurrency::create(
|
||||
[
|
||||
'name' => $request->get('name'),
|
||||
'code' => $request->get('code'),
|
||||
'symbol' => $request->get('symbol'),
|
||||
]
|
||||
);
|
||||
|
||||
Session::flash('success', 'Currency "' . $currency->name . '" created');
|
||||
|
||||
if (intval(Input::get('create_another')) === 1) {
|
||||
Session::put('currency.create.fromStore', true);
|
||||
|
||||
return Redirect::route('currency.create')->withInput();
|
||||
}
|
||||
|
||||
// redirect to previous URL.
|
||||
return Redirect::to(Session::get('categories.create.url'));
|
||||
return Redirect::to(Session::get('currency.create.url'));
|
||||
|
||||
|
||||
}
|
||||
@ -178,19 +170,17 @@ class CurrencyController extends Controller
|
||||
*
|
||||
* @return $this|\Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function update(TransactionCurrency $currency, CurrencyFormRequest $request)
|
||||
public function update(TransactionCurrency $currency, CurrencyFormRequest $request, CurrencyRepositoryInterface $repository)
|
||||
{
|
||||
|
||||
$currency->code = $request->get('code');
|
||||
$currency->symbol = $request->get('symbol');
|
||||
$currency->name = $request->get('name');
|
||||
$currency->save();
|
||||
$data = $request->getCurrencyData();
|
||||
$currency = $repository->update($currency, $data);
|
||||
|
||||
Session::flash('success', 'Currency "' . e($currency->name) . '" updated.');
|
||||
|
||||
|
||||
if (intval(Input::get('return_to_edit')) === 1) {
|
||||
Session::put('currency.edit.fromUpdate', true);
|
||||
|
||||
return Redirect::route('currency.edit', $currency->id);
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,18 @@ class CurrencyFormRequest extends Request
|
||||
return Auth::check();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getCurrencyData()
|
||||
{
|
||||
return [
|
||||
'name' => $this->get('name'),
|
||||
'code' => $this->get('code'),
|
||||
'symbol' => $this->get('symbol'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
@ -62,6 +62,7 @@ class FireflyServiceProvider extends ServiceProvider
|
||||
$this->app->bind('FireflyIII\Repositories\Journal\JournalRepositoryInterface', 'FireflyIII\Repositories\Journal\JournalRepository');
|
||||
$this->app->bind('FireflyIII\Repositories\Bill\BillRepositoryInterface', 'FireflyIII\Repositories\Bill\BillRepository');
|
||||
$this->app->bind('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface', 'FireflyIII\Repositories\PiggyBank\PiggyBankRepository');
|
||||
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
|
||||
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search');
|
||||
|
||||
|
||||
|
84
app/Repositories/Currency/CurrencyRepository.php
Normal file
84
app/Repositories/Currency/CurrencyRepository.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Repositories\Currency;
|
||||
|
||||
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class CurrencyRepository
|
||||
*
|
||||
* @package FireflyIII\Repositories\Currency
|
||||
*/
|
||||
class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countJournals(TransactionCurrency $currency)
|
||||
{
|
||||
return $currency->transactionJournals()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
return TransactionCurrency::get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Preference $preference
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
public function getCurrencyByPreference(Preference $preference)
|
||||
{
|
||||
$preferred = TransactionCurrency::whereCode($preference->data)->first();
|
||||
if (is_null($preferred)) {
|
||||
$preferred = TransactionCurrency::first();
|
||||
}
|
||||
|
||||
return $preferred;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
$currency = TransactionCurrency::create(
|
||||
[
|
||||
'name' => $data['name'],
|
||||
'code' => $data['code'],
|
||||
'symbol' => $data['symbol'],
|
||||
]
|
||||
);
|
||||
|
||||
return $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
public function update(TransactionCurrency $currency, array $data)
|
||||
{
|
||||
$currency->code = $data['code'];
|
||||
$currency->symbol = $data['symbol'];
|
||||
$currency->name = $data['name'];
|
||||
$currency->save();
|
||||
|
||||
return $currency;
|
||||
}
|
||||
}
|
52
app/Repositories/Currency/CurrencyRepositoryInterface.php
Normal file
52
app/Repositories/Currency/CurrencyRepositoryInterface.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Repositories\Currency;
|
||||
|
||||
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Interface CurrencyRepositoryInterface
|
||||
*
|
||||
* @package FireflyIII\Repositories\Currency
|
||||
*/
|
||||
interface CurrencyRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countJournals(TransactionCurrency $currency);
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function get();
|
||||
|
||||
/**
|
||||
* @param Preference $preference
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
public function getCurrencyByPreference(Preference $preference);
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
public function store(array $data);
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
public function update(TransactionCurrency $currency, array $data);
|
||||
|
||||
}
|
217
tests/controllers/CurrencyControllerTest.php
Normal file
217
tests/controllers/CurrencyControllerTest.php
Normal file
@ -0,0 +1,217 @@
|
||||
<?php
|
||||
use Illuminate\Support\Collection;
|
||||
use League\FactoryMuffin\Facade as FactoryMuffin;
|
||||
|
||||
/**
|
||||
* Class CurrencyControllerTest
|
||||
*/
|
||||
class CurrencyControllerTest 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 testCreate()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
|
||||
$this->call('GET', '/currency/create');
|
||||
$this->assertResponseOk();
|
||||
$this->assertViewHas('subTitle', 'Create a new currency');
|
||||
$this->assertViewHas('subTitleIcon', 'fa-plus');
|
||||
|
||||
}
|
||||
|
||||
public function testDefaultCurrency()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
|
||||
$this->call('GET', '/currency/default/' . $currency->id);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success', $currency->name . ' is now the default currency.');
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$repository->shouldReceive('countJournals')->andReturn(0);
|
||||
|
||||
$this->call('GET', '/currency/delete/' . $currency->id);
|
||||
$this->assertResponseOk();
|
||||
$this->assertViewHas('currency');
|
||||
|
||||
}
|
||||
|
||||
public function testDeleteUnable()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$repository->shouldReceive('countJournals')->andReturn(1);
|
||||
|
||||
$this->call('GET', '/currency/delete/' . $currency->id);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('error');
|
||||
|
||||
}
|
||||
|
||||
public function testDestroy()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$repository->shouldReceive('countJournals')->andReturn(0);
|
||||
|
||||
$this->call('POST', '/currency/destroy/' . $currency->id, ['_token' => 'replaceMe']);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success', 'Currency "' . e($currency->name) . '" deleted');
|
||||
|
||||
}
|
||||
|
||||
public function testDestroyUnable()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$repository->shouldReceive('countJournals')->andReturn(1);
|
||||
|
||||
$this->call('POST', '/currency/destroy/' . $currency->id, ['_token' => 'replaceMe']);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('error');
|
||||
|
||||
}
|
||||
|
||||
public function testEdit()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$this->be($user);
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$repository->shouldReceive('countJournals')->andReturn(0);
|
||||
|
||||
$this->call('GET', '/currency/edit/' . $currency->id);
|
||||
$this->assertResponseOk();
|
||||
$this->assertViewHas('currency');
|
||||
}
|
||||
|
||||
public function testIndex()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
$this->be($user);
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$repository->shouldReceive('get')->andReturn(new Collection);
|
||||
$repository->shouldReceive('getCurrencyByPreference')->andReturn($currency);
|
||||
|
||||
$this->call('GET', '/currency');
|
||||
$this->assertResponseOk();
|
||||
}
|
||||
|
||||
public function testStore()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
$this->be($user);
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$request = $this->mock('FireflyIII\Http\Requests\CurrencyFormRequest');
|
||||
$request->shouldReceive('getCurrencyData')->andReturn([]);
|
||||
$repository->shouldReceive('store')->andReturn($currency);
|
||||
|
||||
$this->call('POST', '/currency/store', ['_token' => 'replaceMe']);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success');
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function testStoreAndReturn()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
$this->be($user);
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$request = $this->mock('FireflyIII\Http\Requests\CurrencyFormRequest');
|
||||
$request->shouldReceive('getCurrencyData')->andReturn([]);
|
||||
$repository->shouldReceive('store')->andReturn($currency);
|
||||
|
||||
$this->call('POST', '/currency/store', ['_token' => 'replaceMe', 'create_another' => 1]);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success');
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function testUpdate()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
$this->be($user);
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$request = $this->mock('FireflyIII\Http\Requests\CurrencyFormRequest');
|
||||
$request->shouldReceive('getCurrencyData')->andReturn([]);
|
||||
$repository->shouldReceive('update')->andReturn($currency);
|
||||
|
||||
$this->call('POST', '/currency/update/' . $currency->id, ['_token' => 'replaceMe']);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success');
|
||||
}
|
||||
|
||||
public function testUpdateAndReturn()
|
||||
{
|
||||
$user = FactoryMuffin::create('FireflyIII\User');
|
||||
$currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency');
|
||||
$this->be($user);
|
||||
|
||||
$repository = $this->mock('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface');
|
||||
$request = $this->mock('FireflyIII\Http\Requests\CurrencyFormRequest');
|
||||
$request->shouldReceive('getCurrencyData')->andReturn([]);
|
||||
$repository->shouldReceive('update')->andReturn($currency);
|
||||
|
||||
$this->call('POST', '/currency/update/' . $currency->id, ['_token' => 'replaceMe', 'return_to_edit' => 1]);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user