mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Include tests for budget API
This commit is contained in:
@@ -62,7 +62,7 @@ class BillControllerTest extends TestCase
|
|||||||
$repository->shouldReceive('setUser')->once();
|
$repository->shouldReceive('setUser')->once();
|
||||||
$repository->shouldReceive('destroy')->once()->andReturn(true);
|
$repository->shouldReceive('destroy')->once()->andReturn(true);
|
||||||
|
|
||||||
// get account:
|
// get bill:
|
||||||
$bill = $this->user()->bills()->first();
|
$bill = $this->user()->bills()->first();
|
||||||
|
|
||||||
// call API
|
// call API
|
||||||
|
|||||||
175
tests/Api/V1/Controllers/BudgetControllerTest.php
Normal file
175
tests/Api/V1/Controllers/BudgetControllerTest.php
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* BudgetControllerTest.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\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
|
use Laravel\Passport\Passport;
|
||||||
|
use Log;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Class BudgetControllerTest
|
||||||
|
*/
|
||||||
|
class BudgetControllerTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
Passport::actingAs($this->user());
|
||||||
|
Log::debug(sprintf('Now in %s.', \get_class($this)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a budget.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
||||||
|
*/
|
||||||
|
public function testDelete(): void
|
||||||
|
{
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('destroy')->once()->andReturn(true);
|
||||||
|
|
||||||
|
// get budget:
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$response = $this->delete('/api/v1/budgets/' . $budget->id);
|
||||||
|
$response->assertStatus(204);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show all budgets
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
||||||
|
*/
|
||||||
|
public function testIndex(): void
|
||||||
|
{
|
||||||
|
$budgets = $this->user()->budgets()->get();
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('getBudgets')->once()->andReturn($budgets);
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$response = $this->get('/api/v1/budgets');
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertSee($budgets->first()->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show a single budget.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
||||||
|
*/
|
||||||
|
public function testShow(): void
|
||||||
|
{
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
|
||||||
|
// call API
|
||||||
|
$response = $this->get('/api/v1/budgets/' . $budget->id);
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertSee($budget->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a new budget.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
||||||
|
*/
|
||||||
|
public function testStore(): void
|
||||||
|
{
|
||||||
|
/** @var Budget $budget */
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
|
||||||
|
// mock stuff:
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser')->once();
|
||||||
|
$repository->shouldReceive('store')->once()->andReturn($budget);
|
||||||
|
|
||||||
|
// data to submit
|
||||||
|
$data = [
|
||||||
|
'name' => 'Some budget',
|
||||||
|
'active' => '1',
|
||||||
|
];
|
||||||
|
|
||||||
|
// test API
|
||||||
|
$response = $this->post('/api/v1/budgets', $data);
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertJson(['data' => ['type' => 'budgets', 'links' => true],]);
|
||||||
|
$response->assertSee($budget->name); // the amount
|
||||||
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a budget.
|
||||||
|
*
|
||||||
|
* @covers \FireflyIII\Api\V1\Controllers\BudgetController
|
||||||
|
*/
|
||||||
|
public function testUpdate(): void
|
||||||
|
{
|
||||||
|
// mock repositories
|
||||||
|
$repository = $this->mock(BudgetRepositoryInterface::class);
|
||||||
|
|
||||||
|
/** @var Budget $budget */
|
||||||
|
$budget = $this->user()->budgets()->first();
|
||||||
|
|
||||||
|
// mock calls:
|
||||||
|
$repository->shouldReceive('setUser');
|
||||||
|
$repository->shouldReceive('update')->once()->andReturn($budget);
|
||||||
|
|
||||||
|
// data to submit
|
||||||
|
$data = [
|
||||||
|
'name' => 'Some new budget',
|
||||||
|
'active' => '1',
|
||||||
|
];
|
||||||
|
|
||||||
|
// test API
|
||||||
|
$response = $this->put('/api/v1/budgets/' . $budget->id, $data, ['Accept' => 'application/json']);
|
||||||
|
$response->assertStatus(200);
|
||||||
|
$response->assertJson(['data' => ['type' => 'budgets', 'links' => true],]);
|
||||||
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||||
|
$response->assertSee($budget->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -36,14 +36,15 @@ class ToAccountEndsTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testTriggered(): void
|
public function testTriggered(): void
|
||||||
{
|
{
|
||||||
$count = 0;
|
$count = 0;
|
||||||
while ($count === 0) {
|
$account = null;
|
||||||
|
while ($count === 0 && $account === null) {
|
||||||
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
|
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
|
||||||
$count = $journal->transactions()->where('amount', '>', 0)->count();
|
$count = $journal->transactions()->where('amount', '>', 0)->count();
|
||||||
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
|
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
|
||||||
|
$account = $transaction->account;
|
||||||
}
|
}
|
||||||
|
|
||||||
$account = $transaction->account;
|
|
||||||
$trigger = ToAccountEnds::makeFromStrings(substr($account->name, -3), false);
|
$trigger = ToAccountEnds::makeFromStrings(substr($account->name, -3), false);
|
||||||
$result = $trigger->triggered($journal);
|
$result = $trigger->triggered($journal);
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
@@ -60,7 +61,7 @@ class ToAccountEndsTest extends TestCase
|
|||||||
$count = $journal->transactions()->where('amount', '>', 0)->count();
|
$count = $journal->transactions()->where('amount', '>', 0)->count();
|
||||||
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
|
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
|
||||||
}
|
}
|
||||||
$account = $transaction->account;
|
$account = $transaction->account;
|
||||||
|
|
||||||
$trigger = ToAccountEnds::makeFromStrings('bla-bla-bla' . $account->name, false);
|
$trigger = ToAccountEnds::makeFromStrings('bla-bla-bla' . $account->name, false);
|
||||||
$result = $trigger->triggered($journal);
|
$result = $trigger->triggered($journal);
|
||||||
@@ -74,8 +75,8 @@ class ToAccountEndsTest extends TestCase
|
|||||||
{
|
{
|
||||||
$count = 0;
|
$count = 0;
|
||||||
while ($count === 0) {
|
while ($count === 0) {
|
||||||
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
|
$journal = TransactionJournal::inRandomOrder()->whereNull('deleted_at')->first();
|
||||||
$count = $journal->transactions()->where('amount', '>', 0)->count();
|
$count = $journal->transactions()->where('amount', '>', 0)->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
$trigger = ToAccountEnds::makeFromStrings((string)random_int(1, 1234), false);
|
$trigger = ToAccountEnds::makeFromStrings((string)random_int(1, 1234), false);
|
||||||
|
|||||||
Reference in New Issue
Block a user