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

135 lines
4.6 KiB
PHP
Raw Normal View History

2018-09-15 13:43:57 +02:00
<?php
/**
* ConvertToDepositTest.php
2020-02-16 13:59:55 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-09-15 13:43:57 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-09-15 13:43:57 +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.
2018-09-15 13:43:57 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-09-15 13:43:57 +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.
2018-09-15 13:43:57 +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/>.
2018-09-15 13:43:57 +02:00
*/
declare(strict_types=1);
namespace Tests\Unit\TransactionRules\Actions;
use Exception;
use FireflyIII\Factory\AccountFactory;
2020-08-23 07:42:14 +02:00
use FireflyIII\Models\Account;
2018-09-15 13:43:57 +02:00
use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
2020-08-23 07:42:14 +02:00
use FireflyIII\Models\TransactionJournal;
2018-09-15 13:43:57 +02:00
use FireflyIII\Models\TransactionType;
use FireflyIII\TransactionRules\Actions\ConvertToDeposit;
use Log;
use Tests\TestCase;
/**
*
* Class ConvertToDepositTest
*/
class ConvertToDepositTest 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
}
/**
2019-04-09 15:42:25 +02:00
* Convert a transfer to a deposit.
2018-09-15 13:43:57 +02:00
*
* @covers \FireflyIII\TransactionRules\Actions\ConvertToDeposit
*/
2019-08-01 06:21:44 +02:00
public function testActTransfer(): void
2018-09-15 13:43:57 +02:00
{
2020-08-23 07:42:14 +02:00
/** @var TransactionJournal $transfer */
$transfer = $this->user()->transactionJournals()->where('description', 'Transfer for convertToDeposit.')->first();
$name = sprintf('Random revenue #%d', $this->randomInt());
2018-09-15 13:43:57 +02:00
2019-04-09 15:42:25 +02:00
// journal is a transfer:
2020-08-23 07:42:14 +02:00
$this->assertEquals(TransactionType::TRANSFER, $transfer->transactionType->type);
2018-09-15 13:43:57 +02:00
2020-08-23 07:42:14 +02:00
// make array for action:
$array = [
'transaction_journal_id' => $transfer->id,
'transaction_type_type' => $transfer->transactionType->type,
'user_id' => $this->user()->id,
'source_account_name' => 'Checking Account',
];
2018-09-15 13:43:57 +02:00
// fire the action:
$ruleAction = new RuleAction;
$ruleAction->action_value = $name;
$action = new ConvertToDeposit($ruleAction);
try {
2020-08-23 07:42:14 +02:00
$result = $action->actOnArray($array);
2018-09-15 13:43:57 +02:00
} catch (Exception $e) {
2019-08-03 14:45:37 +02:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2018-09-15 13:43:57 +02:00
$this->assertTrue(false, $e->getMessage());
}
$this->assertTrue($result);
2020-08-23 07:42:14 +02:00
// get journal:
$transfer->refresh();
$this->assertEquals(TransactionType::DEPOSIT, $transfer->transactionType->type);
2018-09-15 13:43:57 +02:00
}
/**
2019-04-09 15:42:25 +02:00
* Convert a withdrawal to a deposit.
2018-09-15 13:43:57 +02:00
*
* @covers \FireflyIII\TransactionRules\Actions\ConvertToDeposit
*/
2019-08-01 06:21:44 +02:00
public function testActWithdrawal(): void
2019-06-13 18:07:49 +02:00
{
2020-08-23 07:42:14 +02:00
/** @var TransactionJournal $withdrawal */
$withdrawal = $this->user()->transactionJournals()->where('description', 'Withdrawal for convertToDeposit.')->first();
$name = sprintf('Random revenue #%d', $this->randomInt());
2018-09-15 13:43:57 +02:00
2019-04-09 15:42:25 +02:00
// journal is a withdrawal:
2020-08-23 07:42:14 +02:00
$this->assertEquals(TransactionType::WITHDRAWAL, $withdrawal->transactionType->type);
2018-09-15 13:43:57 +02:00
2020-08-23 07:42:14 +02:00
// quick DB search for original source:
$source = Account::where('name', 'Checking Account')->first();
2018-09-15 13:43:57 +02:00
2020-08-23 07:42:14 +02:00
// make array for action:
$array = [
'transaction_journal_id' => $withdrawal->id,
'transaction_type_type' => $withdrawal->transactionType->type,
'user_id' => $this->user()->id,
'destination_account_name' => 'SuperMarket',
'source_account_id' => $source->id,
];
2018-09-15 13:43:57 +02:00
// fire the action:
$ruleAction = new RuleAction;
$ruleAction->action_value = $name;
$action = new ConvertToDeposit($ruleAction);
try {
2020-08-23 07:42:14 +02:00
$result = $action->actOnArray($array);
2018-09-15 13:43:57 +02:00
} catch (Exception $e) {
2019-08-03 14:45:37 +02:00
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
2018-09-15 13:43:57 +02:00
$this->assertTrue(false, $e->getMessage());
}
$this->assertTrue($result);
2020-08-23 07:42:14 +02:00
// get journal:
$withdrawal->refresh();
$this->assertEquals(TransactionType::DEPOSIT, $withdrawal->transactionType->type);
2018-09-15 13:43:57 +02:00
}
2018-12-31 07:48:23 +01:00
}