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

127 lines
4.0 KiB
PHP
Raw Normal View History

2018-09-15 13:43:57 +02:00
<?php
/**
* ConvertToTransferTest.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\Unit\TransactionRules\Actions;
use Exception;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
2019-08-01 06:21:44 +02:00
use FireflyIII\Models\Rule;
2018-09-15 13:43:57 +02:00
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\TransactionRules\Actions\ConvertToTransfer;
use Log;
use Tests\TestCase;
/**
*
* Class ConvertToTransferTest
*/
class ConvertToTransferTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-09-15 13:43:57 +02:00
}
/**
* Convert a deposit to a transfer.
*
* @covers \FireflyIII\TransactionRules\Actions\ConvertToTransfer
*/
public function testActDeposit(): void
2019-06-13 18:07:49 +02:00
{
2018-09-15 13:43:57 +02:00
$deposit = $this->getRandomDeposit();
/** @var Account $asset */
2019-08-01 06:21:44 +02:00
$asset = $this->getRandomAsset();
2018-09-15 13:43:57 +02:00
// mock used stuff:
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('setUser')->once();
2019-08-01 06:21:44 +02:00
$accountRepos->shouldReceive('findByName')->withArgs([$asset->name, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]])->andReturn($asset);
2018-09-15 13:43:57 +02:00
// fire the action:
2019-08-01 06:21:44 +02:00
$rule = new Rule;
$rule->title = 'OK';
2018-09-15 13:43:57 +02:00
$ruleAction = new RuleAction;
$ruleAction->action_value = $asset->name;
2019-08-01 06:21:44 +02:00
$ruleAction->rule = $rule;
2018-09-15 13:43:57 +02:00
$action = new ConvertToTransfer($ruleAction);
try {
$result = $action->act($deposit);
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertTrue($result);
// journal is now a transfer.
$deposit->refresh();
$this->assertEquals(TransactionType::TRANSFER, $deposit->transactionType->type);
}
/**
* Convert a withdrawal to a transfer.
*
* @covers \FireflyIII\TransactionRules\Actions\ConvertToTransfer
*/
public function testActWithdrawal(): void
{
$withdrawal = $this->getRandomWithdrawal();
/** @var Account $asset */
2019-08-01 06:21:44 +02:00
$asset = $this->getRandomAsset();
2018-09-15 13:43:57 +02:00
// mock used stuff:
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$accountRepos->shouldReceive('setUser')->once();
2019-08-01 06:21:44 +02:00
$accountRepos->shouldReceive('findByName')->withArgs([$asset->name, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]])->andReturn($asset);
2018-09-15 13:43:57 +02:00
// fire the action:
2019-08-01 06:21:44 +02:00
$rule = new Rule;
$rule->title = 'OK';
2018-09-15 13:43:57 +02:00
$ruleAction = new RuleAction;
$ruleAction->action_value = $asset->name;
2019-08-01 06:21:44 +02:00
$ruleAction->rule = $rule;
2018-09-15 13:43:57 +02:00
$action = new ConvertToTransfer($ruleAction);
try {
$result = $action->act($withdrawal);
} catch (Exception $e) {
$this->assertTrue(false, $e->getMessage());
}
$this->assertTrue($result);
// journal is now a transfer.
$withdrawal->refresh();
$this->assertEquals(TransactionType::TRANSFER, $withdrawal->transactionType->type);
}
2018-12-31 07:48:23 +01:00
}