2017-10-04 15:27:20 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SetSourceAccountTest.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
*
|
2017-10-21 08:40:00 +02: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 14:42:21 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2017-10-04 15:27:20 +02:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Tests\Unit\TransactionRules\Actions;
|
|
|
|
|
|
|
|
use DB;
|
|
|
|
use FireflyIII\Models\AccountType;
|
|
|
|
use FireflyIII\Models\RuleAction;
|
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Models\TransactionType;
|
2018-02-28 20:18:47 +01:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2017-10-04 15:27:20 +02:00
|
|
|
use FireflyIII\TransactionRules\Actions\SetSourceAccount;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class SetSourceAccountTest
|
|
|
|
*/
|
|
|
|
class SetSourceAccountTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Give deposit existing revenue account.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct()
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act()
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findRevenueAccount()
|
|
|
|
*/
|
|
|
|
public function testActDepositExistingUpdated()
|
|
|
|
{
|
2018-02-28 20:18:47 +01:00
|
|
|
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
|
|
|
|
|
|
|
|
2017-10-04 15:27:20 +02:00
|
|
|
$type = TransactionType::whereType(TransactionType::DEPOSIT)->first();
|
2018-03-01 21:39:31 +01:00
|
|
|
|
|
|
|
// select split transactions to exclude them later:
|
|
|
|
$set = TransactionJournal::where('transaction_type_id', $type->id)->get(['transaction_journals.*']);
|
|
|
|
foreach ($set as $current) {
|
|
|
|
if ($current->transactions()->count() === 2) {
|
|
|
|
$journal = $current;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 15:27:20 +02:00
|
|
|
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
|
|
|
$source = $sourceTr->account;
|
|
|
|
$user = $journal->user;
|
|
|
|
$accountType = AccountType::whereType(AccountType::REVENUE)->first();
|
|
|
|
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();
|
|
|
|
$this->assertNotEquals($source->id, $account->id);
|
|
|
|
|
2018-02-28 20:18:47 +01:00
|
|
|
// find account? Return account:
|
|
|
|
$accountRepos->shouldReceive('setUser');
|
|
|
|
$accountRepos->shouldReceive('findByName')->andReturn($account);
|
|
|
|
|
2017-10-04 15:27:20 +02:00
|
|
|
// fire the action:
|
|
|
|
$ruleAction = new RuleAction;
|
|
|
|
$ruleAction->action_value = $account->name;
|
|
|
|
$action = new SetSourceAccount($ruleAction);
|
|
|
|
$result = $action->act($journal);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
// test journal for new account
|
|
|
|
$journal = TransactionJournal::find($journal->id);
|
|
|
|
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
|
|
|
$newSource = $sourceTr->account;
|
|
|
|
$this->assertNotEquals($source->id, $newSource->id);
|
|
|
|
$this->assertEquals($newSource->id, $account->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Give withdrawal existing asset account.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct()
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act()
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findAssetAccount()
|
|
|
|
*/
|
|
|
|
public function testActWithdrawalExistingUpdated()
|
|
|
|
{
|
2018-02-28 20:18:47 +01:00
|
|
|
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
|
|
|
$type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
2018-03-01 21:39:31 +01:00
|
|
|
|
|
|
|
// select split transactions to exclude them later:
|
|
|
|
$set = TransactionJournal::where('transaction_type_id', $type->id)->get(['transaction_journals.*']);
|
|
|
|
foreach ($set as $current) {
|
|
|
|
if ($current->transactions()->count() === 2) {
|
|
|
|
$journal = $current;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 20:18:47 +01:00
|
|
|
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
|
|
|
$source = $sourceTr->account;
|
|
|
|
$user = $journal->user;
|
|
|
|
$accountType = AccountType::whereType(AccountType::ASSET)->first();
|
|
|
|
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();
|
2017-10-04 15:27:20 +02:00
|
|
|
$this->assertNotEquals($source->id, $account->id);
|
|
|
|
|
2018-02-28 20:18:47 +01:00
|
|
|
|
|
|
|
$accountRepos->shouldReceive('setUser');
|
|
|
|
$accountRepos->shouldReceive('findByName')->andReturn($account);
|
|
|
|
|
2017-10-04 15:27:20 +02:00
|
|
|
// fire the action:
|
|
|
|
$ruleAction = new RuleAction;
|
|
|
|
$ruleAction->action_value = $account->name;
|
|
|
|
$action = new SetSourceAccount($ruleAction);
|
|
|
|
$result = $action->act($journal);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
// test journal for new account
|
|
|
|
$journal = TransactionJournal::find($journal->id);
|
|
|
|
$sourceTr = $journal->transactions()->where('amount', '<', 0)->first();
|
|
|
|
$newSource = $sourceTr->account;
|
|
|
|
$this->assertNotEquals($source->id, $newSource->id);
|
|
|
|
$this->assertEquals($newSource->id, $account->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test this on a split journal.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct()
|
|
|
|
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act()
|
|
|
|
*/
|
|
|
|
public function testSplitJournal()
|
|
|
|
{
|
2018-02-28 20:18:47 +01:00
|
|
|
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
|
|
|
$transaction = Transaction::orderBy('count', 'DESC')->groupBy('transaction_journal_id')
|
|
|
|
->get(['transaction_journal_id', DB::raw('COUNT(transaction_journal_id) as count')])
|
|
|
|
->first();
|
|
|
|
$journal = TransactionJournal::find($transaction->transaction_journal_id);
|
|
|
|
|
|
|
|
// mock
|
|
|
|
$accountRepos->shouldReceive('setUser');
|
2017-10-04 15:27:20 +02:00
|
|
|
// fire the action:
|
|
|
|
$ruleAction = new RuleAction;
|
|
|
|
$ruleAction->action_value = 'Some new asset ' . rand(1, 1000);
|
|
|
|
$action = new SetSourceAccount($ruleAction);
|
|
|
|
$result = $action->act($journal);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2017-11-08 09:05:10 +01:00
|
|
|
}
|