firefly-iii/tests/Feature/Controllers/BudgetControllerTest.php

542 lines
22 KiB
PHP
Raw Normal View History

2017-02-12 05:00:11 -06:00
<?php
/**
* BudgetControllerTest.php
* Copyright (c) 2017 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 Tests\Feature\Controllers;
2017-02-12 05:21:44 -06:00
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2017-03-05 04:18:34 -06:00
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionJournal;
2017-02-12 05:21:44 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2017-03-05 04:18:34 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2017-02-12 05:21:44 -06:00
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
2017-02-12 05:00:11 -06:00
use Tests\TestCase;
class BudgetControllerTest extends TestCase
{
2017-02-12 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::amount
*/
public function testAmount()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('updateLimitAmount')->andReturn(new BudgetLimit);
$data = ['amount' => 200,];
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->post(route('budgets.amount', [1]), $data);
$response->assertStatus(200);
}
2017-03-18 05:02:02 -05:00
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::amount
*/
public function testAmountZero()
{
// mock stuff
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('updateLimitAmount')->andReturn(new BudgetLimit);
$data = ['amount' => 0,];
$this->be($this->user());
$response = $this->post(route('budgets.amount', [1]), $data);
$response->assertStatus(200);
}
2017-02-12 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::create
*/
public function testCreate()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('budgets.create'));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::delete
*/
public function testDelete()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('budgets.delete', [1]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::destroy
*/
public function testDestroy()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$repository->shouldReceive('destroy')->andReturn(true);
2017-03-18 14:53:44 -05:00
$this->session(['budgets.delete.uri' => 'http://localhost']);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->post(route('budgets.destroy', [1]));
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::edit
*/
public function testEdit()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->get(route('budgets.edit', [1]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
2017-06-28 11:05:38 -05:00
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::index
* @covers \FireflyIII\Http\Controllers\BudgetController::collectBudgetInformation
* @covers \FireflyIII\Http\Controllers\BudgetController::__construct
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testIndexWithDate(string $range)
{
// mock stuff
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
// set budget limit to current month:
$budgetLimit->start_date = Carbon::now()->startOfMonth();
$budgetLimit->end_date = Carbon::now()->endOfMonth();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository->shouldReceive('cleanupBudgets');
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection);
$repository->shouldReceive('getAvailableBudget')->andReturn('100.123');
$repository->shouldReceive('spentInPeriod')->andReturn('-1');
$repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]));
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.index', ['2017-01-01']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::index
* @covers \FireflyIII\Http\Controllers\BudgetController::collectBudgetInformation
* @covers \FireflyIII\Http\Controllers\BudgetController::__construct
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testIndexWithInvalidDate(string $range)
{
// mock stuff
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
// set budget limit to current month:
$budgetLimit->start_date = Carbon::now()->startOfMonth();
$budgetLimit->end_date = Carbon::now()->endOfMonth();
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository->shouldReceive('cleanupBudgets');
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection);
$repository->shouldReceive('getAvailableBudget')->andReturn('100.123');
$repository->shouldReceive('spentInPeriod')->andReturn('-1');
$repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]));
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.index', ['Hello-there']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
2017-02-12 05:21:44 -06:00
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::index
* @covers \FireflyIII\Http\Controllers\BudgetController::collectBudgetInformation
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\BudgetController::__construct
2017-02-12 05:21:44 -06:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testIndex(string $range)
{
2017-03-05 04:18:34 -06:00
// mock stuff
2017-03-18 14:53:44 -05:00
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
// set budget limit to current month:
$budgetLimit->start_date = Carbon::now()->startOfMonth();
$budgetLimit->end_date = Carbon::now()->endOfMonth();
2017-03-05 04:18:34 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
2017-02-12 05:21:44 -06:00
$repository->shouldReceive('cleanupBudgets');
2017-03-18 14:53:44 -05:00
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
2017-02-12 05:21:44 -06:00
$repository->shouldReceive('getInactiveBudgets')->andReturn(new Collection);
$repository->shouldReceive('getAvailableBudget')->andReturn('100.123');
2017-03-18 14:53:44 -05:00
$repository->shouldReceive('spentInPeriod')->andReturn('-1');
$repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]));
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.index'));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::noBudget
* @covers \FireflyIII\Http\Controllers\BudgetController::getPeriodOverview
2017-02-12 05:21:44 -06:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testNoBudget(string $range)
{
2017-03-05 04:18:34 -06:00
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2017-03-09 14:05:37 -06:00
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
2017-03-09 14:05:37 -06:00
$collector->shouldReceive('getJournals')->andReturn(new Collection);
2017-02-12 05:21:44 -06:00
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
2017-02-12 05:21:44 -06:00
$collector->shouldReceive('withoutBudget')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
2017-02-12 05:21:44 -06:00
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10));
2017-03-05 04:18:34 -06:00
$date = new Carbon();
$this->session(['start' => $date, 'end' => clone $date]);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.no-budget'));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-03-09 14:05:37 -06:00
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::noBudget
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testNoBudgetAll(string $range)
{
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
2017-03-09 14:05:37 -06:00
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
2017-03-09 14:05:37 -06:00
$collector->shouldReceive('withoutBudget')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
2017-03-09 14:05:37 -06:00
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection);
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10));
$date = new Carbon();
$this->session(['start' => $date, 'end' => clone $date]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.no-budget', ['all']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::noBudget
* @covers \FireflyIII\Http\Controllers\BudgetController::getPeriodOverview
2017-03-09 14:05:37 -06:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testNoBudgetDate(string $range)
{
// mock stuff
$collector = $this->mock(JournalCollectorInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection);
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('setTypes')->andReturnSelf();
2017-03-09 14:05:37 -06:00
$collector->shouldReceive('withoutBudget')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10));
$date = new Carbon();
$this->session(['start' => $date, 'end' => clone $date]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.no-budget', ['2016-01-01']));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-02-12 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::postUpdateIncome
*/
public function testPostUpdateIncome()
{
2017-03-05 04:18:34 -06:00
// mock stuff
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('setAvailableBudget');
$data = ['amount' => '200',];
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$response = $this->post(route('budgets.income.post'), $data);
$response->assertStatus(302);
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::show
* @covers \FireflyIII\Http\Controllers\BudgetController::getLimits
2017-02-12 05:21:44 -06:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShow(string $range)
{
2017-03-05 04:18:34 -06:00
// mock stuff
2017-03-18 14:53:44 -05:00
$budgetLimit = factory(BudgetLimit::class)->make();
2017-03-05 04:18:34 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
2017-03-05 04:19:06 -06:00
$collector = $this->mock(JournalCollectorInterface::class);
2017-02-12 05:21:44 -06:00
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setBudget')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10));
2017-03-05 04:18:34 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
$repository = $this->mock(BudgetRepositoryInterface::class);
2017-03-18 14:53:44 -05:00
$repository->shouldReceive('getBudgetLimits')->andReturn(new Collection([$budgetLimit]));
$repository->shouldReceive('spentInPeriod')->andReturn('-1');
2017-03-05 04:18:34 -06:00
$date = new Carbon();
$date->subDay();
$this->session(['first' => $date]);
2017-02-12 05:21:44 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.show', [1]));
$response->assertStatus(200);
$response->assertSee('<ol class="breadcrumb">');
}
2017-03-18 14:53:44 -05:00
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::showByBudgetLimit
* @expectedExceptionMessage This budget limit is not part of
*
*/
public function testShowByBadBudgetLimit()
{
// mock stuff
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$this->be($this->user());
$response = $this->get(route('budgets.show.limit', [1, 8]));
$response->assertStatus(500);
}
2017-02-12 05:21:44 -06:00
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::showByBudgetLimit()
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testShowByBudgetLimit(string $range)
{
2017-03-05 04:18:34 -06:00
// mock stuff
2017-03-05 04:19:06 -06:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
2017-03-05 04:18:34 -06:00
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-02-12 05:21:44 -06:00
// mock account repository
$accountRepository = $this->mock(AccountRepositoryInterface::class);
$accountRepository->shouldReceive('getAccountsByType')->andReturn(new Collection);
2017-03-05 04:18:34 -06:00
2017-02-12 05:21:44 -06:00
// mock budget repository
$budgetRepository = $this->mock(BudgetRepositoryInterface::class);
$budgetRepository->shouldReceive('spentInPeriod')->andReturn('1');
$budgetRepository->shouldReceive('getBudgetLimits')->andReturn(new Collection);
// mock journal collector:
$collector = $this->mock(JournalCollectorInterface::class);
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setLimit')->andReturnSelf();
$collector->shouldReceive('setPage')->andReturnSelf();
$collector->shouldReceive('setBudget')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10));
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('budgets.show.limit', [1, 1]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::store
*/
public function testStore()
{
2017-03-05 04:18:34 -06:00
// mock stuff
2017-03-05 04:19:06 -06:00
$budget = factory(Budget::class)->make();
2017-03-05 04:18:34 -06:00
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('find')->andReturn($budget);
$repository->shouldReceive('store')->andReturn($budget);
2017-03-18 14:53:44 -05:00
$this->session(['budgets.create.uri' => 'http://localhost']);
2017-02-12 05:21:44 -06:00
$data = [
'name' => 'New Budget ' . rand(1000, 9999),
];
$this->be($this->user());
$response = $this->post(route('budgets.store'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::update
*/
public function testUpdate()
{
2017-03-05 04:18:34 -06:00
// mock stuff
2017-03-05 04:19:06 -06:00
$budget = factory(Budget::class)->make();
2017-03-05 04:18:34 -06:00
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('find')->andReturn($budget);
$repository->shouldReceive('update');
2017-03-18 14:53:44 -05:00
$this->session(['budgets.edit.uri' => 'http://localhost']);
2017-02-12 05:21:44 -06:00
$data = [
'name' => 'Updated Budget ' . rand(1000, 9999),
'active' => 1,
];
$this->be($this->user());
$response = $this->post(route('budgets.update', [1]), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
* @covers \FireflyIII\Http\Controllers\BudgetController::updateIncome
*/
public function testUpdateIncome()
{
// must be in list
$this->be($this->user());
2017-03-05 04:18:34 -06:00
// mock stuff
$repository = $this->mock(BudgetRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('getAvailableBudget')->andReturn('1');
$repository->shouldReceive('cleanupBudgets');
2017-03-05 04:18:34 -06:00
2017-02-12 05:21:44 -06:00
$response = $this->get(route('budgets.income', [1]));
$response->assertStatus(200);
}
2017-02-12 05:00:11 -06:00
2017-02-16 15:33:32 -06:00
}