firefly-iii/tests/Feature/Controllers/Transaction/ConvertControllerTest.php

414 lines
20 KiB
PHP
Raw Normal View History

2017-02-12 11:40:39 -06:00
<?php
/**
* ConvertControllerTest.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 11:40:39 -06:00
*/
2017-06-28 11:05:38 -05:00
declare(strict_types=1);
2017-02-12 11:40:39 -06:00
namespace Tests\Feature\Controllers\Transaction;
2017-03-04 04:20:57 -06:00
use DB;
2017-03-04 08:29:20 -06:00
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2017-02-12 11:40:39 -06:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-03-04 04:20:57 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;
2017-03-04 04:20:57 -06:00
use Illuminate\Support\MessageBag;
2017-02-12 11:40:39 -06:00
use Tests\TestCase;
/**
* Class ConvertControllerTest
*
2017-08-12 03:27:45 -05:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-02-12 11:40:39 -06:00
*/
class ConvertControllerTest extends TestCase
{
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::index
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::__construct
2017-02-12 11:40:39 -06:00
*/
public function testIndexDepositTransfer()
{
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getActiveAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once()->andReturn(new Collection);
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
2017-02-12 11:40:39 -06:00
$response = $this->get(route('transactions.convert.index', ['transfer', $deposit->id]));
$response->assertStatus(200);
$response->assertSee('Convert a deposit into a transfer');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::index
*/
public function testIndexDepositWithdrawal()
{
2017-03-04 04:20:57 -06:00
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getActiveAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once()->andReturn(new Collection);
2017-02-12 11:40:39 -06:00
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$this->be($this->user());
$response = $this->get(route('transactions.convert.index', ['withdrawal', $deposit->id]));
$response->assertStatus(200);
$response->assertSee('Convert a deposit into a withdrawal');
}
2017-03-04 04:20:57 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::index
*/
public function testIndexSameType()
{
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getActiveAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once()->andReturn(new Collection);
$this->be($this->user());
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$response = $this->get(route('transactions.convert.index', ['deposit', $deposit->id]));
$response->assertStatus(302);
$response->assertSessionHas('info');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::index
*/
public function testIndexSplit()
{
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getActiveAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once()->andReturn(new Collection);
$this->be($this->user());
$withdrawal = TransactionJournal::where('transaction_type_id', 1)
->whereNull('transaction_journals.deleted_at')
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->groupBy('transaction_journals.id')
->orderBy('ct', 'DESC')
->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')]);
$response = $this->get(route('transactions.convert.index', ['deposit', $withdrawal->id]));
$response->assertStatus(302);
$response->assertSessionHas('error');
}
2017-02-12 11:40:39 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::index
*/
public function testIndexTransferDeposit()
{
2017-03-04 04:20:57 -06:00
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getActiveAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once()->andReturn(new Collection);
2017-02-12 11:40:39 -06:00
$transfer = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->first();
$this->be($this->user());
$response = $this->get(route('transactions.convert.index', ['deposit', $transfer->id]));
$response->assertStatus(200);
$response->assertSee('Convert a transfer into a deposit');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::index
*/
public function testIndexTransferWithdrawal()
{
2017-03-04 04:20:57 -06:00
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getActiveAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once()->andReturn(new Collection);
2017-02-12 11:40:39 -06:00
$transfer = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->first();
$this->be($this->user());
$response = $this->get(route('transactions.convert.index', ['withdrawal', $transfer->id]));
$response->assertStatus(200);
$response->assertSee('Convert a transfer into a withdrawal');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::index
*/
public function testIndexWithdrawalDeposit()
{
2017-03-04 04:20:57 -06:00
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getActiveAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once()->andReturn(new Collection);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$response = $this->get(route('transactions.convert.index', ['deposit', $withdrawal->id]));
$response->assertStatus(200);
$response->assertSee('Convert a withdrawal into a deposit');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::index
*/
public function testIndexWithdrawalTransfer()
{
2017-03-04 04:20:57 -06:00
// mock stuff:
$repository = $this->mock(AccountRepositoryInterface::class);
$repository->shouldReceive('getActiveAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])->once()->andReturn(new Collection);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$response = $this->get(route('transactions.convert.index', ['transfer', $withdrawal->id]));
$response->assertStatus(200);
$response->assertSee('Convert a withdrawal into a transfer');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
2017-02-12 11:40:39 -06:00
*/
2017-03-04 04:20:57 -06:00
public function testPostIndexDepositTransfer()
2017-02-12 11:40:39 -06:00
{
2017-03-04 04:20:57 -06:00
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-04 08:29:20 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('find')->andReturn(new Account);
2017-03-04 04:20:57 -06:00
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$data = ['source_account_asset' => 1];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['transfer', $deposit->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$deposit->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
*/
public function testPostIndexDepositWithdrawal()
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-04 08:29:20 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('store')->andReturn(new Account);
2017-03-04 04:20:57 -06:00
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
2017-11-22 14:12:27 -06:00
$data = ['destination_account_expense' => 'New expense name.'];
2017-03-04 04:20:57 -06:00
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['withdrawal', $deposit->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$deposit->id]));
}
2017-06-28 11:05:38 -05:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
*/
public function testPostIndexDepositWithdrawalEmptyName()
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('getCashAccount')->andReturn(new Account)->once();
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
2017-11-22 14:12:27 -06:00
$data = ['destination_account_expense' => ''];
2017-06-28 11:05:38 -05:00
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['withdrawal', $deposit->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$deposit->id]));
}
2017-03-04 04:20:57 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
*/
public function testPostIndexErrored()
{
// mock stuff
$messageBag = new MessageBag;
$messageBag->add('fake', 'fake error');
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn($messageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = [
'destination_account_asset' => 2,
];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['transfer', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.convert.index', ['transfer', $withdrawal->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
*/
public function testPostIndexSameType()
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
$data = [
'destination_account_asset' => 2,
];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['withdrawal', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertSessionHas('error');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
*/
public function testPostIndexSplit()
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)
->whereNull('transaction_journals.deleted_at')
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->groupBy('transaction_journals.id')
->orderBy('ct', 'DESC')
->where('user_id', $this->user()->id)->first(['transaction_journals.id', DB::raw('count(transactions.`id`) as ct')]);
$data = [
'destination_account_asset' => 2,
];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['transfer', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertSessionHas('error');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
*/
public function testPostIndexTransferDeposit()
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-04 08:29:20 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2017-06-28 11:05:38 -05:00
$accountRepos->shouldReceive('store')->andReturn(new Account)->once();
2017-03-04 08:29:20 -06:00
2017-03-04 04:20:57 -06:00
$transfer = TransactionJournal::where('transaction_type_id', 3)->where('user_id', $this->user()->id)->first();
$data = ['source_account_revenue' => 'New rev'];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['deposit', $transfer->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$transfer->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
*/
public function testPostIndexWithdrawalDeposit()
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-04 08:29:20 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2017-06-28 11:05:38 -05:00
$accountRepos->shouldReceive('store')->andReturn(new Account)->once();
2017-03-04 08:29:20 -06:00
2017-03-04 04:20:57 -06:00
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
2017-11-22 14:12:27 -06:00
$data = ['source_account_revenue' => 'New revenue name.'];
2017-03-04 04:20:57 -06:00
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
2017-06-28 11:05:38 -05:00
*/
public function testPostIndexWithdrawalDepositEmptyName()
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('getCashAccount')->andReturn(new Account)->once();
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
2017-11-22 14:12:27 -06:00
$data = ['source_account_revenue' => ''];
2017-06-28 11:05:38 -05:00
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['deposit', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::postIndex
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getSourceAccount
* @covers \FireflyIII\Http\Controllers\Transaction\ConvertController::getDestinationAccount
2017-03-04 04:20:57 -06:00
*/
public function testPostIndexWithdrawalTransfer()
{
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('convert')->andReturn(new MessageBag);
$repository->shouldReceive('first')->once()->andReturn(new TransactionJournal);
2017-03-04 08:29:20 -06:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('find')->andReturn(new Account);
$withdrawal = TransactionJournal::where('transaction_type_id', 1)->where('user_id', $this->user()->id)->first();
2017-03-04 04:20:57 -06:00
$data = [
2017-02-12 11:40:39 -06:00
'destination_account_asset' => 2,
];
$this->be($this->user());
$response = $this->post(route('transactions.convert.index', ['transfer', $withdrawal->id]), $data);
$response->assertStatus(302);
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
}
2017-02-16 15:33:32 -06:00
}