Various code coverage changes and code updates.

This commit is contained in:
James Cole 2018-07-12 21:32:58 +02:00
parent 0217d9396a
commit 5846431b34
20 changed files with 398 additions and 224 deletions

View File

@ -25,7 +25,6 @@ namespace FireflyIII\Http\Controllers\Account;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\ReconciliationStoreRequest;
use FireflyIII\Http\Requests\ReconciliationUpdateRequest;
@ -38,14 +37,13 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Services\Internal\Update\CurrencyUpdateService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Log;
use Preferences;
/**
* Class ReconcileController.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ReconcileController extends Controller
{
@ -113,63 +111,6 @@ class ReconcileController extends Controller
)->with('data', $preFilled);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Request $request
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return JsonResponse
*
* @throws FireflyException
* @throws \Throwable
*/
public function overview(Request $request, Account $account, Carbon $start, Carbon $end): JsonResponse
{
if (AccountType::ASSET !== $account->accountType->type) {
throw new FireflyException(sprintf('Account %s is not an asset account.', $account->name));
}
$startBalance = $request->get('startBalance');
$endBalance = $request->get('endBalance');
$transactionIds = $request->get('transactions') ?? [];
$clearedIds = $request->get('cleared') ?? [];
$amount = '0';
$clearedAmount = '0';
$route = route('accounts.reconcile.submit', [$account->id, $start->format('Ymd'), $end->format('Ymd')]);
// get sum of transaction amounts:
$transactions = $this->repository->getTransactionsById($transactionIds);
$cleared = $this->repository->getTransactionsById($clearedIds);
$countCleared = 0;
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$amount = bcadd($amount, $transaction->amount);
}
/** @var Transaction $transaction */
foreach ($cleared as $transaction) {
if ($transaction->transactionJournal->date <= $end) {
$clearedAmount = bcadd($clearedAmount, $transaction->amount);
++$countCleared;
}
}
$difference = bcadd(bcadd(bcsub($startBalance, $endBalance), $clearedAmount), $amount);
$diffCompare = bccomp($difference, '0');
$return = [
'post_uri' => $route,
'html' => view(
'accounts.reconcile.overview', compact(
'account', 'start', 'diffCompare', 'difference', 'end', 'clearedIds', 'transactionIds', 'clearedAmount',
'startBalance', 'endBalance', 'amount',
'route', 'countCleared'
)
)->render(),
];
return response()->json($return);
}
/**
* @param Account $account
* @param Carbon|null $start
@ -243,10 +184,10 @@ class ReconcileController extends Controller
// get main transaction:
$transaction = $this->repository->getAssetTransaction($journal);
if(null === $transaction) {
if (null === $transaction) {
throw new FireflyException('The transaction data is incomplete. This is probably a bug. Apologies.');
}
$account = $transaction->account;
$account = $transaction->account;
return view('accounts.reconcile.show', compact('journal', 'subTitle', 'transaction', 'account'));
}
@ -338,52 +279,6 @@ class ReconcileController extends Controller
return redirect(route('accounts.show', [$account->id]));
}
/**
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*
* @throws FireflyException
* @throws \Throwable
*/
public function transactions(Account $account, Carbon $start, Carbon $end)
{
if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
return $this->redirectToOriginalAccount($account);
}
$startDate = clone $start;
$startDate->subDays(1);
$currencyId = (int)$this->accountRepos->getMetaValue($account, 'currency_id');
$currency = $this->currencyRepos->findNull($currencyId);
if (0 === $currency) {
$currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
}
$startBalance = round(app('steam')->balance($account, $startDate), $currency->decimal_places);
$endBalance = round(app('steam')->balance($account, $end), $currency->decimal_places);
// get the transactions
$selectionStart = clone $start;
$selectionStart->subDays(3);
$selectionEnd = clone $end;
$selectionEnd->addDays(3);
// grab transactions:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))
->setRange($selectionStart, $selectionEnd)->withBudgetInformation()->withOpposingAccount()->withCategoryInformation();
$transactions = $collector->getJournals();
$html = view(
'accounts.reconcile.transactions', compact('account', 'transactions', 'currency', 'start', 'end', 'selectionStart', 'selectionEnd')
)->render();
return response()->json(['html' => $html, 'startBalance' => $startBalance, 'endBalance' => $endBalance]);
}
/**
* @param ReconciliationUpdateRequest $request

View File

@ -147,15 +147,7 @@ class UserController extends Controller
$subTitleIcon = 'fa-user';
$information = $repository->getUserData($user);
return view(
'admin.users.show',
compact(
'title',
'mainTitleIcon',
'subTitle',
'subTitleIcon',
'information',
'user'
return view('admin.users.show', compact('title', 'mainTitleIcon', 'subTitle', 'subTitleIcon', 'information', 'user'
)
);
}

View File

@ -0,0 +1,207 @@
<?php
/**
* ReconcileController.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 FireflyIII\Http\Controllers\Json;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Transaction;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Services\Internal\Update\CurrencyUpdateService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
/**
*
* Class ReconcileController
*/
class ReconcileController extends Controller
{
/** @var CurrencyUpdateService */
private $accountRepos;
/** @var AccountRepositoryInterface */
private $currencyRepos;
/** @var JournalRepositoryInterface */
private $repository;
/**
*
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
app('view')->share('mainTitleIcon', 'fa-credit-card');
app('view')->share('title', trans('firefly.accounts'));
$this->repository = app(JournalRepositoryInterface::class);
$this->accountRepos = app(AccountRepositoryInterface::class);
$this->currencyRepos = app(CurrencyRepositoryInterface::class);
return $next($request);
}
);
}
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Request $request
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return JsonResponse
*
* @throws FireflyException
* @throws \Throwable
*/
public function overview(Request $request, Account $account, Carbon $start, Carbon $end): JsonResponse
{
if (AccountType::ASSET !== $account->accountType->type) {
throw new FireflyException(sprintf('Account %s is not an asset account.', $account->name));
}
$startBalance = $request->get('startBalance');
$endBalance = $request->get('endBalance');
$transactionIds = $request->get('transactions') ?? [];
$clearedIds = $request->get('cleared') ?? [];
$amount = '0';
$clearedAmount = '0';
$route = route('accounts.reconcile.submit', [$account->id, $start->format('Ymd'), $end->format('Ymd')]);
// get sum of transaction amounts:
$transactions = $this->repository->getTransactionsById($transactionIds);
$cleared = $this->repository->getTransactionsById($clearedIds);
$countCleared = 0;
/** @var Transaction $transaction */
foreach ($transactions as $transaction) {
$amount = bcadd($amount, $transaction->amount);
}
/** @var Transaction $transaction */
foreach ($cleared as $transaction) {
if ($transaction->transactionJournal->date <= $end) {
$clearedAmount = bcadd($clearedAmount, $transaction->amount);
++$countCleared;
}
}
$difference = bcadd(bcadd(bcsub($startBalance, $endBalance), $clearedAmount), $amount);
$diffCompare = bccomp($difference, '0');
$return = [
'post_uri' => $route,
'html' => view(
'accounts.reconcile.overview', compact(
'account', 'start', 'diffCompare', 'difference', 'end', 'clearedIds', 'transactionIds', 'clearedAmount',
'startBalance', 'endBalance', 'amount',
'route', 'countCleared'
)
)->render(),
];
return response()->json($return);
}
/**
* @param Account $account
* @param Carbon $start
* @param Carbon $end
*
* @return mixed
*
* @throws FireflyException
* @throws \Throwable
*/
public function transactions(Account $account, Carbon $start, Carbon $end)
{
if (AccountType::INITIAL_BALANCE === $account->accountType->type) {
return $this->redirectToOriginalAccount($account);
}
$startDate = clone $start;
$startDate->subDays(1);
$currencyId = (int)$this->accountRepos->getMetaValue($account, 'currency_id');
$currency = $this->currencyRepos->findNull($currencyId);
if (0 === $currency) {
$currency = app('amount')->getDefaultCurrency(); // @codeCoverageIgnore
}
$startBalance = round(app('steam')->balance($account, $startDate), $currency->decimal_places);
$endBalance = round(app('steam')->balance($account, $end), $currency->decimal_places);
// get the transactions
$selectionStart = clone $start;
$selectionStart->subDays(3);
$selectionEnd = clone $end;
$selectionEnd->addDays(3);
// grab transactions:
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class);
$collector->setAccounts(new Collection([$account]))
->setRange($selectionStart, $selectionEnd)->withBudgetInformation()->withOpposingAccount()->withCategoryInformation();
$transactions = $collector->getJournals();
$html = view(
'accounts.reconcile.transactions', compact('account', 'transactions', 'currency', 'start', 'end', 'selectionStart', 'selectionEnd')
)->render();
return response()->json(['html' => $html, 'startBalance' => $startBalance, 'endBalance' => $endBalance]);
}
/**
* @param Account $account
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*
* @throws FireflyException
*/
private function redirectToOriginalAccount(Account $account)
{
/** @var Transaction $transaction */
$transaction = $account->transactions()->first();
if (null === $transaction) {
throw new FireflyException(sprintf('Expected a transaction. Account #%d has none. BEEP, error.', $account->id)); // @codeCoverageIgnore
}
$journal = $transaction->transactionJournal;
/** @var Transaction $opposingTransaction */
$opposingTransaction = $journal->transactions()->where('transactions.id', '!=', $transaction->id)->first();
if (null === $opposingTransaction) {
throw new FireflyException('Expected an opposing transaction. This account has none. BEEP, error.'); // @codeCoverageIgnore
}
return redirect(route('accounts.show', [$opposingTransaction->account_id]));
}
}

View File

@ -26,6 +26,7 @@ use Closure;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\Request;
use Log;
/**
* Class IsDemoUser.
@ -51,6 +52,7 @@ class IsDemoUser
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
if ($repository->hasRole($user, 'demo')) {
Log::info('User is a demo user.');
$request->session()->flash('info', (string)trans('firefly.not_available_demo_user'));
$current = $request->url();
$previous = $request->session()->previousUrl();

View File

@ -117,12 +117,14 @@ Route::group(
// reconcile routes:
Route::get('reconcile/{account}/index/{start_date?}/{end_date?}', ['uses' => 'Account\ReconcileController@reconcile', 'as' => 'reconcile']);
Route::get(
'reconcile/{account}/transactions/{start_date?}/{end_date?}', ['uses' => 'Account\ReconcileController@transactions', 'as' => 'reconcile.transactions']
);
Route::get('reconcile/{account}/overview/{start_date?}/{end_date?}', ['uses' => 'Account\ReconcileController@overview', 'as' => 'reconcile.overview']);
Route::post('reconcile/{account}/submit/{start_date?}/{end_date?}', ['uses' => 'Account\ReconcileController@submit', 'as' => 'reconcile.submit']);
// reconcile JSON routes
Route::get('reconcile/{account}/overview/{start_date?}/{end_date?}', ['uses' => 'Json\ReconcileController@overview', 'as' => 'reconcile.overview']);
Route::get('reconcile/{account}/transactions/{start_date?}/{end_date?}', ['uses' => 'Json\ReconcileController@transactions', 'as' => 'reconcile.transactions']);
// show reconciliation
Route::get('reconcile/show/{tj}', ['uses' => 'Account\ReconcileController@show', 'as' => 'reconcile.show']);
Route::get('reconcile/edit/{tj}', ['uses' => 'Account\ReconcileController@edit', 'as' => 'reconcile.edit']);

View File

@ -87,48 +87,6 @@ class ReconcileControllerTest extends TestCase
$response->assertRedirect(route('transactions.edit', [$journal->id]));
}
/**
* Test overview of reconciliation.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
*/
public function testOverview(): void
{
$transactions = $this->user()->transactions()->inRandomOrder()->take(3)->get();
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->andReturn(new TransactionJournal);
$repository->shouldReceive('getTransactionsById')->andReturn($transactions)->twice();
$parameters = [
'startBalance' => '0',
'endBalance' => '10',
'transactions' => [1, 2, 3],
'cleared' => [4, 5, 6],
];
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.overview', [1, '20170101', '20170131']) . '?' . http_build_query($parameters));
$response->assertStatus(200);
}
/**
* Test overview when it's not an asset.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
* @expectedExceptionMessage is not an asset account
*/
public function testOverviewNotAsset(): void
{
$account = $this->user()->accounts()->where('account_type_id', '!=', 3)->first();
$parameters = [
'startBalance' => '0',
'endBalance' => '10',
'transactions' => [1, 2, 3],
'cleared' => [4, 5, 6],
];
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.overview', [$account->id, '20170101', '20170131']) . '?' . http_build_query($parameters));
$response->assertStatus(500);
}
/**
* Test showing the reconciliation.
@ -275,33 +233,6 @@ class ReconcileControllerTest extends TestCase
$response->assertSessionHas('success');
}
/**
* List transactions for reconciliation view.
*
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
*/
public function testTransactions(): void
{
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('findNull')->once()->andReturn(TransactionCurrency::find(1));
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.transactions', [1, '20170101', '20170131']));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
*/
public function testTransactionsInitialBalance(): void
{
$transaction = Transaction::leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->where('accounts.user_id', $this->user()->id)->where('accounts.account_type_id', 6)->first(['account_id']);
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.transactions', [$transaction->account_id, '20170101', '20170131']));
$response->assertStatus(302);
}
/**
* @covers \FireflyIII\Http\Controllers\Account\ReconcileController
* @covers \FireflyIII\Http\Requests\ReconciliationUpdateRequest

View File

@ -25,6 +25,7 @@ namespace Tests\Feature\Controllers\Admin;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
use Mockery;
use Tests\TestCase;
/**
@ -47,7 +48,8 @@ class UserControllerTest extends TestCase
public function testDelete(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->once()->andReturn(true);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
$this->be($this->user());
$response = $this->get(route('admin.users.delete', [1]));
$response->assertStatus(200);
@ -62,7 +64,8 @@ class UserControllerTest extends TestCase
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('destroy')->once();
$repository->shouldReceive('hasRole')->once()->andReturn(true);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
$this->be($this->user());
$response = $this->post(route('admin.users.destroy', ['2']));
@ -76,6 +79,8 @@ class UserControllerTest extends TestCase
public function testEdit(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
$this->be($this->user());
$response = $this->get(route('admin.users.edit', [1]));
$response->assertStatus(200);
@ -89,6 +94,8 @@ class UserControllerTest extends TestCase
public function testIndex(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
//$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->times(2)->andReturn(true);
$user = $this->user();
$repository->shouldReceive('all')->andReturn(new Collection([$user]));
@ -105,6 +112,7 @@ class UserControllerTest extends TestCase
public function testShow(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
$repository->shouldReceive('getUserData')->andReturn(
[
'export_jobs_success' => 0,
@ -129,6 +137,8 @@ class UserControllerTest extends TestCase
$repository->shouldReceive('changePassword')->once();
$repository->shouldReceive('changeStatus')->once();
$repository->shouldReceive('updateEmail')->once();
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
$data = [
'id' => 1,
'email' => 'test@example.com',

View File

@ -243,19 +243,21 @@ class BillControllerTest extends TestCase
}
/**
* @covers \FireflyIII\Http\Controllers\BillController::store
* @covers \FireflyIII\Http\Controllers\BillController
* @covers \FireflyIII\Http\Requests\BillFormRequest
* @covers \FireflyIII\Http\Requests\Request
*/
public function testStore(): void
{
$this->be($this->user());
$bill = $this->user()->bills()->first();
// mock stuff
$attachHelper = $this->mock(AttachmentHelperInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(BillRepositoryInterface::class);
$ruleGroupRepos =$this->mock(RuleGroupRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('store')->andReturn(new Bill);
$repository->shouldReceive('store')->andReturn($bill);
$attachHelper->shouldReceive('saveAttachmentsForModel');
$attachHelper->shouldReceive('getMessages')->andReturn(new MessageBag);
$ruleGroupRepos->shouldReceive('count')->andReturn(1);
@ -271,7 +273,7 @@ class BillControllerTest extends TestCase
'repeat_freq' => 'monthly',
];
$this->session(['bills.create.uri' => 'http://localhost']);
$this->be($this->user());
$response = $this->post(route('bills.store'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
@ -289,8 +291,9 @@ class BillControllerTest extends TestCase
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(BillRepositoryInterface::class);
$ruleGroupRepos =$this->mock(RuleGroupRepositoryInterface::class);
$bill = $this->user()->bills()->first();
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('store')->andReturn(new Bill);
$repository->shouldReceive('store')->andReturn($bill);
$attachHelper->shouldReceive('saveAttachmentsForModel');
$attachHelper->shouldReceive('getMessages')->andReturn(new MessageBag);
$ruleGroupRepos->shouldReceive('count')->andReturn(1);

View File

@ -103,6 +103,7 @@ class IndexControllerTest extends TestCase
$importJob = new ImportJob;
$importJob->provider = 'fake';
$importJob->key = 'fake_job_1';
$importJob->user_id = 1;
// mock calls
$userRepository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(true)->once();
@ -135,6 +136,7 @@ class IndexControllerTest extends TestCase
$importJob = new ImportJob;
$importJob->provider = 'fake';
$importJob->key = 'fake_job_2';
$importJob->user_id = 1;
// mock call:
$userRepository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(true)->once();
@ -168,6 +170,7 @@ class IndexControllerTest extends TestCase
$importJob = new ImportJob;
$importJob->provider = 'file';
$importJob->key = 'file_job_1';
$importJob->user_id =1;
// mock calls
$fakePrerequisites->shouldReceive('setUser')->once();

View File

@ -0,0 +1,122 @@
<?php
/**
* ReconcileControllerTest.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\Feature\Controllers\Json;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Log;
use Tests\TestCase;
/**
*
* Class ReconcileControllerTest
*/
class ReconcileControllerTest extends TestCase
{
/**
*
*/
public function setUp()
{
parent::setUp();
Log::debug(sprintf('Now in %s.', \get_class($this)));
}
/**
* Test overview of reconciliation.
*
* @covers \FireflyIII\Http\Controllers\Json\ReconcileController
*/
public function testOverview(): void
{
$transactions = $this->user()->transactions()->inRandomOrder()->take(3)->get();
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->andReturn(new TransactionJournal);
$repository->shouldReceive('getTransactionsById')->andReturn($transactions)->twice();
$parameters = [
'startBalance' => '0',
'endBalance' => '10',
'transactions' => [1, 2, 3],
'cleared' => [4, 5, 6],
];
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.overview', [1, '20170101', '20170131']) . '?' . http_build_query($parameters));
$response->assertStatus(200);
}
/**
* Test overview when it's not an asset.
*
* @covers \FireflyIII\Http\Controllers\Json\ReconcileController
* @expectedExceptionMessage is not an asset account
*/
public function testOverviewNotAsset(): void
{
$account = $this->user()->accounts()->where('account_type_id', '!=', 3)->first();
$parameters = [
'startBalance' => '0',
'endBalance' => '10',
'transactions' => [1, 2, 3],
'cleared' => [4, 5, 6],
];
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.overview', [$account->id, '20170101', '20170131']) . '?' . http_build_query($parameters));
$response->assertStatus(500);
}
/**
* List transactions for reconciliation view.
*
* @covers \FireflyIII\Http\Controllers\Json\ReconcileController
*/
public function testTransactions(): void
{
$repository = $this->mock(CurrencyRepositoryInterface::class);
$repository->shouldReceive('findNull')->once()->andReturn(TransactionCurrency::find(1));
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.transactions', [1, '20170101', '20170131']));
$response->assertStatus(200);
}
/**
* @covers \FireflyIII\Http\Controllers\Json\ReconcileController
*/
public function testTransactionsInitialBalance(): void
{
$transaction = Transaction::leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
->where('accounts.user_id', $this->user()->id)->where('accounts.account_type_id', 6)->first(['account_id']);
$this->be($this->user());
$response = $this->get(route('accounts.reconcile.transactions', [$transaction->account_id, '20170101', '20170131']));
$response->assertStatus(302);
}
}

View File

@ -114,10 +114,10 @@ class ReportControllerTest extends TestCase
$popupHelper = $this->mock(PopupReportInterface::class);
$account = factory(Account::class)->make();
$popupHelper->shouldReceive('balanceForNoBudget')->once()->andReturn(new Collection);
$popupHelper->shouldReceive('balanceForNoBudget')->andReturn(new Collection);
$budgetRepos->shouldReceive('findNull')->andReturn(new Budget)->once()->withArgs([0]);
$accountRepos->shouldReceive('findNull')->andReturn($account)->once()->withArgs([1]);
$popupHelper->shouldReceive('balanceForBudget')->once()->andReturn(new Collection);
$this->be($this->user());
$arguments = [

View File

@ -158,27 +158,13 @@ class ProfileControllerTest extends TestCase
$response->assertRedirect(route('profile.index'));
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController
*/
public function testEnable2FADemo(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(true);
$this->be($this->user());
$response = $this->post(route('profile.enable2FA'));
$response->assertStatus(302);
$response->assertRedirect(route('profile.index'));
}
/**
* @covers \FireflyIII\Http\Controllers\ProfileController
*/
public function testEnable2FANoSecret(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->twice()->andReturn(false);
// ask about language:
$langPreference = new Preference;
@ -220,7 +206,7 @@ class ProfileControllerTest extends TestCase
public function testEnable2FASecret(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->twice()->andReturn(false);
// ask about language:
$langPreference = new Preference;
@ -291,6 +277,7 @@ class ProfileControllerTest extends TestCase
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('findByEmail')->once()->andReturn(null);
$repository->shouldReceive('changeEmail')->once()->andReturn(true);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$this->be($this->user());
$response = $this->post(route('profile.change-email.post'), $data);
@ -310,6 +297,7 @@ class ProfileControllerTest extends TestCase
];
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('findByEmail')->once()->andReturn(new User);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$this->be($this->user());
$response = $this->post(route('profile.change-email.post'), $data);
@ -324,6 +312,7 @@ class ProfileControllerTest extends TestCase
public function testPostChangeEmailSame(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$data = [
'email' => $this->user()->email,
];
@ -345,6 +334,7 @@ class ProfileControllerTest extends TestCase
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('changePassword');
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$data = [
'current_password' => 'james',
@ -368,6 +358,7 @@ class ProfileControllerTest extends TestCase
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('changePassword');
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$data = [
'current_password' => 'james3',
@ -391,6 +382,7 @@ class ProfileControllerTest extends TestCase
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('changePassword');
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$data = [
'current_password' => 'james',
@ -440,6 +432,7 @@ class ProfileControllerTest extends TestCase
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('destroy')->once();
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$data = [
'password' => 'james',
];
@ -458,6 +451,7 @@ class ProfileControllerTest extends TestCase
$repository = $this->mock(UserRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$data = [
'password' => 'james2',
];
@ -473,6 +467,8 @@ class ProfileControllerTest extends TestCase
*/
public function testRegenerate(): void
{
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->once()->andReturn(false);
$token = '';
$currentToken = Preference::where('user_id', $this->user()->id)->where('name', 'access_token')->first();
if (null !== $currentToken) {

View File

@ -264,7 +264,7 @@ class TagControllerTest extends TestCase
$repository = $this->mock(TagRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('find')->andReturn(new Tag);
$repository->shouldReceive('findNull')->andReturn(null);
$repository->shouldReceive('store')->andReturn(new Tag);
$this->session(['tags.create.uri' => 'http://localhost']);
@ -299,7 +299,7 @@ class TagControllerTest extends TestCase
];
$repository->shouldReceive('update');
$repository->shouldReceive('find')->andReturn(Tag::first());
$repository->shouldReceive('findNull')->andReturn(Tag::first());
$this->be($this->user());
$response = $this->post(route('tags.update', [1]), $data);

View File

@ -188,7 +188,7 @@ class MassControllerTest extends TestCase
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('update')->once();
$repository->shouldReceive('find')->once()->andReturn($deposit);
$repository->shouldReceive('findNull')->once()->andReturn($deposit);
$repository->shouldReceive('getTransactionType')->andReturn('Deposit');
$repository->shouldReceive('getNoteText')->andReturn('Some note');

View File

@ -84,6 +84,8 @@ class SingleControllerTest extends TestCase
$journalRepos->shouldReceive('getJournalBudgetId')->andReturn(0);
$journalRepos->shouldReceive('getJournalCategoryName')->andReturn('');
$journalRepos->shouldReceive('getTags')->andReturn([]);
$journalRepos->shouldReceive('getMetaField')->andReturnNull();
$note = new Note();
$note->id = 5;

View File

@ -323,7 +323,7 @@ class TransactionControllerTest extends TestCase
$journal->date = new Carbon('2016-01-01');
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('find')->once()->andReturn($journal);
$repository->shouldReceive('findNull')->once()->andReturn($journal);
$repository->shouldReceive('setOrder')->once()->andReturn(true);
$data = [

View File

@ -92,6 +92,8 @@ class UserEventHandlerTest extends TestCase
$listener = new UserEventHandler();
// mock stuff
$repository->shouldReceive('hasRole')->once()->andReturn(false);
$repository->shouldReceive('count')->once()->andReturn(1);
$repository->shouldReceive('getRole')->once()->andReturn(null);
$repository->shouldReceive('attachRole')->once()->withArgs([Mockery::any(), 'owner']);
@ -112,6 +114,7 @@ class UserEventHandlerTest extends TestCase
$listener = new UserEventHandler();
// mock stuff
$repository->shouldReceive('hasRole')->once()->andReturn(false);
$repository->shouldReceive('count')->once()->andReturn(1);
$repository->shouldReceive('getRole')->once()->andReturn(new Role);
$repository->shouldReceive('attachRole')->once()->withArgs([Mockery::any(), 'owner']);
@ -131,6 +134,7 @@ class UserEventHandlerTest extends TestCase
$listener = new UserEventHandler();
// mock stuff
$repository->shouldReceive('hasRole')->once()->andReturn(true);
$repository->shouldReceive('count')->once()->andReturn(1);
$listener->checkSingleUserIsAdmin($event);

View File

@ -59,10 +59,11 @@ class VersionCheckEventHandlerTest extends TestCase
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig);
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
// request thing:
$request->shouldReceive('call')->once();
$request->shouldReceive('getReleases')->once()->andThrow(new FireflyException('Errrr'));
$request->shouldReceive('call')->once()->andThrow(new FireflyException('Errrr'));
$request->shouldReceive('getReleases')->once();
$handler = new VersionCheckEventHandler;
@ -140,6 +141,7 @@ class VersionCheckEventHandlerTest extends TestCase
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig);
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
@ -164,6 +166,7 @@ class VersionCheckEventHandlerTest extends TestCase
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig);
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);

View File

@ -127,8 +127,8 @@ class SandstormTest extends TestCase
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('count')->twice()->andReturn(0);
$repository->shouldReceive('store')->once()->andReturn($this->user());
$repository->shouldReceive('attachRole')->twice()->andReturn(true);
$repository->shouldReceive('getRole')->once()->andReturn(new Role);
$repository->shouldReceive('attachRole')->once()->andReturn(true);
//$repository->shouldReceive('getRole')->once()->andReturn(new Role);
$repository->shouldReceive('hasRole')->andReturn(false);
$response = $this->get('/_test/sandstorm', ['X-Sandstorm-User-Id' => 'abcd']);

View File

@ -72,10 +72,12 @@ class ToAccountStartsTest extends TestCase
$count = $journal->transactions()->where('amount', '>', 0)->count();
$transaction = $journal->transactions()->where('amount', '>', 0)->first();
$account = null === $transaction ? null : $transaction->account;
Log::debug(sprintf('Journal with id #%d', $journal->id));
Log::debug(sprintf('Count of transactions is %d', $count));
Log::debug(sprintf('Account is null: %s', var_export(null === $account, true)));
} while ($loopCount < 30 && $count !== 1 && null !== $account);
} while ($loopCount < 30 && $count < 1 && null !== $account);
Log::debug(sprintf('Loop has ended. loopCount is %d', $loopCount));
$trigger = ToAccountStarts::makeFromStrings('bla-bla-bla' . $account->name, false);