Fix actions and associated tests.

This commit is contained in:
James Cole
2020-08-23 16:12:16 +02:00
parent 139b3ffab4
commit fecc9f7659
8 changed files with 286 additions and 318 deletions

View File

@@ -22,12 +22,9 @@ 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\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Models\TransactionType;
use FireflyIII\TransactionRules\Actions\SetSourceAccount;
use Tests\TestCase;
@@ -36,16 +33,6 @@ use Tests\TestCase;
*/
class SetSourceAccountTest extends TestCase
{
/**
* Set up test
*/
public function setUp(): void
{
self::markTestIncomplete('Incomplete for refactor.');
return;
}
/**
* Give deposit existing revenue account.
*
@@ -53,24 +40,25 @@ class SetSourceAccountTest extends TestCase
*/
public function testActDepositExistingUpdated(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$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();
$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();
$this->assertNotEquals($source->id, $account->id);
// find account? Return account:
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('findByName')->andReturn($account);
$array = [
'user_id' => $this->user()->id,
'transaction_journal_id' => $deposit->id,
'transaction_type_type' => TransactionType::DEPOSIT,
];
// fire the action:
$ruleAction = new RuleAction;
$ruleAction->action_value = $account->name;
$action = new SetSourceAccount($ruleAction);
$result = $action->act($deposit);
$result = $action->actOnArray($array);
$this->assertTrue($result);
// test journal for new account
@@ -78,6 +66,9 @@ class SetSourceAccountTest extends TestCase
$newSource = $sourceTr->account;
$this->assertNotEquals($source->id, $newSource->id);
$this->assertEquals($newSource->id, $account->id);
$sourceTr->account_id = $source->id;
$sourceTr->save();
}
/**
@@ -87,19 +78,19 @@ class SetSourceAccountTest extends TestCase
*/
public function testActDepositRevenue(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$account = $this->getRandomRevenue();
$deposit = $this->getRandomDeposit();
$deposit = $this->getRandomDeposit();
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('findByName')->andReturn(null);
$accountRepos->shouldReceive('store')->once()->andReturn($account);
$array = [
'transaction_journal_id' => $deposit->id,
'user_id' => $this->user()->id,
'transaction_type_type' => TransactionType::DEPOSIT,
];
// fire the action:
$ruleAction = new RuleAction;
$ruleAction->action_value = 'Some new revenue #' . $this->randomInt();
$ruleAction->action_value = sprintf('Some new revenue #%d', $this->randomInt());
$action = new SetSourceAccount($ruleAction);
$result = $action->act($deposit);
$result = $action->actOnArray($array);
$this->assertTrue($result);
}
@@ -110,9 +101,7 @@ class SetSourceAccountTest extends TestCase
*/
public function testActWithdrawalExistingUpdated(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$withdrawal = $this->getRandomWithdrawal();
$withdrawal = $this->getRandomWithdrawal();
$sourceTr = $withdrawal->transactions()->where('amount', '<', 0)->first();
$source = $sourceTr->account;
$user = $withdrawal->user;
@@ -120,15 +109,17 @@ class SetSourceAccountTest extends TestCase
$account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();
$this->assertNotEquals($source->id, $account->id);
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('findByName')->andReturn($account);
$array = [
'user_id' => $this->user()->id,
'transaction_type_type' => TransactionType::WITHDRAWAL,
'transaction_journal_id' => $withdrawal->id,
];
// fire the action:
$ruleAction = new RuleAction;
$ruleAction->action_value = $account->name;
$action = new SetSourceAccount($ruleAction);
$result = $action->act($withdrawal);
$result = $action->actOnArray($array);
$this->assertTrue($result);
// test journal for new account
@@ -136,6 +127,9 @@ class SetSourceAccountTest extends TestCase
$newSource = $sourceTr->account;
$this->assertNotEquals($source->id, $newSource->id);
$this->assertEquals($newSource->id, $account->id);
$sourceTr->account_id = $source->id;
$sourceTr->save();
}
/**
@@ -145,17 +139,19 @@ class SetSourceAccountTest extends TestCase
*/
public function testActWithdrawalNotExisting(): void
{
$accountRepos = $this->mock(AccountRepositoryInterface::class);
$withdrawal = $this->getRandomWithdrawal();
$withdrawal = $this->getRandomWithdrawal();
$accountRepos->shouldReceive('setUser');
$accountRepos->shouldReceive('findByName')->andReturn(null);
$array = [
'user_id' => $this->user()->id,
'transaction_type_type' => TransactionType::WITHDRAWAL,
'transaction_journal_id' => $withdrawal->id,
];
// fire the action:
$ruleAction = new RuleAction;
$ruleAction->action_value = 'Some new account #' . $this->randomInt();
$ruleAction->action_value = sprintf('Some new account #%d', $this->randomInt());
$action = new SetSourceAccount($ruleAction);
$result = $action->act($withdrawal);
$result = $action->actOnArray($array);
$this->assertFalse($result);
}
}