Expand test coverage. Remove else-statement.

This commit is contained in:
James Cole
2017-03-22 17:02:15 +01:00
parent 32c89f9a98
commit 6fe319702d
6 changed files with 167 additions and 48 deletions

View File

@@ -273,32 +273,16 @@ class PiggyBankController extends Controller
public function postAdd(Request $request, PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
{
$amount = $request->get('amount');
Log::debug(sprintf('Found amount is %s', $amount));
/** @var Carbon $date */
$date = session('end', Carbon::now()->endOfMonth());
$leftOnAccount = $piggyBank->leftOnAccount($date);
$savedSoFar = strval($piggyBank->currentRelevantRep()->currentamount);
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = strval(min(round($leftOnAccount, 12), round($leftToSave, 12)));
if (bccomp($amount, $maxAmount) <= 0) {
$repetition = $piggyBank->currentRelevantRep();
$currentAmount = $repetition->currentamount ?? '0';
$repetition->currentamount = bcadd($currentAmount, $amount);
$repetition->save();
// create event
$repository->createEvent($piggyBank, $amount);
Session::flash(
'success', strval(trans('firefly.added_amount_to_piggy', ['amount' => Amount::format($amount, false), 'name' => $piggyBank->name]))
);
if ($repository->canAddAmount($piggyBank, $amount)) {
$repository->addAmount($piggyBank, $amount);
Session::flash('success', strval(trans('firefly.added_amount_to_piggy', ['amount' => Amount::format($amount, false), 'name' => $piggyBank->name])));
Preferences::mark();
return redirect(route('piggy-banks.index'));
}
Log::error('Cannot add ' . $amount . ' because max amount is ' . $maxAmount . ' (left on account is ' . $leftOnAccount . ')');
Log::error('Cannot add ' . $amount . ' because canAddAmount returned false.');
Session::flash('error', strval(trans('firefly.cannot_add_amount_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])));
return redirect(route('piggy-banks.index'));
@@ -313,26 +297,24 @@ class PiggyBankController extends Controller
*/
public function postRemove(Request $request, PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank)
{
$amount = strval(round($request->get('amount'), 12));
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
if (bccomp($amount, $savedSoFar) <= 0) {
$repetition = $piggyBank->currentRelevantRep();
$repetition->currentamount = bcsub($repetition->currentamount, $amount);
$repetition->save();
// create event
$repository->createEvent($piggyBank, bcmul($amount, '-1'));
$amount = $request->get('amount');
if ($repository->canRemoveAmount($piggyBank, $amount)) {
$repository->removeAmount($piggyBank, $amount);
Session::flash(
'success', strval(trans('firefly.removed_amount_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)]))
'success', strval(trans('firefly.removed_amount_from_piggy', ['amount' => Amount::format($amount, false), 'name' => $piggyBank->name]))
);
Preferences::mark();
return redirect(route('piggy-banks.index'));
}
$amount = strval(round($request->get('amount'), 12));
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
if (bccomp($amount, $savedSoFar) <= 0) {
}
Session::flash('error', strval(trans('firefly.cannot_remove_from_piggy', ['amount' => Amount::format($amount, false), 'name' => e($piggyBank->name)])));
return redirect(route('piggy-banks.index'));
@@ -392,9 +374,11 @@ class PiggyBankController extends Controller
Preferences::mark();
if (intval($request->get('create_another')) === 1) {
// @codeCoverageIgnoreStart
Session::put('piggy-banks.create.fromStore', true);
return redirect(route('piggy-banks.create'))->withInput();
// @codeCoverageIgnoreEnd
}
return redirect($this->getPreviousUri('piggy-banks.edit.uri'));
@@ -416,9 +400,11 @@ class PiggyBankController extends Controller
Preferences::mark();
if (intval($request->get('return_to_edit')) === 1) {
// @codeCoverageIgnoreStart
Session::put('piggy-banks.edit.fromUpdate', true);
return redirect(route('piggy-banks.edit', [$piggyBank->id]));
// @codeCoverageIgnoreEnd
}
return redirect($this->getPreviousUri('piggy-banks.edit.uri'));

View File

@@ -160,11 +160,10 @@ class PreferencesController extends Controller
Preferences::set('showDepositsFrontpage', $showDepositsFrontpage);
// save page size:
Preferences::set('transactionPageSize', 50);
$transactionPageSize = intval($request->get('transactionPageSize'));
if ($transactionPageSize > 0 && $transactionPageSize < 1337) {
Preferences::set('transactionPageSize', $transactionPageSize);
} else {
Preferences::set('transactionPageSize', 50);
}
$twoFactorAuthEnabled = false;

View File

@@ -32,6 +32,54 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
/** @var User */
private $user;
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function addAmount(PiggyBank $piggyBank, string $amount): bool
{
$repetition = $piggyBank->currentRelevantRep();
$currentAmount = $repetition->currentamount ?? '0';
$repetition->currentamount = bcadd($currentAmount, $amount);
$repetition->save();
// create event
$this->createEvent($piggyBank, $amount);
return true;
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function canAddAmount(PiggyBank $piggyBank, string $amount): bool
{
$leftOnAccount = $piggyBank->leftOnAccount(new Carbon);
$savedSoFar = strval($piggyBank->currentRelevantRep()->currentamount);
$leftToSave = bcsub($piggyBank->targetamount, $savedSoFar);
$maxAmount = strval(min(round($leftOnAccount, 12), round($leftToSave, 12)));
return bccomp($amount, $maxAmount) <= 0;
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function canRemoveAmount(PiggyBank $piggyBank, string $amount): bool
{
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
return bccomp($amount, $savedSoFar) <= 0;
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
@@ -119,6 +167,24 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return $set;
}
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function removeAmount(PiggyBank $piggyBank, string $amount): bool
{
$repetition = $piggyBank->currentRelevantRep();
$repetition->currentamount = bcsub($repetition->currentamount, $amount);
$repetition->save();
// create event
$this->createEvent($piggyBank, bcmul($amount, '-1'));
return true;
}
/**
* Set all piggy banks to order 0.
*

View File

@@ -26,6 +26,30 @@ use Illuminate\Support\Collection;
interface PiggyBankRepositoryInterface
{
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function addAmount(PiggyBank $piggyBank, string $amount): bool;
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function canAddAmount(PiggyBank $piggyBank, string $amount): bool;
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function canRemoveAmount(PiggyBank $piggyBank, string $amount): bool;
/**
* Create a new event.
*
@@ -82,6 +106,14 @@ interface PiggyBankRepositoryInterface
*/
public function getPiggyBanksWithAmount(): Collection;
/**
* @param PiggyBank $piggyBank
* @param string $amount
*
* @return bool
*/
public function removeAmount(PiggyBank $piggyBank, string $amount): bool;
/**
* Set all piggy banks to order 0.
*