mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 10:53:37 +00:00
Implement budget limit API tests
This commit is contained in:
@@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||||||
namespace FireflyIII\Api\V1\Controllers;
|
namespace FireflyIII\Api\V1\Controllers;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Exception;
|
||||||
use FireflyIII\Api\V1\Requests\AvailableBudgetRequest;
|
use FireflyIII\Api\V1\Requests\AvailableBudgetRequest;
|
||||||
use FireflyIII\Api\V1\Requests\BudgetLimitRequest;
|
use FireflyIII\Api\V1\Requests\BudgetLimitRequest;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
@@ -42,6 +43,7 @@ use League\Fractal\Resource\Collection as FractalCollection;
|
|||||||
use League\Fractal\Resource\Item;
|
use League\Fractal\Resource\Item;
|
||||||
use League\Fractal\Serializer\JsonApiSerializer;
|
use League\Fractal\Serializer\JsonApiSerializer;
|
||||||
use Log;
|
use Log;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BudgetLimitController
|
* Class BudgetLimitController
|
||||||
|
@@ -51,8 +51,8 @@ class BudgetLimit extends Model
|
|||||||
'updated_at' => 'datetime',
|
'updated_at' => 'datetime',
|
||||||
'start_date' => 'date',
|
'start_date' => 'date',
|
||||||
'end_date' => 'date',
|
'end_date' => 'date',
|
||||||
'repeats' => 'boolean',
|
|
||||||
];
|
];
|
||||||
|
protected $fillable = ['budget_id', 'start_date', 'end_date', 'amount'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param string $value
|
||||||
|
329
tests/Api/V1/Controllers/BudgetLimitControllerTest.php
Normal file
329
tests/Api/V1/Controllers/BudgetLimitControllerTest.php
Normal file
@@ -0,0 +1,329 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* BudgetLimitControllerTest.php
|
||||||
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
||||||
|
*
|
||||||
|
* This file is part of Firefly III.
|
||||||
|
*
|
||||||
|
* Firefly III is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Firefly III is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Api\V1\Controllers;
|
||||||
|
|
||||||
|
|
||||||
|
use FireflyIII\Models\Budget;
|
||||||
|
use FireflyIII\Models\BudgetLimit;
|
||||||
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
|
use Laravel\Passport\Passport;
|
||||||
|
use Log;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Class BudgetLimitControllerTest
|
||||||
|
*/
|
||||||
|
class BudgetLimitControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
Passport::actingAs($this->user());
|
||||||
|
Log::debug(sprintf('Now in %s.', \get_class($this)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
|
||||||
|
*/
|
||||||
|
public function testDelete(): void
|
||||||
|
{
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('destroyBudgetLimit')->once()->andReturn(true);
|
||||||
|
|
||||||
|
// Create a budget limit (just in case).
|
||||||
|
/** @var Budget $budget */
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
$budgetLimit = BudgetLimit::create(
|
||||||
|
[
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
'amount' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$response = $this->delete('/api/v1/budget_limits/' . $budgetLimit->id);
|
||||||
|
$response->assertStatus(204);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show budget limits by budget, include no dates.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
|
||||||
|
*/
|
||||||
|
public function testIndex(): void
|
||||||
|
{
|
||||||
|
/** @var Budget $budget */
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('findNull')->andReturn($budget);
|
||||||
|
$repository->shouldReceive('getBudgetLimits')->once()->andReturn($budget->budgetlimits()->get());
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$params = [
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
];
|
||||||
|
$response = $this->get('/api/v1/budget_limits?' . http_build_query($params));
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show budget limits by budget, include dates.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
|
||||||
|
*/
|
||||||
|
public function testIndexNoBudget(): void
|
||||||
|
{
|
||||||
|
/** @var Budget $budget */
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('findNull')->andReturn($budget);
|
||||||
|
$repository->shouldReceive('getAllBudgetLimits')->once()->andReturn($budget->budgetlimits()->get());
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$params = [
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
];
|
||||||
|
$response = $this->get('/api/v1/budget_limits?' . http_build_query($params));
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show budget limits by budget, include dates.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
|
||||||
|
*/
|
||||||
|
public function testIndexWithDates(): void
|
||||||
|
{
|
||||||
|
/** @var Budget $budget */
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('findNull')->andReturn($budget);
|
||||||
|
$repository->shouldReceive('getBudgetLimits')->once()->andReturn($budget->budgetlimits()->get());
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$params = [
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
];
|
||||||
|
$response = $this->get('/api/v1/budget_limits?' . http_build_query($params));
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
|
||||||
|
*/
|
||||||
|
public function testShow(): void
|
||||||
|
{
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
|
||||||
|
// Create a budget limit (just in case).
|
||||||
|
/** @var Budget $budget */
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
$budgetLimit = BudgetLimit::create(
|
||||||
|
[
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
'amount' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
$response = $this->get('/api/v1/budget_limits/' . $budgetLimit->id);
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store new budget limit.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
|
||||||
|
*/
|
||||||
|
public function testStore(): void
|
||||||
|
{
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
$budgetLimit = BudgetLimit::create(
|
||||||
|
[
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
'amount' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$data
|
||||||
|
= [
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
'amount' => 1,
|
||||||
|
];
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('findNull')->andReturn($budget)->once();
|
||||||
|
$repository->shouldReceive('storeBudgetLimit')->andReturn($budgetLimit)->once();
|
||||||
|
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$response = $this->post('/api/v1/budget_limits', $data);
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store new budget limit, but give error
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
|
||||||
|
*/
|
||||||
|
public function testStoreBadBudget(): void
|
||||||
|
{
|
||||||
|
$data
|
||||||
|
= [
|
||||||
|
'budget_id' => '1',
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
'amount' => 1,
|
||||||
|
];
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('findNull')->andReturn(null)->once();
|
||||||
|
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$response = $this->post('/api/v1/budget_limits', $data);
|
||||||
|
$response->assertStatus(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test update of budget limit.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
|
||||||
|
*/
|
||||||
|
public function testUpdate(): void
|
||||||
|
{
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
$budgetLimit = BudgetLimit::create(
|
||||||
|
[
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
'amount' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$data
|
||||||
|
= [
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
'amount' => 2,
|
||||||
|
];
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('findNull')->andReturn($budget)->once();
|
||||||
|
$repository->shouldReceive('updateBudgetLimit')->andReturn($budgetLimit)->once();
|
||||||
|
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$response = $this->put('/api/v1/budget_limits/' . $budgetLimit->id, $data);
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test update of budget limit but submit bad budget.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetLimitController
|
||||||
|
*/
|
||||||
|
public function testUpdateBadBudget(): void
|
||||||
|
{
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
$budgetLimit = BudgetLimit::create(
|
||||||
|
[
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
'amount' => 1,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
$data
|
||||||
|
= [
|
||||||
|
'budget_id' => $budget->id,
|
||||||
|
'start_date' => '2018-01-01',
|
||||||
|
'end_date' => '2018-01-31',
|
||||||
|
'amount' => 2,
|
||||||
|
];
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
$repository->shouldReceive('findNull')->andReturn(null)->once();
|
||||||
|
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$response = $this->put('/api/v1/budget_limits/' . $budgetLimit->id, $data);
|
||||||
|
$response->assertStatus(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -22,6 +22,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Tests\Unit\TransactionRules\Triggers;
|
namespace Tests\Unit\TransactionRules\Triggers;
|
||||||
|
|
||||||
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\TransactionRules\Triggers\HasAnyBudget;
|
use FireflyIII\TransactionRules\Triggers\HasAnyBudget;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
@@ -52,9 +53,21 @@ class HasAnyBudgetTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testTriggeredNot(): void
|
public function testTriggeredNot(): void
|
||||||
{
|
{
|
||||||
|
do {
|
||||||
|
/** @var TransactionJournal $journal */
|
||||||
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
|
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
|
||||||
|
$count = $journal->transactions()->count();
|
||||||
|
} while ($count !== 0);
|
||||||
|
|
||||||
$journal->budgets()->detach();
|
$journal->budgets()->detach();
|
||||||
$this->assertEquals(0, $journal->budgets()->count());
|
$this->assertEquals(0, $journal->budgets()->count());
|
||||||
|
|
||||||
|
// also detach all transactions:
|
||||||
|
/** @var Transaction $transaction */
|
||||||
|
foreach ($journal->transactions()->get() as $transaction) {
|
||||||
|
$transaction->budgets()->detach();
|
||||||
|
}
|
||||||
|
|
||||||
$trigger = HasAnyBudget::makeFromStrings('', false);
|
$trigger = HasAnyBudget::makeFromStrings('', false);
|
||||||
$result = $trigger->triggered($journal);
|
$result = $trigger->triggered($journal);
|
||||||
$this->assertFalse($result);
|
$this->assertFalse($result);
|
||||||
|
Reference in New Issue
Block a user