Fix budget limit tests

This commit is contained in:
James Cole
2021-03-13 20:01:42 +01:00
parent 25bc0b0b78
commit 07f62b0d5c
15 changed files with 86 additions and 64 deletions

View File

@@ -104,7 +104,7 @@ class ShowController extends Controller
*/ */
public function show(Request $request, Budget $budget, BudgetLimit $budgetLimit): JsonResponse public function show(Request $request, Budget $budget, BudgetLimit $budgetLimit): JsonResponse
{ {
if ($budget->id !== $budgetLimit->budget_id) { if ((int)$budget->id !== (int)$budgetLimit->budget_id) {
throw new FireflyException('20028: The budget limit does not belong to the budget.'); throw new FireflyException('20028: The budget limit does not belong to the budget.');
} }
// continue! // continue!

View File

@@ -74,7 +74,7 @@ class UpdateController extends Controller
public function update(UpdateRequest $request, Budget $budget, BudgetLimit $budgetLimit): JsonResponse public function update(UpdateRequest $request, Budget $budget, BudgetLimit $budgetLimit): JsonResponse
{ {
if ($budget->id !== $budgetLimit->budget_id) { if ((int)$budget->id !== (int)$budgetLimit->budget_id) {
throw new FireflyException('20028: The budget limit does not belong to the budget.'); throw new FireflyException('20028: The budget limit does not belong to the budget.');
} }
$data = $request->getAll(); $data = $request->getAll();

View File

@@ -23,9 +23,11 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests\Models\BudgetLimit; namespace FireflyIII\Api\V1\Requests\Models\BudgetLimit;
use Carbon\Carbon;
use FireflyIII\Support\Request\ChecksLogin; use FireflyIII\Support\Request\ChecksLogin;
use FireflyIII\Support\Request\ConvertsDataTypes; use FireflyIII\Support\Request\ConvertsDataTypes;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Validator;
/** /**
* Class UpdateRequest * Class UpdateRequest
@@ -43,13 +45,15 @@ class UpdateRequest extends FormRequest
*/ */
public function getAll(): array public function getAll(): array
{ {
return [ $fields = [
'start' => $this->date('start'), 'start' => ['start', 'date'],
'end' => $this->date('end'), 'end' => ['end', 'date'],
'amount' => $this->string('amount'), 'amount' => ['amount', 'string'],
'currency_id' => $this->integer('currency_id'), 'currency_id' => ['currency_id', 'integer'],
'currency_code' => $this->string('currency_code'), 'currency_code' => ['currency_code', 'string'],
]; ];
return $this->getAllData($fields);
} }
/** /**
@@ -60,12 +64,37 @@ return [
public function rules(): array public function rules(): array
{ {
return [ return [
'start' => 'before:end|date', 'start' => 'date',
'end' => 'after:start|date', 'end' => 'date',
'amount' => 'gt:0', 'amount' => 'gt:0',
'currency_id' => 'numeric|exists:transaction_currencies,id', 'currency_id' => 'numeric|exists:transaction_currencies,id',
'currency_code' => 'min:3|max:3|exists:transaction_currencies,code', 'currency_code' => 'min:3|max:3|exists:transaction_currencies,code',
]; ];
} }
/**
* Configure the validator instance with special rules for after the basic validation rules.
*
* @param Validator $validator
* TODO duplicate code.
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
// validate start before end only if both are there.
$data = $validator->getData();
if (array_key_exists('start', $data) && array_key_exists('end', $data)) {
$start = new Carbon($data['start']);
$end = new Carbon($data['end']);
if ($end->isBefore($start)) {
$validator->errors()->add('end', (string)trans('validation.date_after'));
}
}
}
);
}
} }

View File

@@ -359,8 +359,8 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
{ {
$budgetLimit->amount = array_key_exists('amount', $data) ? $data['amount'] : $budgetLimit->amount; $budgetLimit->amount = array_key_exists('amount', $data) ? $data['amount'] : $budgetLimit->amount;
$budgetLimit->budget_id = array_key_exists('budget_id', $data) ? $data['budget_id'] : $budgetLimit->budget_id; $budgetLimit->budget_id = array_key_exists('budget_id', $data) ? $data['budget_id'] : $budgetLimit->budget_id;
$budgetLimit->start_date = array_key_exists('start_date', $data) ? $data['start_date']->format('Y-m-d 00:00:00') : $budgetLimit->start_date; $budgetLimit->start_date = array_key_exists('start', $data) ? $data['start']->format('Y-m-d 00:00:00') : $budgetLimit->start_date;
$budgetLimit->end_date = array_key_exists('end_date', $data) ? $data['end_date']->format('Y-m-d 23:59:59') : $budgetLimit->end_date; $budgetLimit->end_date = array_key_exists('end', $data) ? $data['end']->format('Y-m-d 23:59:59') : $budgetLimit->end_date;
// if no currency has been provided, use the user's default currency: // if no currency has been provided, use the user's default currency:
$currency = null; $currency = null;
@@ -381,8 +381,6 @@ class BudgetLimitRepository implements BudgetLimitRepositoryInterface
$budgetLimit->transaction_currency_id = $currency->id; $budgetLimit->transaction_currency_id = $currency->id;
$budgetLimit->save(); $budgetLimit->save();
Log::debug(sprintf('Updated budget limit with ID #%d and amount %s', $budgetLimit->id, $data['amount']));
return $budgetLimit; return $budgetLimit;
} }

View File

@@ -33,16 +33,6 @@ use Log;
*/ */
class BillDestroyService class BillDestroyService
{ {
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
/** /**
* @param Bill $bill * @param Bill $bill
*/ */

View File

@@ -34,16 +34,6 @@ use Log;
*/ */
class BudgetDestroyService class BudgetDestroyService
{ {
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
/** /**
* @param Budget $budget * @param Budget $budget
*/ */

View File

@@ -34,16 +34,6 @@ use Log;
*/ */
class CategoryDestroyService class CategoryDestroyService
{ {
/**
* Constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
/** /**
* @param Category $category * @param Category $category
*/ */

View File

@@ -67,10 +67,6 @@ class AccountValidator
/** @var AccountRepositoryInterface accountRepository */ /** @var AccountRepositoryInterface accountRepository */
$this->accountRepository = app(AccountRepositoryInterface::class); $this->accountRepository = app(AccountRepositoryInterface::class);
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
} }
/** /**

View File

@@ -49,10 +49,8 @@ class StoreControllerTest extends TestCase
/** /**
* @param array $submission * @param array $submission
* * emptyDataProvider / storeDataProvider
* @dataProvider storeDataProvider * @dataProvider emptyDataProvider
*
* @ data Provider emptyDataProvider
*/ */
public function testStore(array $submission): void public function testStore(array $submission): void
{ {

View File

@@ -51,7 +51,7 @@ class StoreControllerTest extends TestCase
/** /**
* @param array $submission * @param array $submission
* *
* @ data Provider storeDataProvider * emptyDataProvider / storeDataProvider
* @dataProvider emptyDataProvider * @dataProvider emptyDataProvider
*/ */
public function testStore(array $submission): void public function testStore(array $submission): void

View File

@@ -51,8 +51,8 @@ class StoreControllerTest extends TestCase
/** /**
* @param array $submission * @param array $submission
* *
* @dataProvider storeDataProvider * emptyDataProvider / storeDataProvider
* @ data Provider emptyDataProvider * @dataProvider emptyDataProvider
*/ */
public function testStore(array $submission): void public function testStore(array $submission): void
{ {

View File

@@ -51,8 +51,8 @@ class StoreControllerTest extends TestCase
/** /**
* @param array $submission * @param array $submission
* *
* @dataProvider storeDataProvider * emptyDataProvider / storeDataProvider
* @ data Provider emptyDataProvider * @dataProvider emptyDataProvider
*/ */
public function testStore(array $submission): void public function testStore(array $submission): void
{ {

View File

@@ -51,8 +51,8 @@ class StoreControllerTest extends TestCase
/** /**
* @param array $submission * @param array $submission
* *
* @dataProvider storeDataProvider * emptyDataProvider / storeDataProvider
* @ data Provider emptyDataProvider * @dataProvider emptyDataProvider
*/ */
public function testStore(array $submission): void public function testStore(array $submission): void
{ {

View File

@@ -51,8 +51,8 @@ class StoreControllerTest extends TestCase
/** /**
* @param array $submission * @param array $submission
* *
* @dataProvider storeDataProvider * emptyDataProvider / storeDataProvider
* @ data Provider emptyDataProvider * @dataProvider emptyDataProvider
*/ */
public function testStore(array $submission): void public function testStore(array $submission): void
{ {

View File

@@ -58,7 +58,6 @@ class UpdateControllerTest extends TestCase
'updated_at', 'updated_at',
]; ];
$route = route('api.v1.budgets.limits.update', [$submission['id'], $submission['bl_id']]); $route = route('api.v1.budgets.limits.update', [$submission['id'], $submission['bl_id']]);
$this->updateAndCompare($route, $submission, $ignore); $this->updateAndCompare($route, $submission, $ignore);
} }
@@ -100,11 +99,43 @@ class UpdateControllerTest extends TestCase
$autoBudgetType = $autoBudgetTypes[rand(0, count($autoBudgetTypes) - 1)]; $autoBudgetType = $autoBudgetTypes[rand(0, count($autoBudgetTypes) - 1)];
$set = [ $set = [
'name' => [ 'currency_id' => [
'id' => 1, 'id' => 1,
'bl_id' => 1, 'bl_id' => 1,
'fields' => [ 'fields' => [
'amount' => ['test_value' => number_format($faker->randomFloat(2,10,100), 2)], 'currency_id' => ['test_value' => (string)$rand],
],
'extra_ignore' => ['currency_code','currency_name','currency_symbol'],
],
'currency_code' => [
'id' => 1,
'bl_id' => 1,
'fields' => [
'currency_code' => ['test_value' => $currencies[$rand]],
],
'extra_ignore' => ['currency_id','currency_name','currency_symbol'],
],
'start' => [
'id' => 1,
'bl_id' => 1,
'fields' => [
'start' => ['test_value' => $faker->dateTimeBetween('-2 year', '-1 year')->format('Y-m-d')],
],
'extra_ignore' => [],
],
'end' => [
'id' => 1,
'bl_id' => 1,
'fields' => [
'end' => ['test_value' => $faker->dateTimeBetween('-1 year', 'now')->format('Y-m-d')],
],
'extra_ignore' => [],
],
'amount' => [
'id' => 1,
'bl_id' => 1,
'fields' => [
'amount' => ['test_value' => number_format($faker->randomFloat(2, 10, 100), 2)],
], ],
'extra_ignore' => [], 'extra_ignore' => [],
], ],