Files
firefly-iii/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php

155 lines
6.0 KiB
PHP
Raw Normal View History

2017-10-04 15:27:20 +02:00
<?php
/**
* SetSourceAccountTest.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2017-10-04 15:27:20 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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;
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
2019-08-17 10:48:28 +02:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2017-10-04 15:27:20 +02:00
*/
class SetSourceAccountTest extends TestCase
{
/**
* Give deposit existing revenue account.
*
2018-03-04 07:56:30 +01:00
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount
2017-10-04 15:27:20 +02:00
*/
2018-05-11 19:58:10 +02:00
public function testActDepositExistingUpdated(): void
2017-10-04 15:27:20 +02:00
{
2018-02-28 20:18:47 +01:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-09-02 20:13:25 +02:00
$deposit = $this->getRandomDeposit();
$sourceTr = $deposit->transactions()->where('amount', '<', 0)->first();
$source = $sourceTr->account;
$user = $deposit->user;
$accountType = AccountType::whereType(AccountType::REVENUE)->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
// 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);
2018-09-02 20:13:25 +02:00
$result = $action->act($deposit);
2017-10-04 15:27:20 +02:00
$this->assertTrue($result);
// test journal for new account
2018-09-02 20:13:25 +02:00
$sourceTr = $deposit->transactions()->where('amount', '<', 0)->first();
2017-10-04 15:27:20 +02:00
$newSource = $sourceTr->account;
$this->assertNotEquals($source->id, $newSource->id);
$this->assertEquals($newSource->id, $account->id);
}
2018-03-04 07:56:30 +01:00
/**
* Give deposit new revenueaccount.
*
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount
*/
2018-05-11 19:58:10 +02:00
public function testActDepositRevenue(): void
2018-03-04 07:56:30 +01:00
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2019-08-01 06:21:44 +02:00
$account = $this->getRandomRevenue();
2018-09-02 20:13:25 +02:00
$deposit = $this->getRandomDeposit();
2018-03-04 07:56:30 +01:00
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('findByName')->andReturn(null);
$accountRepos->shouldReceive('store')->once()->andReturn($account);
// fire the action:
$ruleAction = new RuleAction;
2019-07-31 16:53:09 +02:00
$ruleAction->action_value = 'Some new revenue #' . $this->randomInt();
2018-03-04 07:56:30 +01:00
$action = new SetSourceAccount($ruleAction);
2018-09-02 20:13:25 +02:00
$result = $action->act($deposit);
2018-03-04 07:56:30 +01:00
$this->assertTrue($result);
}
2017-10-04 15:27:20 +02:00
/**
* Give withdrawal existing asset account.
*
2018-03-04 07:56:30 +01:00
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount
2017-10-04 15:27:20 +02:00
*/
2018-05-11 19:58:10 +02:00
public function testActWithdrawalExistingUpdated(): void
2017-10-04 15:27:20 +02:00
{
2018-02-28 20:18:47 +01:00
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-09-02 20:13:25 +02:00
$withdrawal = $this->getRandomWithdrawal();
2018-09-02 20:13:25 +02:00
$sourceTr = $withdrawal->transactions()->where('amount', '<', 0)->first();
$source = $sourceTr->account;
2018-09-02 20:13:25 +02:00
$user = $withdrawal->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);
2018-09-02 20:13:25 +02:00
$result = $action->act($withdrawal);
2017-10-04 15:27:20 +02:00
$this->assertTrue($result);
// test journal for new account
2018-09-02 20:13:25 +02:00
$sourceTr = $withdrawal->transactions()->where('amount', '<', 0)->first();
2017-10-04 15:27:20 +02:00
$newSource = $sourceTr->account;
$this->assertNotEquals($source->id, $newSource->id);
$this->assertEquals($newSource->id, $account->id);
}
2018-03-04 07:56:30 +01:00
/**
* Give withdrawal not existing asset account.
*
* @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount
*/
2018-05-11 19:58:10 +02:00
public function testActWithdrawalNotExisting(): void
2018-03-04 07:56:30 +01:00
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
2018-09-02 20:13:25 +02:00
$withdrawal = $this->getRandomWithdrawal();
2018-03-04 07:56:30 +01:00
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('findByName')->andReturn(null);
// fire the action:
$ruleAction = new RuleAction;
2019-07-31 16:53:09 +02:00
$ruleAction->action_value = 'Some new account #' . $this->randomInt();
2018-03-04 07:56:30 +01:00
$action = new SetSourceAccount($ruleAction);
2018-09-02 20:13:25 +02:00
$result = $action->act($withdrawal);
2018-03-04 07:56:30 +01:00
$this->assertFalse($result);
}
2017-11-08 09:05:10 +01:00
}