mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Code for currency controller tests.
This commit is contained in:
parent
0f260da8e6
commit
8a7297e131
@ -89,14 +89,16 @@ class CurrencyController extends Controller
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
* @param CurrencyRepositoryInterface $repository
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|View
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
|
||||
*/
|
||||
public function delete(TransactionCurrency $currency)
|
||||
public function delete(CurrencyRepositoryInterface $repository, TransactionCurrency $currency)
|
||||
{
|
||||
if (!$this->canDeleteCurrency($currency)) {
|
||||
if (!$repository->canDeleteCurrency($currency)) {
|
||||
Session::flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
|
||||
|
||||
return redirect(route('currencies.index'));
|
||||
@ -114,23 +116,21 @@ class CurrencyController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
* @param CurrencyRepositoryInterface $repository
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Exception
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function destroy(TransactionCurrency $currency)
|
||||
public function destroy(CurrencyRepositoryInterface $repository, TransactionCurrency $currency)
|
||||
{
|
||||
if (!$this->canDeleteCurrency($currency)) {
|
||||
if (!$repository->canDeleteCurrency($currency)) {
|
||||
Session::flash('error', trans('firefly.cannot_delete_currency', ['name' => $currency->name]));
|
||||
|
||||
return redirect(route('currencies.index'));
|
||||
}
|
||||
|
||||
$repository->destroy($currency);
|
||||
Session::flash('success', trans('firefly.deleted_currency', ['name' => $currency->name]));
|
||||
if (auth()->user()->hasRole('owner')) {
|
||||
$currency->forceDelete();
|
||||
}
|
||||
|
||||
return redirect(session('currencies.delete.url'));
|
||||
}
|
||||
@ -235,40 +235,4 @@ class CurrencyController extends Controller
|
||||
return redirect(session('currencies.edit.url'));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function canDeleteCurrency(TransactionCurrency $currency): bool
|
||||
{
|
||||
$repository = app(CurrencyRepositoryInterface::class);
|
||||
|
||||
// has transactions still
|
||||
if ($repository->countJournals($currency) > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// is the only currency left
|
||||
if ($repository->get()->count() === 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// is the default currency for the user or the system
|
||||
$defaultCode = Preferences::get('currencyPreference', config('firefly.default_currency', 'EUR'))->data;
|
||||
if ($currency->code === $defaultCode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// is the default currency for the system
|
||||
$defaultSystemCode = config('firefly.default_currency', 'EUR');
|
||||
if ($currency->code === $defaultSystemCode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// can be deleted
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
60
app/Providers/CurrencyServiceProvider.php
Normal file
60
app/Providers/CurrencyServiceProvider.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* CurrencyServiceProvider.php
|
||||
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This software may be modified and distributed under the terms of the
|
||||
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
|
||||
namespace FireflyIII\Providers;
|
||||
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
/**
|
||||
* Class CurrencyServiceProvider
|
||||
*
|
||||
* @package FireflyIII\Providers
|
||||
*/
|
||||
class CurrencyServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind(
|
||||
'FireflyIII\Repositories\Currency\CurrencyRepositoryInterface',
|
||||
function (Application $app, array $arguments) {
|
||||
if (!isset($arguments[0]) && $app->auth->check()) {
|
||||
return app('FireflyIII\Repositories\Currency\CurrencyRepository', [auth()->user()]);
|
||||
}
|
||||
if (!isset($arguments[0]) && !$app->auth->check()) {
|
||||
throw new FireflyException('There is no user present.');
|
||||
}
|
||||
|
||||
return app('FireflyIII\Repositories\Currency\CurrencyRepository', $arguments);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -96,7 +96,7 @@ class FireflyServiceProvider extends ServiceProvider
|
||||
// chart generator:
|
||||
$this->app->bind('FireflyIII\Generator\Chart\Basic\GeneratorInterface', 'FireflyIII\Generator\Chart\Basic\ChartJsGenerator');
|
||||
|
||||
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
|
||||
// other generators
|
||||
$this->app->bind('FireflyIII\Repositories\User\UserRepositoryInterface', 'FireflyIII\Repositories\User\UserRepository');
|
||||
$this->app->bind('FireflyIII\Helpers\Attachments\AttachmentHelperInterface', 'FireflyIII\Helpers\Attachments\AttachmentHelper');
|
||||
$this->app->bind(
|
||||
|
@ -16,7 +16,9 @@ namespace FireflyIII\Repositories\Currency;
|
||||
|
||||
use FireflyIII\Models\Preference;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Support\Collection;
|
||||
use Preferences;
|
||||
|
||||
/**
|
||||
* Class CurrencyRepository
|
||||
@ -25,6 +27,50 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* CategoryRepository constructor.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canDeleteCurrency(TransactionCurrency $currency): bool
|
||||
{
|
||||
if ($this->countJournals($currency) > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// is the only currency left
|
||||
if ($this->get()->count() === 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// is the default currency for the user or the system
|
||||
$defaultCode = Preferences::getForUser($this->user, 'currencyPreference', config('firefly.default_currency', 'EUR'))->data;
|
||||
if ($currency->code === $defaultCode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// is the default currency for the system
|
||||
$defaultSystemCode = config('firefly.default_currency', 'EUR');
|
||||
if ($currency->code === $defaultSystemCode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// can be deleted
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
@ -36,6 +82,20 @@ class CurrencyRepository implements CurrencyRepositoryInterface
|
||||
return $currency->transactionJournals()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy(TransactionCurrency $currency): bool
|
||||
{
|
||||
if ($this->user->hasRole('owner')) {
|
||||
$currency->forceDelete();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find by ID
|
||||
*
|
||||
|
@ -25,6 +25,13 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
interface CurrencyRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canDeleteCurrency(TransactionCurrency $currency): bool;
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
@ -32,6 +39,13 @@ interface CurrencyRepositoryInterface
|
||||
*/
|
||||
public function countJournals(TransactionCurrency $currency): int;
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $currency
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy(TransactionCurrency $currency): bool;
|
||||
|
||||
/**
|
||||
* Find by ID
|
||||
*
|
||||
|
@ -80,6 +80,7 @@ return [
|
||||
FireflyIII\Providers\BillServiceProvider::class,
|
||||
FireflyIII\Providers\BudgetServiceProvider::class,
|
||||
FireflyIII\Providers\CategoryServiceProvider::class,
|
||||
FireflyIII\Providers\CurrencyServiceProvider::class,
|
||||
FireflyIII\Providers\ExportJobServiceProvider::class,
|
||||
FireflyIII\Providers\JournalServiceProvider::class,
|
||||
FireflyIII\Providers\PiggyBankServiceProvider::class,
|
||||
|
@ -8,6 +8,7 @@
|
||||
*
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
|
||||
|
||||
/**
|
||||
@ -43,10 +44,10 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testDefaultCurrency()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
$this->be($this->user());
|
||||
$this->call('GET', route('currencies.default', [1]));
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,10 +67,15 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testDestroy()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
$this->session(['currencies.delete.url' => 'http://localhost']);
|
||||
$repository = $this->mock(CurrencyRepositoryInterface::class);
|
||||
$repository->shouldReceive('canDeleteCurrency')->andReturn(true);
|
||||
$repository->shouldReceive('destroy')->andReturn(true);
|
||||
|
||||
$this->be($this->user());
|
||||
$this->call('post', route('currencies.destroy', [1]));
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -101,10 +107,16 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testStore()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
$this->session(['currencies.create.url' => 'http://localhost']);
|
||||
$data = [
|
||||
'name' => 'XX',
|
||||
'code' => 'XXX',
|
||||
'symbol' => 'x',
|
||||
];
|
||||
$this->be($this->user());
|
||||
$this->call('post', route('currencies.store'), $data);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,9 +124,15 @@ class CurrencyControllerTest extends TestCase
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
// Remove the following lines when you implement this test.
|
||||
$this->markTestIncomplete(
|
||||
'This test has not been implemented yet.'
|
||||
);
|
||||
$this->session(['currencies.edit.url' => 'http://localhost']);
|
||||
$data = [
|
||||
'name' => 'XA',
|
||||
'code' => 'XAX',
|
||||
'symbol' => 'a',
|
||||
];
|
||||
$this->be($this->user());
|
||||
$this->call('post', route('currencies.update', [2]), $data);
|
||||
$this->assertResponseStatus(302);
|
||||
$this->assertSessionHas('success');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user