diff --git a/app/Services/Internal/Support/AccountServiceTrait.php b/app/Services/Internal/Support/AccountServiceTrait.php index 05f07ca2e6..c49ebbcc68 100644 --- a/app/Services/Internal/Support/AccountServiceTrait.php +++ b/app/Services/Internal/Support/AccountServiceTrait.php @@ -255,8 +255,7 @@ trait AccountServiceTrait return true; } - - return true; + return true; // @codeCoverageIgnore } /** diff --git a/app/Services/Internal/Support/BillServiceTrait.php b/app/Services/Internal/Support/BillServiceTrait.php index 4ae1c624c9..e3861fb4b3 100644 --- a/app/Services/Internal/Support/BillServiceTrait.php +++ b/app/Services/Internal/Support/BillServiceTrait.php @@ -45,7 +45,7 @@ trait BillServiceTrait if (0 === strlen($note)) { $dbNote = $bill->notes()->first(); if (null !== $dbNote) { - $dbNote->delete(); + $dbNote->delete(); // @codeCoverageIgnore } return true; diff --git a/app/Services/Internal/Update/TransactionUpdateService.php b/app/Services/Internal/Update/TransactionUpdateService.php index 464ebad2c6..fa5ac3da05 100644 --- a/app/Services/Internal/Update/TransactionUpdateService.php +++ b/app/Services/Internal/Update/TransactionUpdateService.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace FireflyIII\Services\Internal\Update; -use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Transaction; use FireflyIII\Services\Internal\Support\TransactionServiceTrait; use FireflyIII\User; @@ -70,7 +69,6 @@ class TransactionUpdateService * @param array $data * * @return Transaction - * @throws FireflyException */ public function update(Transaction $transaction, array $data): Transaction { @@ -159,6 +157,6 @@ class TransactionUpdateService $category = $this->findCategory(0, $category); $this->setCategory($transaction, $category); - return $category; + return $transaction; } } \ No newline at end of file diff --git a/app/TransactionRules/Actions/SetDestinationAccount.php b/app/TransactionRules/Actions/SetDestinationAccount.php index 59d7577d9f..2ba82a4c7e 100644 --- a/app/TransactionRules/Actions/SetDestinationAccount.php +++ b/app/TransactionRules/Actions/SetDestinationAccount.php @@ -135,7 +135,6 @@ class SetDestinationAccount implements ActionInterface { $account = $this->repository->findByName($this->action->action_value, [AccountType::EXPENSE]); if (null === $account) { - // create new revenue account with this name: $data = [ 'name' => $this->action->action_value, 'accountType' => 'expense', diff --git a/tests/Unit/Factory/BillFactoryTest.php b/tests/Unit/Factory/BillFactoryTest.php index df04b26a2a..282a655d15 100644 --- a/tests/Unit/Factory/BillFactoryTest.php +++ b/tests/Unit/Factory/BillFactoryTest.php @@ -68,6 +68,40 @@ class BillFactoryTest extends TestCase } + /** + * Create basic bill with minimum data. + * + * @covers \FireflyIII\Factory\BillFactory + * @covers \FireflyIII\Services\Internal\Support\BillServiceTrait + */ + public function testCreateEmptyNotes() + { + $data = [ + 'name' => 'Some new bill #' . rand(1, 1000), + 'match' => 'i,am,word' . rand(1, 1000), + 'amount_min' => '5', + 'amount_max' => '10', + 'date' => '2018-01-01', + 'repeat_freq' => 'monthly', + 'skip' => 0, + 'automatch' => true, + 'active' => true, + 'notes' => '', + ]; + + /** @var BillFactory $factory */ + $factory = app(BillFactory::class); + $factory->setUser($this->user()); + $bill = $factory->create($data); + + $this->assertEquals($data['name'], $bill->name); + $this->assertEquals($data['match'], $bill->match); + $this->assertEquals($data['amount_min'], $bill->amount_min); + $this->assertEquals($data['repeat_freq'], $bill->repeat_freq); + $this->assertEquals(0, $bill->notes()->count()); + + } + /** * Find by ID * diff --git a/tests/Unit/Services/Internal/Update/AccountUpdateServiceTest.php b/tests/Unit/Services/Internal/Update/AccountUpdateServiceTest.php index fb184885d8..2569ddda29 100644 --- a/tests/Unit/Services/Internal/Update/AccountUpdateServiceTest.php +++ b/tests/Unit/Services/Internal/Update/AccountUpdateServiceTest.php @@ -29,6 +29,7 @@ use FireflyIII\Models\Account; use FireflyIII\Models\Note; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; +use FireflyIII\Services\Internal\Destroy\JournalDestroyService; use FireflyIII\Services\Internal\Update\AccountUpdateService; use Tests\TestCase; @@ -109,6 +110,62 @@ class AccountUpdateServiceTest extends TestCase $this->assertEquals($data['name'], $account->name); } + /** + * @covers \FireflyIII\Services\Internal\Update\AccountUpdateService + * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait + */ + public function testUpdateBasicEmptyNote() + { + /** @var Account $account */ + $account = $this->user()->accounts()->first(); + $data = [ + 'name' => 'Some new name #' . rand(1, 1000), + 'active' => true, + 'virtualBalance' => '0', + 'iban' => null, + 'accountRole' => 'defaultAsset', + 'notes' => '', + ]; + + /** @var AccountUpdateService $service */ + $service = app(AccountUpdateService::class); + $account = $service->update($account, $data); + + $this->assertEquals($data['name'], $account->name); + $this->assertEquals(0, $account->notes()->count()); + } + + /** + * @covers \FireflyIII\Services\Internal\Update\AccountUpdateService + * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait + */ + public function testUpdateBasicExistingNote() + { + /** @var Account $account */ + $account = $this->user()->accounts()->first(); + $note = new Note; + $note->noteable()->associate($account); + $note->text = 'Hi there'; + $note->save(); + + $data = [ + 'name' => 'Some new name #' . rand(1, 1000), + 'active' => true, + 'virtualBalance' => '0', + 'iban' => null, + 'accountRole' => 'defaultAsset', + 'notes' => '', + ]; + + /** @var AccountUpdateService $service */ + $service = app(AccountUpdateService::class); + $account = $service->update($account, $data); + + $this->assertEquals($data['name'], $account->name); + $this->assertEquals(0, $account->notes()->count()); + } + + /** * @covers \FireflyIII\Services\Internal\Update\AccountUpdateService * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait @@ -161,6 +218,61 @@ class AccountUpdateServiceTest extends TestCase $this->assertEquals($data['notes'], $note->text); } + /** + * @covers \FireflyIII\Services\Internal\Update\AccountUpdateService + * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait + */ + public function testUpdateExistingIBZero() + { + $deleteService = $this->mock(JournalDestroyService::class); + $deleteService->shouldReceive('destroy')->once(); + + /** @var Account $account */ + $account = Account::create( + ['user_id' => $this->user()->id, 'account_type_id' => 1, 'name' => 'Some name #' . rand(1, 1000), + 'virtual_balance' => '0', 'iban' => null, 'active' => true] + ); + $opposing = $this->user()->accounts()->first(); + $journal = TransactionJournal::create( + ['user_id' => $this->user()->id, 'transaction_type_id' => 4, 'transaction_currency_id' => 1, 'description' => 'IB', + 'date' => '2018-01-01', 'completed' => true, + ] + ); + // transactions: + Transaction::create( + ['account_id' => $account->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => '100', 'identifier' => 0,] + ); + Transaction::create( + ['account_id' => $opposing->id, 'transaction_journal_id' => $journal->id, + 'transaction_currency_id' => 1, 'amount' => '-100', 'identifier' => 0,] + ); + + + $data = [ + 'name' => 'Some new name #' . rand(1, 1000), + 'active' => true, + 'virtualBalance' => '0', + 'iban' => null, + 'accountRole' => 'defaultAsset', + 'openingBalance' => '0', + 'openingBalanceDate' => new Carbon('2018-01-01'), + 'notes' => 'Hello', + 'currency_id' => 1, + ]; + + /** @var AccountUpdateService $service */ + $service = app(AccountUpdateService::class); + $account = $service->update($account, $data); + + $this->assertEquals($data['name'], $account->name); + $this->assertEquals(1, $account->transactions()->count()); + $this->assertEquals(100, $account->transactions()->first()->amount); + /** @var Note $note */ + $note = $account->notes()->first(); + $this->assertEquals($data['notes'], $note->text); + } + /** * @covers \FireflyIII\Services\Internal\Update\AccountUpdateService * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait diff --git a/tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php b/tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php index 82122c66a9..59277bec06 100644 --- a/tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php +++ b/tests/Unit/Services/Internal/Update/JournalUpdateServiceTest.php @@ -32,6 +32,7 @@ use FireflyIII\Factory\TransactionJournalMetaFactory; use FireflyIII\Models\TransactionJournal; use FireflyIII\Services\Internal\Update\JournalUpdateService; use FireflyIII\Services\Internal\Update\TransactionUpdateService; +use Mockery; use Tests\TestCase; /** @@ -81,6 +82,95 @@ class JournalUpdateServiceTest extends TestCase $this->assertEquals(0, $result->transactions()->count()); } + /** + * @covers \FireflyIII\Services\Internal\Update\JournalUpdateService + * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait + */ + public function testUpdateBasicEmptyNote() + { + // mock other stuff: + $transactionFactory = $this->mock(TransactionFactory::class); + $transactionService = $this->mock(TransactionUpdateService::class); + $billFactory = $this->mock(BillFactory::class); + $tagFactory = $this->mock(TagFactory::class); + $metaFactory = $this->mock(TransactionJournalMetaFactory::class); + + // mock calls + $billFactory->shouldReceive('setUser'); + $billFactory->shouldReceive('find')->andReturn(null); + $transactionService->shouldReceive('setUser'); + $transactionFactory->shouldReceive('setUser'); + $tagFactory->shouldReceive('setUser'); + $metaFactory->shouldReceive('setUser'); + + + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 2)->first(); + $data = [ + 'description' => 'Updated journal #' . rand(1, 1000), + 'date' => new Carbon('2018-01-01'), + 'bill_id' => null, + 'bill_name' => null, + 'tags' => [], + 'notes' => '', + 'transactions' => [], + ]; + + /** @var JournalUpdateService $service */ + $service = app(JournalUpdateService::class); + $result = $service->update($journal, $data); + + $this->assertEquals($data['description'], $result->description); + $this->assertEquals(0, $result->transactions()->count()); + $this->assertEquals(0, $result->notes()->count()); + } + + /** + * @covers \FireflyIII\Services\Internal\Update\JournalUpdateService + */ + public function testUpdateBudget() + { + $budget = $this->user()->budgets()->first(); + $service = $this->mock(TransactionUpdateService::class); + $service->shouldReceive('setUser'); + $service->shouldReceive('updateBudget')->withArgs([Mockery::any(), $budget->id])->twice(); + + + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); + + // call update service to update budget. Should call transaction service twice. + /** @var JournalUpdateService $service */ + $service = app(JournalUpdateService::class); + $service->updateBudget($journal, $budget->id); + } + + /** + * @covers \FireflyIII\Services\Internal\Update\JournalUpdateService + */ + public function testUpdateCategory() + { + $service = $this->mock(TransactionUpdateService::class); + $service->shouldReceive('setUser'); + $service->shouldReceive('updateCategory')->withArgs([Mockery::any(), 'New category'])->twice(); + + + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->inRandomOrder()->where('transaction_type_id', 1)->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); + + // call update service to update budget. Should call transaction service twice. + /** @var JournalUpdateService $service */ + $service = app(JournalUpdateService::class); + $service->updateCategory($journal, 'New category'); + } + + /** * @covers \FireflyIII\Services\Internal\Update\JournalUpdateService * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait diff --git a/tests/Unit/Services/Internal/Update/TransactionUpdateServiceTest.php b/tests/Unit/Services/Internal/Update/TransactionUpdateServiceTest.php new file mode 100644 index 0000000000..fe97806c57 --- /dev/null +++ b/tests/Unit/Services/Internal/Update/TransactionUpdateServiceTest.php @@ -0,0 +1,222 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Services\Internal\Update; + + +use FireflyIII\Factory\BudgetFactory; +use FireflyIII\Factory\CategoryFactory; +use FireflyIII\Models\Transaction; +use FireflyIII\Services\Internal\Update\TransactionUpdateService; +use Tests\TestCase; + +/** + * Class TransactionUpdateServiceTest + */ +class TransactionUpdateServiceTest extends TestCase +{ + /** + * @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService + */ + public function testReconcile() + { + $transaction = $this->user()->transactions()->inRandomOrder()->first(); + + /** @var TransactionUpdateService $service */ + $service = app(TransactionUpdateService::class); + $service->setUser($this->user()); + $result = $service->reconcile($transaction->id); + $this->assertEquals($result->id, $transaction->id); + $this->assertEquals(true, $result->reconciled); + } + + /** + * @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService + * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait + */ + public function testReconcileNull() + { + /** @var TransactionUpdateService $service */ + $service = app(TransactionUpdateService::class); + $service->setUser($this->user()); + $result = $service->reconcile(-1); + $this->assertNull($result); + } + + /** + * @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService + * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait + */ + public function testUpdateBudget() + { + + /** @var Transaction $source */ + $source = $this->user()->transactions()->where('amount', '>', 0)->inRandomOrder()->first(); + $budget = $this->user()->budgets()->inRandomOrder()->first(); + + $factory = $this->mock(BudgetFactory::class); + $factory->shouldReceive('setUser'); + $factory->shouldReceive('find')->andReturn($budget); + + /** @var TransactionUpdateService $service */ + $service = app(TransactionUpdateService::class); + $service->setUser($this->user()); + $result = $service->updateBudget($source, $budget->id); + + $this->assertEquals(1, $result->budgets()->count()); + $this->assertEquals($budget->name, $result->budgets()->first()->name); + } + + /** + * @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService + * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait + */ + public function testUpdateCategory() + { + + /** @var Transaction $source */ + $source = $this->user()->transactions()->where('amount', '>', 0)->inRandomOrder()->first(); + $category = $this->user()->categories()->inRandomOrder()->first(); + + $factory = $this->mock(CategoryFactory::class); + $factory->shouldReceive('setUser'); + $factory->shouldReceive('findOrCreate')->andReturn($category); + + /** @var TransactionUpdateService $service */ + $service = app(TransactionUpdateService::class); + $service->setUser($this->user()); + $result = $service->updateCategory($source, $category->name); + + $this->assertEquals(1, $result->categories()->count()); + $this->assertEquals($category->name, $result->categories()->first()->name); + } + + /** + * @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService + * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait + */ + public function testUpdateDestinationBasic() + { + /** @var Transaction $source */ + $source = $this->user()->transactions()->where('amount', '>', 0)->inRandomOrder()->first(); + + $data = [ + 'currency_id' => 1, + 'currency_code' => null, + 'description' => 'Some new description', + 'reconciled' => false, + 'foreign_amount' => null, + 'budget_id' => null, + 'budget_name' => null, + 'destination_id' => intval($source->account_id), + 'destination_name' => null, + 'category_id' => null, + 'category_name' => null, + 'amount' => $source->amount, + 'foreign_currency_id' => null, + 'foreign_currency_code' => null, + ]; + + /** @var TransactionUpdateService $service */ + $service = app(TransactionUpdateService::class); + $service->setUser($this->user()); + $result = $service->update($source, $data); + + $this->assertEquals($source->id, $result->id); + $this->assertEquals($result->description, $data['description']); + } + + /** + * @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService + * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait + */ + public function testUpdateDestinationForeign() + { + /** @var Transaction $source */ + $source = $this->user()->transactions()->where('amount', '>', 0)->inRandomOrder()->first(); + + $data = [ + 'currency_id' => 1, + 'currency_code' => null, + 'description' => 'Some new description', + 'reconciled' => false, + 'foreign_amount' => '12.34', + 'budget_id' => null, + 'budget_name' => null, + 'destination_id' => intval($source->account_id), + 'destination_name' => null, + 'category_id' => null, + 'category_name' => null, + 'amount' => $source->amount, + 'foreign_currency_id' => 2, + 'foreign_currency_code' => null, + ]; + + /** @var TransactionUpdateService $service */ + $service = app(TransactionUpdateService::class); + $service->setUser($this->user()); + $result = $service->update($source, $data); + + $this->assertEquals($source->id, $result->id); + $this->assertEquals($result->description, $data['description']); + $this->assertEquals($data['foreign_amount'], $result->foreign_amount); + $this->assertEquals($data['foreign_currency_id'], $result->foreign_currency_id); + } + + /** + * @covers \FireflyIII\Services\Internal\Update\TransactionUpdateService + * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait + */ + public function testUpdateSourceBasic() + { + /** @var Transaction $source */ + $source = $this->user()->transactions()->where('amount', '<', 0)->inRandomOrder()->first(); + + $data = [ + 'currency_id' => 1, + 'currency_code' => null, + 'description' => 'Some new description', + 'reconciled' => false, + 'foreign_amount' => null, + 'budget_id' => null, + 'budget_name' => null, + 'source_id' => intval($source->account_id), + 'source_name' => null, + 'category_id' => null, + 'category_name' => null, + 'amount' => $source->amount, + 'foreign_currency_id' => null, + 'foreign_currency_code' => null, + ]; + + /** @var TransactionUpdateService $service */ + $service = app(TransactionUpdateService::class); + $service->setUser($this->user()); + $result = $service->update($source, $data); + + $this->assertEquals($source->id, $result->id); + $this->assertEquals($result->description, $data['description']); + + + } +} \ No newline at end of file diff --git a/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php index 7662bbb64b..fc25d63627 100644 --- a/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php +++ b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php @@ -42,23 +42,18 @@ class SetDestinationAccountTest extends TestCase /** * Give deposit existing asset account. * - * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct() - * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act() - * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findAssetAccount() + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount */ public function testActDepositExisting() { $accountRepos = $this->mock(AccountRepositoryInterface::class); $type = TransactionType::whereType(TransactionType::DEPOSIT)->first(); - // select split transactions to exclude them later: - $set = TransactionJournal::where('transaction_type_id', $type->id)->get(['transaction_journals.*']); - foreach ($set as $current) { - if ($current->transactions()->count() === 2) { - $journal = $current; - break; - } - } + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); $destination = $destinationTr->account; @@ -86,26 +81,81 @@ class SetDestinationAccountTest extends TestCase $this->assertEquals($newDestination->id, $account->id); } + /** + * Give deposit not existing asset account. + * + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount + */ + public function testActDepositNotExisting() + { + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $type = TransactionType::whereType(TransactionType::DEPOSIT)->first(); + + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); + + // find account? Return account: + $accountRepos->shouldReceive('setUser'); + $accountRepos->shouldReceive('findByName')->andReturn(null); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Not existing asset account #' . rand(1, 1000); + $action = new SetDestinationAccount($ruleAction); + $result = $action->act($journal); + $this->assertFalse($result); + } + + /** + * Give withdrawal not existing expense account. + * + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount + */ + public function testActWithDrawalNotExisting() + { + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); + $account = $this->user()->accounts()->inRandomOrder()->where('account_type_id', 4)->first(); + + + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); + + // find account? Return account: + $accountRepos->shouldReceive('setUser'); + $accountRepos->shouldReceive('findByName')->andReturn(null); + $accountRepos->shouldReceive('store')->once()->andReturn($account); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Not existing expense account #' . rand(1, 1000); + $action = new SetDestinationAccount($ruleAction); + $result = $action->act($journal); + + $this->assertTrue($result); + } + /** * Give withdrawal existing expense account. * - * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct() - * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act() - * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findExpenseAccount + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount */ public function testActWithdrawalExisting() { $accountRepos = $this->mock(AccountRepositoryInterface::class); $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); - // select split transactions to exclude them later: - $set = TransactionJournal::where('transaction_type_id', $type->id)->get(['transaction_journals.*']); - foreach ($set as $current) { - if ($current->transactions()->count() === 2) { - $journal = $current; - break; - } - } + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); $destinationTr = $journal->transactions()->where('amount', '>', 0)->first(); @@ -137,8 +187,7 @@ class SetDestinationAccountTest extends TestCase /** * Test this on a split journal. * - * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct() - * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act() + * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount */ public function testSplitJournal() { diff --git a/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php index ecb6099d79..9042e0aa56 100644 --- a/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php +++ b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php @@ -40,9 +40,7 @@ class SetSourceAccountTest extends TestCase /** * Give deposit existing revenue account. * - * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct() - * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act() - * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findRevenueAccount() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount */ public function testActDepositExistingUpdated() { @@ -51,14 +49,11 @@ class SetSourceAccountTest extends TestCase $type = TransactionType::whereType(TransactionType::DEPOSIT)->first(); - // select split transactions to exclude them later: - $set = TransactionJournal::where('transaction_type_id', $type->id)->get(['transaction_journals.*']); - foreach ($set as $current) { - if ($current->transactions()->count() === 2) { - $journal = $current; - break; - } - } + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); $source = $sourceTr->account; @@ -86,26 +81,50 @@ class SetSourceAccountTest extends TestCase $this->assertEquals($newSource->id, $account->id); } + /** + * Give deposit new revenueaccount. + * + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount + */ + public function testActDepositRevenue() + { + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $type = TransactionType::whereType(TransactionType::DEPOSIT)->first(); + $account = $this->user()->accounts()->inRandomOrder()->where('account_type_id', 5)->first(); + + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); + + $accountRepos->shouldReceive('setUser'); + $accountRepos->shouldReceive('findByName')->andReturn(null); + $accountRepos->shouldReceive('store')->once()->andReturn($account); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Some new revenue #' . rand(1, 1000); + $action = new SetSourceAccount($ruleAction); + $result = $action->act($journal); + $this->assertTrue($result); + } + /** * Give withdrawal existing asset account. * - * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct() - * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act() - * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findAssetAccount() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount */ public function testActWithdrawalExistingUpdated() { $accountRepos = $this->mock(AccountRepositoryInterface::class); $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); - // select split transactions to exclude them later: - $set = TransactionJournal::where('transaction_type_id', $type->id)->get(['transaction_journals.*']); - foreach ($set as $current) { - if ($current->transactions()->count() === 2) { - $journal = $current; - break; - } - } + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); $sourceTr = $journal->transactions()->where('amount', '<', 0)->first(); $source = $sourceTr->account; @@ -133,11 +152,37 @@ class SetSourceAccountTest extends TestCase $this->assertEquals($newSource->id, $account->id); } + /** + * Give withdrawal not existing asset account. + * + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount + */ + public function testActWithdrawalNotExisting() + { + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first(); + + do { + /** @var TransactionJournal $journal */ + $journal = $this->user()->transactionJournals()->where('transaction_type_id', $type->id)->inRandomOrder()->first(); + $count = $journal->transactions()->count(); + } while ($count !== 2); + + $accountRepos->shouldReceive('setUser'); + $accountRepos->shouldReceive('findByName')->andReturn(null); + + // fire the action: + $ruleAction = new RuleAction; + $ruleAction->action_value = 'Some new account #' . rand(1, 1000); + $action = new SetSourceAccount($ruleAction); + $result = $action->act($journal); + $this->assertFalse($result); + } + /** * Test this on a split journal. * - * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct() - * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act() + * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount */ public function testSplitJournal() {