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

132 lines
4.2 KiB
PHP
Raw Normal View History

2018-09-15 13:43:57 +02:00
<?php
/**
* ConvertToDepositTest.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
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;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionType;
use FireflyIII\TransactionRules\Actions\ConvertToDeposit;
use Log;
use Tests\TestCase;
/**
*
* Class ConvertToDepositTest
2019-08-17 10:48:28 +02:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-09-15 13:43:57 +02:00
*/
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
{
$revenue = $this->getRandomRevenue();
2019-07-31 16:53:09 +02:00
$name = 'Random revenue #' . $this->randomInt();
2019-04-09 15:42:25 +02:00
$journal = $this->getRandomTransfer();
2018-09-15 13:43:57 +02:00
2019-04-09 15:42:25 +02:00
// journal is a transfer:
$this->assertEquals(TransactionType::TRANSFER, $journal->transactionType->type);
2018-09-15 13:43:57 +02:00
// mock used stuff:
$factory = $this->mock(AccountFactory::class);
$factory->shouldReceive('setUser')->once();
$factory->shouldReceive('findOrCreate')->once()->withArgs([$name, AccountType::REVENUE])->andReturn($revenue);
// fire the action:
$ruleAction = new RuleAction;
$ruleAction->action_value = $name;
$action = new ConvertToDeposit($ruleAction);
try {
$result = $action->act($journal);
} 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);
// journal is now a deposit.
$journal->refresh();
$this->assertEquals(TransactionType::DEPOSIT, $journal->transactionType->type);
}
/**
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
{
2018-09-15 13:43:57 +02:00
$revenue = $this->getRandomRevenue();
2019-07-31 16:53:09 +02:00
$name = 'Random revenue #' . $this->randomInt();
2019-04-09 15:42:25 +02:00
$journal = $this->getRandomWithdrawal();
2018-09-15 13:43:57 +02:00
2019-04-09 15:42:25 +02:00
// journal is a withdrawal:
$this->assertEquals(TransactionType::WITHDRAWAL, $journal->transactionType->type);
2018-09-15 13:43:57 +02:00
// mock used stuff:
$factory = $this->mock(AccountFactory::class);
$factory->shouldReceive('setUser')->once();
$factory->shouldReceive('findOrCreate')->once()->withArgs([$name, AccountType::REVENUE])->andReturn($revenue);
// fire the action:
$ruleAction = new RuleAction;
$ruleAction->action_value = $name;
$action = new ConvertToDeposit($ruleAction);
try {
$result = $action->act($journal);
} 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);
// journal is now a deposit.
$journal->refresh();
$this->assertEquals(TransactionType::DEPOSIT, $journal->transactionType->type);
}
2018-12-31 07:48:23 +01:00
}