firefly-iii/tests/Feature/Controllers/Chart/AccountControllerTest.php

353 lines
16 KiB
PHP
Raw Normal View History

2017-02-12 10:58:16 -06:00
<?php
/**
* AccountControllerTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* 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
2017-12-17 07:42:21 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 10:58:16 -06:00
*/
2017-03-25 07:41:17 -05:00
declare(strict_types=1);
2017-02-12 10:58:16 -06:00
namespace Tests\Feature\Controllers\Chart;
2017-03-11 15:28:51 -06:00
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
2017-03-25 07:41:17 -05:00
use FireflyIII\Models\Account;
2017-03-11 15:28:51 -06:00
use FireflyIII\Models\AccountType;
2017-03-25 07:41:17 -05:00
use FireflyIII\Models\Category;
use FireflyIII\Models\Transaction;
2018-02-28 08:50:00 -06:00
use FireflyIII\Models\TransactionCurrency;
2017-03-11 15:28:51 -06:00
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2018-02-28 08:50:00 -06:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-03-11 15:28:51 -06:00
use Illuminate\Support\Collection;
2018-03-24 00:08:50 -05:00
use Log;
2017-03-25 07:41:17 -05:00
use Preferences;
2017-03-11 15:28:51 -06:00
use Steam;
2017-02-12 10:58:16 -06:00
use Tests\TestCase;
2017-03-12 03:22:33 -05:00
/**
* Class AccountControllerTest
*
2017-08-12 03:27:45 -05:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-03-12 03:22:33 -05:00
*/
2017-02-12 10:58:16 -06:00
class AccountControllerTest extends TestCase
{
2018-03-24 00:08:50 -05:00
/**
*
*/
public function setUp()
{
parent::setUp();
Log::debug(sprintf('Now in %s.', get_class($this)));
}
2017-03-17 10:34:57 -05:00
2017-02-12 10:58:16 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::expenseAccounts
2017-02-24 14:01:33 -06:00
* @covers \FireflyIII\Generator\Chart\Basic\GeneratorInterface::singleSet
2017-02-12 10:58:16 -06:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testExpenseAccounts(string $range)
{
2018-02-28 08:50:00 -06:00
$account = factory(Account::class)->make();
$generator = $this->mock(GeneratorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2017-03-11 15:28:51 -06:00
2017-03-25 07:41:17 -05:00
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::EXPENSE, AccountType::BENEFICIARY]])->andReturn(new Collection([$account]));
2017-03-11 15:28:51 -06:00
$generator->shouldReceive('singleSet')->andReturn([]);
Steam::shouldReceive('balancesByAccounts')->twice()->andReturn([]);
2017-03-11 15:28:51 -06:00
2017-02-12 10:58:16 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.account.expense'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::expenseBudget
2017-03-11 15:28:51 -06:00
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::getBudgetNames
2017-02-12 10:58:16 -06:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testExpenseBudget(string $range)
{
2017-03-12 01:38:13 -06:00
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
2017-03-25 07:41:17 -05:00
$transaction = factory(Transaction::class)->make();
2017-03-11 15:28:51 -06:00
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf();
2017-03-25 07:41:17 -05:00
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
2017-03-11 15:28:51 -06:00
$generator->shouldReceive('pieChart')->andReturn([]);
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection);
2017-02-12 10:58:16 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.account.expense-budget', [1, '20120101', '20120131']));
$response->assertStatus(200);
}
2017-03-25 07:41:17 -05:00
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::expenseBudgetAll
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::getBudgetNames
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testExpenseBudgetAll(string $range)
{
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$transaction = factory(Transaction::class)->make();
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$generator->shouldReceive('pieChart')->andReturn([]);
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection);
$accountRepos->shouldReceive('oldestJournalDate')->andReturn(Carbon::createFromTimestamp(time())->startOfMonth());
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.account.expense-budget-all', [1]));
$response->assertStatus(200);
}
2017-02-12 10:58:16 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::expenseCategory
2017-03-25 07:41:17 -05:00
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::getCategoryNames
2017-02-12 10:58:16 -06:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testExpenseCategory(string $range)
{
2017-03-25 07:41:17 -05:00
$transaction = factory(Transaction::class)->make();
$category = factory(Category::class)->make();
2017-03-11 15:28:51 -06:00
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf();
2017-03-25 07:41:17 -05:00
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
2017-03-11 15:28:51 -06:00
$generator->shouldReceive('pieChart')->andReturn([]);
2017-03-25 07:41:17 -05:00
$categoryRepos->shouldReceive('getCategories')->andReturn(new Collection([$category]));
2017-03-11 15:28:51 -06:00
2017-02-12 10:58:16 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.account.expense-category', [1, '20120101', '20120131']));
$response->assertStatus(200);
}
2017-03-25 07:41:17 -05:00
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::expenseCategoryAll
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::getCategoryNames
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testExpenseCategoryAll(string $range)
{
$transaction = factory(Transaction::class)->make();
$category = factory(Category::class)->make();
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$generator->shouldReceive('pieChart')->andReturn([]);
$categoryRepos->shouldReceive('getCategories')->andReturn(new Collection([$category]));
$accountRepos->shouldReceive('oldestJournalDate')->andReturn(Carbon::createFromTimestamp(time())->startOfMonth());
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.account.expense-category-all', [1]));
$response->assertStatus(200);
}
2017-02-12 10:58:16 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::frontpage
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::__construct
2017-02-24 14:01:33 -06:00
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::accountBalanceChart
* @covers \FireflyIII\Generator\Chart\Basic\GeneratorInterface::multiSet
2017-02-12 10:58:16 -06:00
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testFrontpage(string $range)
{
2018-02-28 08:50:00 -06:00
$generator = $this->mock(GeneratorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
2017-03-11 15:28:51 -06:00
2017-03-25 07:41:17 -05:00
// change the preference:
Preferences::setForUser($this->user(), 'frontPageAccounts', []);
2017-03-11 15:28:51 -06:00
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection);
Steam::shouldReceive('balanceInRange')->andReturn([]);
$generator->shouldReceive('multiSet')->andReturn([]);
2018-02-28 08:50:00 -06:00
2017-02-12 10:58:16 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.account.frontpage'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::incomeCategory
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testIncomeCategory(string $range)
{
2017-03-25 07:41:17 -05:00
$transaction = factory(Transaction::class)->make();
$account = factory(Account::class)->make();
2017-03-11 15:28:51 -06:00
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT]])->andReturnSelf();
2017-03-25 07:41:17 -05:00
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
2017-03-11 15:28:51 -06:00
$generator->shouldReceive('pieChart')->andReturn([]);
2017-03-25 07:41:17 -05:00
$categoryRepos->shouldReceive('getCategories')->andReturn(new Collection([$account]));
2017-03-11 15:28:51 -06:00
2017-02-12 10:58:16 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.account.income-category', [1, '20120101', '20120131']));
$response->assertStatus(200);
}
2017-03-25 07:41:17 -05:00
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::incomeCategoryAll
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testIncomeCategoryAll(string $range)
{
$transaction = factory(Transaction::class)->make();
$account = factory(Account::class)->make();
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$categoryRepos = $this->mock(CategoryRepositoryInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT]])->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$generator->shouldReceive('pieChart')->andReturn([]);
$categoryRepos->shouldReceive('getCategories')->andReturn(new Collection([$account]));
$accountRepos->shouldReceive('oldestJournalDate')->andReturn(Carbon::createFromTimestamp(time())->startOfMonth());
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.account.income-category-all', [1]));
$response->assertStatus(200);
}
2017-02-12 10:58:16 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::period
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testPeriod(string $range)
{
2017-03-12 01:38:13 -06:00
$generator = $this->mock(GeneratorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2017-03-11 15:28:51 -06:00
$accountRepos->shouldReceive('oldestJournalDate')->andReturn(new Carbon);
Steam::shouldReceive('balanceInRange')->andReturn(['2012-01-01' => '0']);
$generator->shouldReceive('singleSet')->andReturn([]);
2017-02-12 10:58:16 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
2018-02-28 08:50:00 -06:00
$response = $this->get(route('chart.account.period', [1, '2012-01-01', '2012-01-31']));
2017-02-12 10:58:16 -06:00
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::report
2017-03-11 15:28:51 -06:00
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::accountBalanceChart
2017-02-12 10:58:16 -06:00
*/
public function testReport()
{
2018-02-28 08:50:00 -06:00
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
$currencyRepos->shouldReceive('findNull')->andReturn(TransactionCurrency::find(1));
2017-03-12 01:38:13 -06:00
$generator = $this->mock(GeneratorInterface::class);
2017-03-11 15:28:51 -06:00
$generator->shouldReceive('multiSet')->andreturn([]);
2017-03-17 10:34:57 -05:00
Steam::shouldReceive('balanceInRange')->andReturn(['2012-01-01' => '0']);
2017-03-11 15:28:51 -06:00
2017-02-12 10:58:16 -06:00
$this->be($this->user());
$response = $this->get(route('chart.account.report', ['1', '20120101', '20120131']));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::revenueAccounts
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testRevenueAccounts(string $range)
{
2017-07-07 23:28:44 -05:00
$account = factory(Account::class)->make();
2017-03-12 01:38:13 -06:00
$generator = $this->mock(GeneratorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2017-03-11 15:28:51 -06:00
2017-03-25 07:41:17 -05:00
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::REVENUE]])->andReturn(new Collection([$account]));
2017-03-11 15:28:51 -06:00
$generator->shouldReceive('singleSet')->andReturn([]);
Steam::shouldReceive('balancesByAccounts')->twice()->andReturn([]);
2017-03-11 15:28:51 -06:00
2017-02-12 10:58:16 -06:00
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.account.revenue'));
$response->assertStatus(200);
}
2017-02-16 15:33:32 -06:00
}