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

187 lines
8.5 KiB
PHP
Raw Normal View History

2017-02-12 11:40:39 -06:00
<?php
/**
* SplitControllerTest.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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-02-12 11:40:39 -06:00
*/
2017-06-05 00:37:53 -05:00
declare(strict_types=1);
2017-02-12 11:40:39 -06:00
namespace Tests\Feature\Controllers\Transaction;
2017-03-04 08:29:20 -06:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
2017-03-03 23:53:46 -06:00
use FireflyIII\Models\AccountType;
2017-03-04 08:29:20 -06:00
use FireflyIII\Models\Transaction;
2017-02-12 11:40:39 -06:00
use FireflyIII\Models\TransactionJournal;
2017-03-03 23:53:46 -06:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-03-04 08:29:20 -06:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
2017-03-03 23:53:46 -06:00
use Illuminate\Support\Collection;
2017-03-04 08:29:20 -06:00
use Illuminate\Support\MessageBag;
2017-02-12 11:40:39 -06:00
use Tests\TestCase;
/**
* Class SplitControllerTest
*
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 SplitControllerTest extends TestCase
{
2017-03-05 11:15:38 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::edit
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::__construct
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::arrayFromJournal
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::getTransactionDataFromJournal
*/
2017-06-05 00:37:53 -05:00
public function testEdit()
2017-03-05 11:15:38 -06:00
{
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$accountRepository = $this->mock(AccountRepositoryInterface::class);
$budgetRepository = $this->mock(BudgetRepositoryInterface::class);
2017-08-20 07:36:14 -05:00
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$destination = $deposit->transactions()->where('amount', '>', 0)->first();
$account = $destination->account;
2017-06-05 00:37:53 -05:00
$transactions = factory(Transaction::class, 3)->make();
2017-03-05 11:15:38 -06:00
$tasker = $this->mock(JournalTaskerInterface::class);
$currencyRepository->shouldReceive('get')->once()->andReturn(new Collection);
$accountRepository->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])
2017-08-20 07:36:14 -05:00
->andReturn(new Collection([$account]))->once();
2017-03-05 11:15:38 -06:00
$budgetRepository->shouldReceive('getActiveBudgets')->andReturn(new Collection);
$tasker->shouldReceive('getTransactionsOverview')->andReturn($transactions->toArray());
$this->be($this->user());
$response = $this->get(route('transactions.split.edit', [$deposit->id]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-06-05 00:37:53 -05:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::edit
*/
public function testEditOpeningBalance()
{
$opening = TransactionJournal::where('transaction_type_id', 4)->where('user_id', $this->user()->id)->first();
$this->be($this->user());
$response = $this->get(route('transactions.split.edit', [$opening->id]));
$response->assertStatus(302);
}
2017-03-05 11:15:38 -06:00
2017-02-12 11:40:39 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::edit
2017-02-17 13:14:38 -06:00
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::__construct
2017-03-03 23:53:46 -06:00
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::arrayFromJournal
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::getTransactionDataFromJournal
2017-02-12 11:40:39 -06:00
*/
2017-06-05 00:37:53 -05:00
public function testEditSingle()
2017-02-12 11:40:39 -06:00
{
2017-03-03 23:53:46 -06:00
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
$accountRepository = $this->mock(AccountRepositoryInterface::class);
$budgetRepository = $this->mock(BudgetRepositoryInterface::class);
2017-06-05 00:37:53 -05:00
$transactions = factory(Transaction::class, 1)->make();
2017-03-04 08:29:20 -06:00
$tasker = $this->mock(JournalTaskerInterface::class);
2017-08-20 07:36:14 -05:00
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$destination = $deposit->transactions()->where('amount', '>', 0)->first();
$account = $destination->account;
2017-03-03 23:53:46 -06:00
$currencyRepository->shouldReceive('get')->once()->andReturn(new Collection);
$accountRepository->shouldReceive('getAccountsByType')->withArgs([[AccountType::DEFAULT, AccountType::ASSET]])
2017-08-20 07:36:14 -05:00
->andReturn(new Collection([$account]))->once();
2017-03-03 23:53:46 -06:00
$budgetRepository->shouldReceive('getActiveBudgets')->andReturn(new Collection);
2017-03-04 08:29:20 -06:00
$tasker->shouldReceive('getTransactionsOverview')->andReturn($transactions->toArray());
2017-03-03 23:53:46 -06:00
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$response = $this->get(route('transactions.split.edit', [$deposit->id]));
$response->assertStatus(200);
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::update
2017-03-03 23:53:46 -06:00
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::arrayFromInput
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::getTransactionDataFromRequest
2017-02-12 11:40:39 -06:00
*/
public function testUpdate()
{
2017-07-26 11:40:48 -05:00
$this->markTestIncomplete('Mockery cannot yet handle PHP7.1 null argument method things.');
2017-03-18 14:53:44 -05:00
$this->session(['transactions.edit-split.uri' => 'http://localhost']);
2017-02-12 11:40:39 -06:00
$deposit = TransactionJournal::where('transaction_type_id', 2)->where('user_id', $this->user()->id)->first();
$data = [
'id' => $deposit->id,
'what' => 'deposit',
'journal_description' => 'Updated salary',
'currency_id' => 1,
'journal_destination_account_id' => 1,
'journal_amount' => 1591,
'date' => '2014-01-24',
'tags' => '',
'transactions' => [
[
2017-06-05 00:37:53 -05:00
'description' => 'Split #1',
'source_account_name' => 'Job',
'transaction_currency_id' => 1,
'amount' => 1591,
'category' => '',
2017-02-12 11:40:39 -06:00
],
],
];
2017-03-04 08:29:20 -06:00
// mock stuff
$repository = $this->mock(JournalRepositoryInterface::class);
$repository->shouldReceive('updateSplitJournal')->andReturn($deposit);
2017-03-04 08:29:20 -06:00
$repository->shouldReceive('first')->times(2)->andReturn(new TransactionJournal);
2017-07-26 11:40:48 -05:00
2017-03-04 08:29:20 -06:00
$attachmentRepos = $this->mock(AttachmentHelperInterface::class);
$attachmentRepos->shouldReceive('saveAttachmentsForModel');
$attachmentRepos->shouldReceive('getMessages')->andReturn(new MessageBag);
2017-02-12 11:40:39 -06:00
$this->be($this->user());
$response = $this->post(route('transactions.split.update', [$deposit->id]), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
// journal is updated?
$response = $this->get(route('transactions.show', [$deposit->id]));
$response->assertStatus(200);
$response->assertSee('Updated salary');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-03-03 23:53:46 -06:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\SplitController::update
*/
public function testUpdateOpeningBalance()
{
2017-03-18 14:53:44 -05:00
$this->session(['transactions.edit-split.uri' => 'http://localhost']);
2017-03-03 23:53:46 -06:00
$opening = TransactionJournal::where('transaction_type_id', 4)->where('user_id', $this->user()->id)->first();
$data = [
'id' => $opening->id,
];
$this->be($this->user());
$response = $this->post(route('transactions.split.update', [$opening->id]), $data);
$response->assertStatus(302);
$response->assertSessionMissing('success');
}
2017-02-16 15:33:32 -06:00
}