Expanded test coverage.

This commit is contained in:
James Cole 2017-03-25 13:41:17 +01:00
parent 1fb0a64f31
commit 5770edcde2
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
26 changed files with 520 additions and 109 deletions

View File

@ -13,14 +13,13 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers\Auth;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\Request;
use Password;
/**
* @codeCoverageIgnore
*
* Class ForgotPasswordController
*
* @package FireflyIII\Http\Controllers\Auth
@ -46,21 +45,18 @@ class ForgotPasswordController extends Controller
*
* @return \Illuminate\Http\RedirectResponse
*/
public function sendResetLinkEmail(Request $request)
public function sendResetLinkEmail(Request $request, UserRepositoryInterface $repository)
{
$this->validate($request, ['email' => 'required|email']);
// verify if the user is not a demo user. If so, we give him back an error.
$user = User::where('email', $request->get('email'))->first();
if (!is_null($user) && $user->hasRole('demo')) {
return back()->withErrors(
['email' => trans('firefly.cannot_reset_demo_user')]
);
if (!is_null($user) && $repository->hasRole($user, 'demo')) {
return back()->withErrors(['email' => trans('firefly.cannot_reset_demo_user')]);
}
$response = $this->broker()->sendResetLink(
$request->only('email')
);
$response = $this->broker()->sendResetLink($request->only('email'));
if ($response === Password::RESET_LINK_SENT) {
return back()->with('status', trans($response));
@ -69,8 +65,6 @@ class ForgotPasswordController extends Controller
// If an error was returned by the password broker, we will get this message
// translated so we can notify a user of the problem. We'll redirect back
// to where the users came from so they can attempt this process again.
return back()->withErrors(
['email' => trans($response)]
);
return back()->withErrors(['email' => trans($response)]); // @codeCoverageIgnore
}
}

View File

@ -41,11 +41,12 @@ class TwoFactorController extends Controller
$user = auth()->user();
// to make sure the validator in the next step gets the secret, we push it in session
$secret = Preferences::get('twoFactorAuthSecret', null)->data;
$secretPreference = Preferences::get('twoFactorAuthSecret', null);
$secret = is_null($secretPreference) ? null : $secretPreference->data;
$title = strval(trans('firefly.two_factor_title'));
// make sure the user has two factor configured:
$has2FA = Preferences::get('twoFactorAuthEnabled', null)->data;
$has2FA = Preferences::get('twoFactorAuthEnabled', false)->data;
if (is_null($has2FA) || $has2FA === false) {
return redirect(route('index'));
}

View File

@ -497,8 +497,6 @@ class AccountController extends Controller
$cache->addProperty('chart.account.account-balance-chart');
$cache->addProperty($accounts);
if ($cache->has()) {
Log::debug('Return chart.account.account-balance-chart from cache.');
return $cache->get(); // @codeCoverageIgnore
}
Log::debug('Regenerate chart.account.account-balance-chart from scratch.');

View File

@ -9,7 +9,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Chart;
@ -24,7 +24,6 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
@ -41,8 +40,6 @@ use Response;
class BudgetReportController extends Controller
{
/** @var AccountRepositoryInterface */
private $accountRepository;
/** @var BudgetRepositoryInterface */
private $budgetRepository;
/** @var GeneratorInterface */
@ -58,7 +55,6 @@ class BudgetReportController extends Controller
function ($request, $next) {
$this->generator = app(GeneratorInterface::class);
$this->budgetRepository = app(BudgetRepositoryInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
return $next($request);
}
@ -133,8 +129,6 @@ class BudgetReportController extends Controller
if ($cache->has()) {
return Response::json($cache->get()); // @codeCoverageIgnore
}
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$format = Navigation::preferredCarbonLocalizedFormat($start, $end);
$function = Navigation::preferredEndOfPeriod($start, $end);
$chartData = [];
@ -163,7 +157,7 @@ class BudgetReportController extends Controller
'entries' => [],
];
}
$allBudgetLimits = $repository->getAllBudgetLimits($start, $end);
$allBudgetLimits = $this->budgetRepository->getAllBudgetLimits($start, $end);
$sumOfExpenses = [];
$leftOfLimits = [];
while ($currentStart < $end) {

View File

@ -9,7 +9,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Chart;
@ -23,8 +23,6 @@ use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Category;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
use Navigation;
@ -41,10 +39,6 @@ use Response;
class CategoryReportController extends Controller
{
/** @var AccountRepositoryInterface */
private $accountRepository;
/** @var CategoryRepositoryInterface */
private $categoryRepository;
/** @var GeneratorInterface */
private $generator;
@ -57,8 +51,6 @@ class CategoryReportController extends Controller
$this->middleware(
function ($request, $next) {
$this->generator = app(GeneratorInterface::class);
$this->categoryRepository = app(CategoryRepositoryInterface::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
return $next($request);
}

View File

@ -29,7 +29,6 @@ use Google2FA;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Validation\Validator;
use Session;
/**
* Class FireflyValidator
@ -66,7 +65,7 @@ class FireflyValidator extends Validator
return false;
}
$secret = Session::get('two-factor-secret');
$secret = session('two-factor-secret');
return Google2FA::verifyKey($secret, $value);
}

View File

@ -8,7 +8,10 @@
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use Carbon\Carbon;
/*
|--------------------------------------------------------------------------
@ -182,6 +185,7 @@ $factory->define(
'description' => $faker->words(3, true),
'source_account_name' => $faker->words(3, true),
'destination_account_id' => $faker->numberBetween(1, 10),
'date' => new Carbon,
'destination_account_name' => $faker->words(3, true),
'amount' => strval($faker->randomFloat(2, -100, 100)),
'budget_id' => 0,

View File

@ -8,7 +8,7 @@
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
@ -76,6 +76,23 @@ class CreateSupportTables extends Migration
}
}
private function createConfigurationTable()
{
if (!Schema::hasTable('configuration')) {
Schema::create(
'configuration', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('name', 50);
$table->text('data');
$table->unique(['name']);
}
);
}
}
/**
*
*/
@ -99,23 +116,6 @@ class CreateSupportTables extends Migration
}
}
private function createConfigurationTable()
{
if (!Schema::hasTable('configuration')) {
Schema::create(
'configuration', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->string('name', 50);
$table->text('data');
$table->unique(['name']);
}
);
}
}
/**
*
*/

View File

@ -8,7 +8,7 @@
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
@ -18,6 +18,14 @@ use Illuminate\Database\Schema\Blueprint;
*/
class CreateUsersTable extends Migration
{
/**
* Reverse the migrations.
*/
public function down()
{
Schema::drop('users');
}
/**
* Run the migrations.
*
@ -40,12 +48,4 @@ class CreateUsersTable extends Migration
);
}
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::drop('users');
}
}

View File

@ -8,7 +8,7 @@
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

View File

@ -8,7 +8,7 @@
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

View File

@ -8,7 +8,7 @@
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

View File

@ -43,11 +43,11 @@ class ChangesForV431 extends Migration
);
// change field "start_date" to "startdate"
// Schema::table(
// 'budget_limits', function (Blueprint $table) {
// $table->renameColumn('startdate', 'start_date');
// }
// );
// Schema::table(
// 'budget_limits', function (Blueprint $table) {
// $table->renameColumn('startdate', 'start_date');
// }
// );
}

View File

@ -9,7 +9,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use FireflyIII\Models\AccountType;
use Illuminate\Database\Seeder;

View File

@ -8,7 +8,7 @@
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use Illuminate\Database\Seeder;

View File

@ -9,8 +9,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use FireflyIII\Models\Role;
@ -30,7 +29,7 @@ class PermissionSeeder extends Seeder
$owner->save();
$demo = new Role;
$demo->name ='demo';
$demo->name = 'demo';
$demo->display_name = 'Demo User';
$demo->description = 'User is a demo user';
$demo->save();

View File

@ -9,7 +9,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use FireflyIII\Models\TransactionCurrency;
use Illuminate\Database\Seeder;

View File

@ -9,7 +9,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
use FireflyIII\Models\TransactionType;
use Illuminate\Database\Seeder;

View File

@ -28,7 +28,7 @@ Route::group(
// Password Reset Routes...
Route::get('password/reset/{token}', ['uses' => 'Auth\ResetPasswordController@showResetForm', 'as' => 'password.reset']);
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
Route::post('password/email', ['uses' => 'Auth\ForgotPasswordController@sendResetLinkEmail','as' => 'password.email']);
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
@ -50,7 +50,6 @@ Route::group(
/**
* For the two factor routes, the user must be logged in, but NOT 2FA. Account confirmation does not matter here.
*
* @deprecated
*/
Route::group(
['middleware' => 'user-logged-in-no-2fa', 'prefix' => 'two-factor', 'as' => 'two-factor.', 'namespace' => 'Auth'], function () {

View File

@ -0,0 +1,51 @@
<?php
/**
* ForgotPasswordControllerTest.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\Auth;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Tests\TestCase;
class ForgotPasswordControllerTest extends TestCase
{
/**
* @covers \FireflyIII\Http\Controllers\Auth\ForgotPasswordController::__construct
* @covers \FireflyIII\Http\Controllers\Auth\ForgotPasswordController::sendResetLinkEmail
*/
public function testSendResetLinkEmail()
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->andReturn(false);
$data = [
'email' => 'thegrumpydictator@gmail.com',
];
$response = $this->post(route('password.email'), $data);
$response->assertStatus(302);
}
/**
* @covers \FireflyIII\Http\Controllers\Auth\ForgotPasswordController::__construct
* @covers \FireflyIII\Http\Controllers\Auth\ForgotPasswordController::sendResetLinkEmail
*/
public function testSendResetLinkEmailDemo()
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->andReturn(true);
$data = [
'email' => 'thegrumpydictator@gmail.com',
];
$response = $this->post(route('password.email'), $data);
$response->assertStatus(302);
}
}

View File

@ -7,12 +7,13 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace Tests\Feature\Controllers\Auth;
use FireflyIII\Models\Preference;
use PragmaRX\Google2FA\Contracts\Google2FA;
use Preferences;
use Tests\TestCase;
@ -30,17 +31,53 @@ class TwoFactorControllerTest extends TestCase
{
$this->be($this->user());
$falsePreference = new Preference;
$falsePreference->data = true;
$truePref = new Preference;
$truePref->data = true;
$secretPreference = new Preference;
$secretPreference->data = 'BlablaSeecret';
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($falsePreference);
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference);
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference);
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($truePref)->twice();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once();
$response = $this->get(route('two-factor.index'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController::index
*/
public function testIndexNo2FA()
{
$this->be($this->user());
$falsePreference = new Preference;
$falsePreference->data = false;
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($falsePreference)->twice();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn(null)->once();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn(null)->once();
$response = $this->get(route('two-factor.index'));
$response->assertStatus(302);
$response->assertRedirect(route('index'));
}
/**
* @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController::index
* @expectedExceptionMessage Your two factor authentication secret is empty
*/
public function testIndexNoSecret()
{
$this->be($this->user());
$truePref = new Preference;
$truePref->data = true;
$secretPreference = new Preference;
$secretPreference->data = '';
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthEnabled', false])->andReturn($truePref)->twice();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret', null])->andReturn($secretPreference)->once();
Preferences::shouldReceive('get')->withArgs(['twoFactorAuthSecret'])->andReturn($secretPreference)->once();
$response = $this->get(route('two-factor.index'));
$response->assertStatus(500);
}
/**
* @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController::lostTwoFactor
*/
@ -59,4 +96,18 @@ class TwoFactorControllerTest extends TestCase
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Auth\TwoFactorController::postIndex
*/
public function testPostIndex()
{
$data = ['code' => '123456'];
$google = $this->mock(Google2FA::class);
$google->shouldReceive('verifyKey')->andReturn(true)->once();
$this->be($this->user());
$response = $this->post(route('two-factor.post'), $data);
$response->assertStatus(302);
}
}

View File

@ -7,7 +7,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace Tests\Feature\Controllers\Chart;
@ -15,12 +15,16 @@ namespace Tests\Feature\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Category;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use Illuminate\Support\Collection;
use Preferences;
use Steam;
use Tests\TestCase;
@ -58,10 +62,11 @@ class AccountControllerTest extends TestCase
*/
public function testExpenseAccounts(string $range)
{
$account = factory(Account::class)->make();
$generator = $this->mock(GeneratorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::EXPENSE, AccountType::BENEFICIARY]])->andReturn(new Collection);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::EXPENSE, AccountType::BENEFICIARY]])->andReturn(new Collection([$account]));
$generator->shouldReceive('singleSet')->andReturn([]);
Steam::shouldReceive('balancesById')->twice()->andReturn([]);
@ -84,12 +89,13 @@ class AccountControllerTest extends TestCase
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::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);
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$generator->shouldReceive('pieChart')->andReturn([]);
$budgetRepos->shouldReceive('getBudgets')->andReturn(new Collection);
@ -100,14 +106,48 @@ class AccountControllerTest extends TestCase
$response->assertStatus(200);
}
/**
* @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);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::expenseCategory
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::getCategoryNames
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testExpenseCategory(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);
@ -116,9 +156,9 @@ class AccountControllerTest extends TestCase
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection);
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$generator->shouldReceive('pieChart')->andReturn([]);
$categoryRepos->shouldReceive('getCategories')->andReturn(new Collection);
$categoryRepos->shouldReceive('getCategories')->andReturn(new Collection([$category]));
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
@ -126,6 +166,37 @@ class AccountControllerTest extends TestCase
$response->assertStatus(200);
}
/**
* @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);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::frontpage
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::__construct
@ -140,6 +211,9 @@ class AccountControllerTest extends TestCase
$generator = $this->mock(GeneratorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
// change the preference:
Preferences::setForUser($this->user(), 'frontPageAccounts', []);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection);
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection);
Steam::shouldReceive('balanceInRange')->andReturn([]);
@ -159,6 +233,8 @@ class AccountControllerTest extends TestCase
*/
public function testIncomeCategory(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);
@ -167,9 +243,9 @@ class AccountControllerTest extends TestCase
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT]])->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection);
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$generator->shouldReceive('pieChart')->andReturn([]);
$categoryRepos->shouldReceive('getCategories')->andReturn(new Collection);
$categoryRepos->shouldReceive('getCategories')->andReturn(new Collection([$account]));
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
@ -177,6 +253,36 @@ class AccountControllerTest extends TestCase
$response->assertStatus(200);
}
/**
* @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);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::period
* @dataProvider dateRangeProvider
@ -198,6 +304,17 @@ class AccountControllerTest extends TestCase
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::period
* @expectedExceptionMessage YYYY-MM-DD
*/
public function testPeriodBadDate()
{
$this->be($this->user());
$response = $this->get(route('chart.account.period', [1, 'bcdefed']));
$response->assertStatus(500);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::report
* @covers \FireflyIII\Http\Controllers\Chart\AccountController::accountBalanceChart
@ -221,10 +338,11 @@ class AccountControllerTest extends TestCase
*/
public function testRevenueAccounts(string $range)
{
$account = factory(Account::class)->make();
$generator = $this->mock(GeneratorInterface::class);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::REVENUE]])->andReturn(new Collection);
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::REVENUE]])->andReturn(new Collection([$account]));
$generator->shouldReceive('singleSet')->andReturn([]);
Steam::shouldReceive('balancesById')->twice()->andReturn([]);

View File

@ -14,6 +14,7 @@ namespace Tests\Feature\Controllers\Chart;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Transaction;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
use Tests\TestCase;
@ -52,12 +53,13 @@ class BillControllerTest extends TestCase
*/
public function testSingle()
{
$transaction = factory(Transaction::class)->make();
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setBills')->andReturnSelf()->once();
$collector->shouldReceive('getJournals')->andReturn(new Collection)->once();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]))->once();
$generator->shouldReceive('multiSet')->once()->andReturn([]);

View File

@ -7,7 +7,7 @@
* See the LICENSE file for details.
*/
declare(strict_types = 1);
declare(strict_types=1);
namespace Tests\Feature\Controllers\Chart;
@ -17,6 +17,7 @@ use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection;
@ -74,10 +75,25 @@ class BudgetControllerTest extends TestCase
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::budgetLimit
* @expectedExceptionMessage This budget limit is not part of this budget.
*/
public function testBudgetLimitWrongLimit()
{
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$this->be($this->user());
$response = $this->get(route('chart.budget.budget-limit', [1, 8]));
$response->assertStatus(500);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::frontpage
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::getExpensesForBudget
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::spentInPeriodWithout
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::spentInPeriodMulti
* @dataProvider dateRangeProvider
*
* @param string $range
@ -90,16 +106,94 @@ class BudgetControllerTest extends TestCase
$budget = factory(Budget::class)->make();
$budgetLimit = factory(BudgetLimit::class)->make();
$budgetLimit->budget_id = $budget->id;
$transaction = factory(Transaction::class)->make();
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]))->once();
$repository->shouldReceive('getBudgetLimits')->once()->andReturn(new Collection([$budgetLimit]));
$repository->shouldReceive('spentInPeriod')->andReturn('-100');
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('withoutBudget')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection);
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->once();
$collector->shouldReceive('withoutBudget')->andReturnSelf()->once();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]))->once();
$generator->shouldReceive('multiSet')->once()->andReturn([]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.budget.frontpage'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::frontpage
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::getExpensesForBudget
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::spentInPeriodWithout
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::spentInPeriodMulti
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testFrontpageMultiLimit(string $range)
{
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$budget = factory(Budget::class)->make();
$one = factory(BudgetLimit::class)->make();
$two = factory(BudgetLimit::class)->make();
$one->budget_id = $budget->id;
$two->budget_id = $budget->id;
$transaction = factory(Transaction::class)->make();
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]))->once();
$repository->shouldReceive('getBudgetLimits')->once()->andReturn(new Collection([$one, $two]));
$repository->shouldReceive('spentInPeriod')->andReturn('-100');
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->once();
$collector->shouldReceive('withoutBudget')->andReturnSelf()->once();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]))->once();
$generator->shouldReceive('multiSet')->once()->andReturn([]);
$this->be($this->user());
$this->changeDateRange($this->user(), $range);
$response = $this->get(route('chart.budget.frontpage'));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::frontpage
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::getExpensesForBudget
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::spentInPeriodWithout
* @covers \FireflyIII\Http\Controllers\Chart\BudgetController::spentInPeriodMulti
* @dataProvider dateRangeProvider
*
* @param string $range
*/
public function testFrontpageNoLimits(string $range)
{
$repository = $this->mock(BudgetRepositoryInterface::class);
$generator = $this->mock(GeneratorInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$budget = factory(Budget::class)->make();
$transaction = factory(Transaction::class)->make();
$repository->shouldReceive('getActiveBudgets')->andReturn(new Collection([$budget]));
$repository->shouldReceive('getBudgetLimits')->once()->andReturn(new Collection);
$repository->shouldReceive('spentInPeriod')->andReturn('-100');
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL]])->andReturnSelf()->once();
$collector->shouldReceive('setRange')->andReturnSelf()->once();
$collector->shouldReceive('withoutBudget')->andReturnSelf()->once();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]))->once();
$generator->shouldReceive('multiSet')->once()->andReturn([]);

View File

@ -0,0 +1,115 @@
<?php
/**
* BudgetReportControllerTest.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\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Helpers\Chart\MetaPieChartInterface;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection;
use Tests\TestCase;
class BudgetReportControllerTest extends TestCase
{
/**
* @covers \FireflyIII\Http\Controllers\Chart\BudgetReportController::accountExpense
* @covers \FireflyIII\Http\Controllers\Chart\BudgetReportController::__construct
*/
public function testAccountExpense()
{
$generator = $this->mock(GeneratorInterface::class);
$pieChart = $this->mock(MetaPieChartInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$pieChart->shouldReceive('setAccounts')->once()->andReturnSelf();
$pieChart->shouldReceive('setBudgets')->once()->andReturnSelf();
$pieChart->shouldReceive('setStart')->once()->andReturnSelf();
$pieChart->shouldReceive('setEnd')->once()->andReturnSelf();
$pieChart->shouldReceive('setCollectOtherObjects')->once()->andReturnSelf()->withArgs([false]);
$pieChart->shouldReceive('generate')->withArgs(['expense', 'account'])->andReturn([])->once();
$generator->shouldReceive('pieChart')->andReturn([])->once();
$this->be($this->user());
$response = $this->get(route('chart.budget.account-expense', ['1', '1', '20120101', '20120131', 0]));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\BudgetReportController::budgetExpense
*/
public function testBudgetExpense()
{
$generator = $this->mock(GeneratorInterface::class);
$pieChart = $this->mock(MetaPieChartInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$pieChart->shouldReceive('setAccounts')->once()->andReturnSelf();
$pieChart->shouldReceive('setBudgets')->once()->andReturnSelf();
$pieChart->shouldReceive('setStart')->once()->andReturnSelf();
$pieChart->shouldReceive('setEnd')->once()->andReturnSelf();
$pieChart->shouldReceive('setCollectOtherObjects')->once()->andReturnSelf()->withArgs([false]);
$pieChart->shouldReceive('generate')->withArgs(['expense', 'budget'])->andReturn([])->once();
$generator->shouldReceive('pieChart')->andReturn([])->once();
$this->be($this->user());
$response = $this->get(route('chart.budget.budget-expense', ['1', '1', '20120101', '20120131', 0]));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Chart\BudgetReportController::mainChart
* @covers \FireflyIII\Http\Controllers\Chart\BudgetReportController::filterBudgetLimits
* @covers \FireflyIII\Http\Controllers\Chart\BudgetReportController::getExpenses
* @covers \FireflyIII\Http\Controllers\Chart\BudgetReportController::groupByBudget
*/
public function testMainChart()
{
$generator = $this->mock(GeneratorInterface::class);
$pieChart = $this->mock(MetaPieChartInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$one = factory(BudgetLimit::class)->make();
$one->budget_id = 1;
$two = factory(BudgetLimit::class)->make();
$two->budget_id = 1;
$two->start_date = new Carbon('2012-01-01');
$two->end_date = new Carbon('2012-01-31');
$transaction = factory(Transaction::class)->make();
$transaction->transaction_amount = '-100';
$budgetRepos->shouldReceive('getAllBudgetLimits')->andReturn(new Collection([$one, $two]))->once();
$collector->shouldReceive('setAccounts')->andReturnSelf();
$collector->shouldReceive('setRange')->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL, TransactionType::TRANSFER]])->andReturnSelf();
$collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT, TransactionType::TRANSFER]])->andReturnSelf();
$collector->shouldReceive('disableFilter')->andReturnSelf();
$collector->shouldReceive('setBudgets')->andReturnSelf();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$generator->shouldReceive('multiSet')->andReturn([])->once();
$this->be($this->user());
$response = $this->get(route('chart.budget.main', ['1', '1', '20120101', '20120131', 0]));
$response->assertStatus(200);
}
}

View File

@ -45,7 +45,7 @@ class CategoryControllerTest extends TestCase
$repository->shouldReceive('spentInPeriod')->andReturn('0');
$repository->shouldReceive('earnedInPeriod')->andReturn('0');
$repository->shouldReceive('firstUseDate')->andReturn(new Carbon)->once();
$repository->shouldReceive('firstUseDate')->andReturn(new Carbon('1900-01-01'))->once();
$accountRepos->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->andReturn(new Collection)->once();
$generator->shouldReceive('multiSet')->once()->andReturn([]);