Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
New unit tests.
James Colec17a278e574c
diff --git a/app/TransactionRules/Actions/ClearNotes.php b/app/TransactionRules/Actions/ClearNotes.phpindex e4825d78ba..38cc19998a 100644--- a/app/TransactionRules/Actions/ClearNotes.php+++ b/app/TransactionRules/Actions/ClearNotes.php@@ -12,6 +12,7 @@ declare(strict_types=1);namespace FireflyIII\TransactionRules\Actions;+use FireflyIII\Models\Note;use FireflyIII\Models\RuleAction;use FireflyIII\Models\TransactionJournal;use Log;@@ -45,8 +46,11 @@ class ClearNotes implements ActionInterfacepublic function act(TransactionJournal $journal): bool{Log::debug(sprintf('RuleAction ClearNotes removed all notes.'));- $journal->deleteMeta('notes');- $journal->save();+ $notes = $journal->notes()->get();+ /** @var Note $note */+ foreach ($notes as $note) {+ $note->delete();+ }return true;}diff --git a/app/TransactionRules/Actions/SetDescription.php b/app/TransactionRules/Actions/SetDescription.phpindex 17c5eb1a13..25488f6b36 100644--- a/app/TransactionRules/Actions/SetDescription.php+++ b/app/TransactionRules/Actions/SetDescription.php@@ -45,8 +45,7 @@ class SetDescription implements ActionInterface*/public function act(TransactionJournal $journal): bool{- $oldDescription = $journal->description;-+ $oldDescription = $journal->description;$journal->description = $this->action->action_value;$journal->save();diff --git a/app/TransactionRules/Actions/SetDestinationAccount.php b/app/TransactionRules/Actions/SetDestinationAccount.phpindex 9646006810..292407aa6e 100644--- a/app/TransactionRules/Actions/SetDestinationAccount.php+++ b/app/TransactionRules/Actions/SetDestinationAccount.php@@ -66,7 +66,7 @@ class SetDestinationAccount implements ActionInterfaceif ($count > 2) {Log::error(sprintf('Cannot change destination account of journal #%d because it is a split journal.', $journal->id));- return true;+ return false;}// journal type:@@ -81,7 +81,7 @@ class SetDestinationAccount implements ActionInterface));- return true;+ return false;}// if this is a withdrawal, the new destination account must be a expense account and may be created:diff --git a/app/TransactionRules/Actions/SetSourceAccount.php b/app/TransactionRules/Actions/SetSourceAccount.phpindex 4b27c044d2..08fa29e315 100644--- a/app/TransactionRules/Actions/SetSourceAccount.php+++ b/app/TransactionRules/Actions/SetSourceAccount.php@@ -66,7 +66,7 @@ class SetSourceAccount implements ActionInterfaceif ($count > 2) {Log::error(sprintf('Cannot change source account of journal #%d because it is a split journal.', $journal->id));- return true;+ return false;}// journal type:@@ -80,7 +80,7 @@ class SetSourceAccount implements ActionInterface));- return true;+ return false;}// if this is a deposit, the new source account must be a revenue account and may be created:diff --git a/tests/Unit/TransactionRules/Actions/AppendNotesTest.php b/tests/Unit/TransactionRules/Actions/AppendNotesTest.phpnew file mode 100644index 0000000000..093e9f32d8--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/AppendNotesTest.php@@ -0,0 +1,84 @@+<?php+/**+ * AppendNotesTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+++use FireflyIII\Models\Note;+use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\AppendNotes;+use Tests\TestCase;++/**+ * Class AppendNotesTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class AppendNotesTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\AppendNotes::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\AppendNotes::act()+ */+ public function testAct()+ {+ // give journal some notes.+ $journal = TransactionJournal::find(3);+ $note = $journal->notes()->first();+ $start = 'Default note text';+ $toAppend = 'This is appended';+ if (is_null($note)) {+ $note = new Note();+ $note->noteable()->associate($journal);+ }+ $note->text = $start;+ $note->save();++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $toAppend;+ $action = new AppendNotes($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ $newNote = $journal->notes()->first();+ $this->assertEquals($start . $toAppend, $newNote->text);++ }++ /**+ * @covers \FireflyIII\TransactionRules\Actions\AppendNotes::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\AppendNotes::act()+ */+ public function testActNewNote()+ {+ // give journal some notes.+ $journal = TransactionJournal::find(4);+ $note = $journal->notes()->first();+ if (!is_null($note)) {+ $note->forceDelete();+ }+ $toAppend = 'This is appended';++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $toAppend;+ $action = new AppendNotes($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ $newNote = $journal->notes()->first();+ $this->assertEquals($toAppend, $newNote->text);++ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php b/tests/Unit/TransactionRules/Actions/ClearBudgetTest.phpnew file mode 100644index 0000000000..1b00597916--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/ClearBudgetTest.php@@ -0,0 +1,51 @@+<?php+/**+ * ClearBudgetTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+++use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\ClearBudget;+use Tests\TestCase;++/**+ * Class ClearBudgetTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class ClearBudgetTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\ClearBudget::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\ClearBudget::act()+ */+ public function testAct()+ {+ // associate budget with journal:+ $journal = TransactionJournal::find(5);+ $budget = $journal->user->budgets()->first();+ $journal->budgets()->save($budget);+ $this->assertGreaterThan(0, $journal->budgets()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = null;+ $action = new ClearBudget($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // assert result+ $this->assertEquals(0, $journal->budgets()->count());++ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php b/tests/Unit/TransactionRules/Actions/ClearCategoryTest.phpnew file mode 100644index 0000000000..294e3b984f--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/ClearCategoryTest.php@@ -0,0 +1,49 @@+<?php+/**+ * ClearCategoryTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\ClearCategory;+use Tests\TestCase;++/**+ * Class ClearCategoryTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class ClearCategoryTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\ClearCategory::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\ClearCategory::act()+ */+ public function testAct()+ {+ // associate budget with journal:+ $journal = TransactionJournal::find(5);+ $category = $journal->user->categories()->first();+ $journal->budgets()->save($category);+ $this->assertGreaterThan(0, $journal->categories()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = null;+ $action = new ClearCategory($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // assert result+ $this->assertEquals(0, $journal->categories()->count());++ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/ClearNotesTest.php b/tests/Unit/TransactionRules/Actions/ClearNotesTest.phpnew file mode 100644index 0000000000..41c6d8bc22--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/ClearNotesTest.php@@ -0,0 +1,57 @@+<?php+/**+ * ClearNotesTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+++use FireflyIII\Models\Note;+use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\ClearNotes;+use Tests\TestCase;++/**+ * Class ClearNotesTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class ClearNotesTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\ClearNotes::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\ClearNotes::act()+ */+ public function testAct()+ {+ // give journal a note:+ $journal = TransactionJournal::find(6);+ $note = $journal->notes()->first();+ if (is_null($note)) {+ $note = new Note;+ $note->noteable()->associate($journal);+ }+ $note->text = 'Hello test note';+ $note->save();+ $this->assertEquals(1, $journal->notes()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = null;+ $action = new ClearNotes($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // assert result+ $this->assertEquals(0, $journal->notes()->count());++ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.php b/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.phpnew file mode 100644index 0000000000..d6063ef3fa--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/PrependDescriptionTest.php@@ -0,0 +1,53 @@+<?php+/**+ * PrependDescriptionTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+++use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\PrependDescription;+use Tests\TestCase;++/**+ * Class PrependDescriptionTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class PrependDescriptionTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\PrependDescription::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\PrependDescription::act()+ */+ public function testAct()+ {+ // get journal, give fixed description+ $description = 'text' . rand(1, 1000);+ $prepend = 'prepend' . rand(1, 1234);+ $journal = TransactionJournal::find(7);+ $journal->description = $description;+ $journal->save();++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $prepend;+ $action = new PrependDescription($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);+ $journal = TransactionJournal::find(7);++ // assert result+ $this->assertEquals($prepend.$description, $journal->description);++ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/PrependNotesTest.php b/tests/Unit/TransactionRules/Actions/PrependNotesTest.phpnew file mode 100644index 0000000000..0d42bb23ee--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/PrependNotesTest.php@@ -0,0 +1,84 @@+<?php+/**+ * PrependNotesTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+++use FireflyIII\Models\Note;+use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\PrependNotes;+use Tests\TestCase;++/**+ * Class PrependNotesTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class PrependNotesTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\PrependNotes::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\PrependNotes::act()+ */+ public function testAct()+ {+ // give journal some notes.+ $journal = TransactionJournal::find(8);+ $note = $journal->notes()->first();+ $start = 'Default note text';+ $toPrepend = 'This is prepended';+ if (is_null($note)) {+ $note = new Note();+ $note->noteable()->associate($journal);+ }+ $note->text = $start;+ $note->save();++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $toPrepend;+ $action = new PrependNotes($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ $newNote = $journal->notes()->first();+ $this->assertEquals($toPrepend . $start, $newNote->text);++ }++ /**+ * @covers \FireflyIII\TransactionRules\Actions\PrependNotes::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\PrependNotes::act()+ */+ public function testActNewNote()+ {+ // give journal some notes.+ $journal = TransactionJournal::find(4);+ $note = $journal->notes()->first();+ if (!is_null($note)) {+ $note->forceDelete();+ }+ $toPrepend = 'This is appended';++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $toPrepend;+ $action = new PrependNotes($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ $newNote = $journal->notes()->first();+ $this->assertEquals($toPrepend, $newNote->text);++ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/RemoveAllTagsTest.php b/tests/Unit/TransactionRules/Actions/RemoveAllTagsTest.phpnew file mode 100644index 0000000000..2e74697ba7--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/RemoveAllTagsTest.php@@ -0,0 +1,52 @@+<?php+/**+ * RemoveAllTagsTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+++use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\RemoveAllTags;+use Tests\TestCase;++/**+ * Class RemoveAllTagsTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class RemoveAllTagsTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\RemoveAllTags::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\RemoveAllTags::act()+ */+ public function testAct()+ {+ // get journal, link al tags:+ $journal = TransactionJournal::find(9);+ $tags = $journal->user->tags()->get();+ foreach ($tags as $tag) {+ $journal->tags()->save($tag);+ $journal->save();+ }+ $this->assertEquals($tags->count(), $journal->tags()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = null;+ $action = new RemoveAllTags($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ $this->assertEquals(0, $journal->tags()->count());+ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/RemoveTagTest.php b/tests/Unit/TransactionRules/Actions/RemoveTagTest.phpnew file mode 100644index 0000000000..78434a7d72--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/RemoveTagTest.php@@ -0,0 +1,79 @@+<?php+/**+ * RemoveTagTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+++use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\RemoveTag;+use Tests\TestCase;++/**+ * Class RemoveTagTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class RemoveTagTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\RemoveTag::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\RemoveTag::act()+ */+ public function testAct()+ {+ // get journal, link al tags:+ $journal = TransactionJournal::find(10);+ $tags = $journal->user->tags()->get();+ foreach ($tags as $tag) {+ $journal->tags()->save($tag);+ $journal->save();+ }+ $firstTag = $tags->first();+ $this->assertEquals($tags->count(), $journal->tags()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $firstTag->tag;+ $action = new RemoveTag($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);+ foreach ($journal->tags()->get() as $tag) {+ $this->assertNotEquals($firstTag->id, $tag->id);+ }+ $this->assertEquals(($tags->count() - 1), $journal->tags()->count());+ }++ /**+ * @covers \FireflyIII\TransactionRules\Actions\RemoveTag::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\RemoveTag::act()+ */+ public function testActNoTag()+ {+ // get journal, link al tags:+ $journal = TransactionJournal::find(11);+ $tags = $journal->user->tags()->get();+ foreach ($tags as $tag) {+ $journal->tags()->save($tag);+ $journal->save();+ }+ $this->assertEquals($tags->count(), $journal->tags()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = rand(1, 1234) . 'nosuchtag';+ $action = new RemoveTag($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);+ $this->assertEquals($tags->count(), $journal->tags()->count());+ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/SetBudgetTest.php b/tests/Unit/TransactionRules/Actions/SetBudgetTest.phpnew file mode 100644index 0000000000..d00b55cf99--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/SetBudgetTest.php@@ -0,0 +1,49 @@+<?php+/**+ * SetBudgetTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+++use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\SetBudget;+use Tests\TestCase;++/**+ * Class SetBudgetTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class SetBudgetTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\SetBudget::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetBudget::act()+ */+ public function testAct()+ {+ // get journal, remove all budgets+ $journal = TransactionJournal::find(12);+ $budget = $journal->user->budgets()->first();+ $journal->budgets()->detach();+ $this->assertEquals(0, $journal->budgets()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $budget->name;+ $action = new SetBudget($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);+ $this->assertEquals(1, $journal->budgets()->count());+ $this->assertEquals($budget->name, $journal->budgets()->first()->name);+ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/SetCategoryTest.php b/tests/Unit/TransactionRules/Actions/SetCategoryTest.phpnew file mode 100644index 0000000000..09fef3f2f4--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/SetCategoryTest.php@@ -0,0 +1,48 @@+<?php+/**+ * SetCategoryTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;++use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\SetBudget;+use Tests\TestCase;++/**+ * Class SetCategoryTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class SetCategoryTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\SetBudget::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetBudget::act()+ */+ public function testAct()+ {+ // get journal, remove all budgets+ $journal = TransactionJournal::find(13);+ $category = $journal->user->categories()->first();+ $journal->categories()->detach();+ $this->assertEquals(0, $journal->categories()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $category->name;+ $action = new SetBudget($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);+ $this->assertEquals(1, $journal->categories()->count());+ $this->assertEquals($category->name, $journal->categories()->first()->name);+ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/SetDescriptionTest.php b/tests/Unit/TransactionRules/Actions/SetDescriptionTest.phpnew file mode 100644index 0000000000..92c7037173--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/SetDescriptionTest.php@@ -0,0 +1,52 @@+<?php+/**+ * SetDescriptionTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;++use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\SetDescription;+use Tests\TestCase;++/**+ * Class SetDescriptionTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class SetDescriptionTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\SetDescription::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetDescription::act()+ */+ public function testAct()+ {+ // get journal, give fixed description+ $description = 'text' . rand(1, 1000);+ $newDescription = 'new description' . rand(1, 1234);+ $journal = TransactionJournal::find(14);+ $journal->description = $description;+ $journal->save();++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $newDescription;+ $action = new SetDescription($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);+ $journal = TransactionJournal::find(14);++ // assert result+ $this->assertEquals($newDescription, $journal->description);++ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.phpnew file mode 100644index 0000000000..91dc27d940--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/SetDestinationAccountTest.php@@ -0,0 +1,175 @@+<?php+/**+ * SetDestinationAccountTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++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\Models\TransactionType;+use FireflyIII\TransactionRules\Actions\SetDestinationAccount;+use Tests\TestCase;++/**+ * Try split journal+ *+ * Class SetDestinationAccountTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+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()+ */+ public function testActDepositExisting()+ {+ $type = TransactionType::whereType(TransactionType::DEPOSIT)->first();+ $journal = TransactionJournal::where('transaction_type_id', $type->id)->first();+ $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();+ $destination = $destinationTr->account;+ $user = $journal->user;+ $accountType = AccountType::whereType(AccountType::ASSET)->first();+ $account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $destination->id)->first();+ $this->assertNotEquals($destination->id, $account->id);++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $account->name;+ $action = new SetDestinationAccount($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // test journal for new account+ $journal = TransactionJournal::find($journal->id);+ $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();+ $newDestination = $destinationTr->account;+ $this->assertNotEquals($destination->id, $newDestination->id);+ $this->assertEquals($newDestination->id, $account->id);+ }++ /**+ * Give deposit new asset account (will fail)+ *+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act()+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findAssetAccount()+ */+ public function testActDepositNew()+ {+ $type = TransactionType::whereType(TransactionType::DEPOSIT)->first();+ $journal = TransactionJournal::where('transaction_type_id', $type->id)->first();+ $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();+ $destination = $destinationTr->account;++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = 'Some new asset ' . rand(1, 1000);+ $action = new SetDestinationAccount($ruleAction);+ $result = $action->act($journal);+ $this->assertFalse($result);++ // test journal for still having old account+ $journal = TransactionJournal::find($journal->id);+ $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();+ $newDestination = $destinationTr->account;+ $this->assertEquals($destination->id, $newDestination->id);+ }++ /**+ * Give withdrawal existing expense account.+ *+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act()+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findExpenseAccount+ */+ public function testActWithdrawalExisting()+ {+ $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();+ $journal = TransactionJournal::where('transaction_type_id', $type->id)->first();+ $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();+ $destination = $destinationTr->account;+ $user = $journal->user;+ $accountType = AccountType::whereType(AccountType::EXPENSE)->first();+ $account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $destination->id)->first();+ $this->assertNotEquals($destination->id, $account->id);++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $account->name;+ $action = new SetDestinationAccount($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // test journal for new account+ $journal = TransactionJournal::find($journal->id);+ $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();+ $newDestination = $destinationTr->account;+ $this->assertNotEquals($destination->id, $newDestination->id);+ $this->assertEquals($newDestination->id, $account->id);+ }++ /**+ * Give withdrawal new expense account.+ *+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act()+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::findExpenseAccount+ */+ public function testActWithdrawalNew()+ {+ $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();+ $journal = TransactionJournal::where('transaction_type_id', $type->id)->first();+ $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();+ $destination = $destinationTr->account;++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = 'Some new expense ' . rand(1, 1000);+ $action = new SetDestinationAccount($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // test journal for new account+ $journal = TransactionJournal::find($journal->id);+ $destinationTr = $journal->transactions()->where('amount', '>', 0)->first();+ $newDestination = $destinationTr->account;+ $this->assertNotEquals($destination->id, $newDestination->id);+ }++ /**+ * Test this on a split journal.+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetDestinationAccount::act()+ */+ public function testSplitJournal()+ {+ $transaction = Transaction::orderBy('count', 'DESC')->groupBy('transaction_journal_id')+ ->get(['transaction_journal_id', DB::raw('COUNT(transaction_journal_id) as count')])+ ->first();+ $journal = TransactionJournal::find($transaction->transaction_journal_id);+ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = 'Some new asset ' . rand(1, 1000);+ $action = new SetDestinationAccount($ruleAction);+ $result = $action->act($journal);+ $this->assertFalse($result);+ }++}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/SetNotesTest.php b/tests/Unit/TransactionRules/Actions/SetNotesTest.phpnew file mode 100644index 0000000000..96e4d8edf5--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/SetNotesTest.php@@ -0,0 +1,81 @@+<?php+/**+ * SetNotesTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++declare(strict_types=1);++namespace Tests\Unit\TransactionRules\Actions;+++use FireflyIII\Models\Note;+use FireflyIII\Models\RuleAction;+use FireflyIII\Models\TransactionJournal;+use FireflyIII\TransactionRules\Actions\ClearNotes;+use FireflyIII\TransactionRules\Actions\SetNotes;+use Tests\TestCase;++/**+ * Class SetNotesTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+class SetNotesTest extends TestCase+{+ /**+ * @covers \FireflyIII\TransactionRules\Actions\SetNotes::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetNotes::act()+ */+ public function testAct()+ {+ // give journal a note:+ $journal = TransactionJournal::find(15);+ $note = $journal->notes()->first();+ if (is_null($note)) {+ $note = new Note;+ $note->noteable()->associate($journal);+ }+ $note->text = 'Hello test note';+ $note->save();+ $this->assertEquals(1, $journal->notes()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = 'These are new notes ' . rand(1, 1234);+ $action = new SetNotes($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // assert result+ $this->assertEquals(1, $journal->notes()->count());+ $this->assertEquals($note->id, $journal->notes()->first()->id);++ }++ /**+ * @covers \FireflyIII\TransactionRules\Actions\SetNotes::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetNotes::act()+ */+ public function testActNoNotes()+ {+ // give journal a note:+ $journal = TransactionJournal::find(16);+ $journal->notes()->forceDelete();+ $this->assertEquals(0, $journal->notes()->count());++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = 'These are new notes ' . rand(1, 1234);+ $action = new SetNotes($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // assert result+ $this->assertEquals(1, $journal->notes()->count());+ }+}\ No newline at end of filediff --git a/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.phpnew file mode 100644index 0000000000..d4ec0912be--- /dev/null+++ b/tests/Unit/TransactionRules/Actions/SetSourceAccountTest.php@@ -0,0 +1,173 @@+<?php+/**+ * SetSourceAccountTest.php+ * Copyright (c) 2017 [email protected]+ * This software may be modified and distributed under the terms of the+ * Creative Commons Attribution-ShareAlike 4.0 International License.+ *+ * See the LICENSE file for details.+ */++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\Models\TransactionType;+use FireflyIII\TransactionRules\Actions\SetSourceAccount;+use Tests\TestCase;++/**+ * Class SetSourceAccountTest+ *+ * @package Tests\Unit\TransactionRules\Actions+ */+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()+ */+ public function testActDepositExistingUpdated()+ {+ $type = TransactionType::whereType(TransactionType::DEPOSIT)->first();+ $journal = TransactionJournal::where('transaction_type_id', $type->id)->first();+ $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();+ $source = $sourceTr->account;+ $user = $journal->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);++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $account->name;+ $action = new SetSourceAccount($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // test journal for new account+ $journal = TransactionJournal::find($journal->id);+ $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();+ $newSource = $sourceTr->account;+ $this->assertNotEquals($source->id, $newSource->id);+ $this->assertEquals($newSource->id, $account->id);+ }++ /**+ * Give deposit new revenue account.+ *+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act()+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findRevenueAccount+ */+ public function testActDepositNewUpdated()+ {+ $type = TransactionType::whereType(TransactionType::DEPOSIT)->first();+ $journal = TransactionJournal::where('transaction_type_id', $type->id)->first();+ $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();+ $source = $sourceTr->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);++ // test journal for new account+ $journal = TransactionJournal::find($journal->id);+ $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();+ $newSource = $sourceTr->account;+ $this->assertNotEquals($source->id, $newSource->id);+ }++ /**+ * Give withdrawal existing asset account.+ *+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act()+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findAssetAccount()+ */+ public function testActWithdrawalExistingUpdated()+ {+ $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();+ $journal = TransactionJournal::where('transaction_type_id', $type->id)->first();+ $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();+ $source = $sourceTr->account;+ $user = $journal->user;+ $accountType = AccountType::whereType(AccountType::ASSET)->first();+ $account = $user->accounts()->where('account_type_id', $accountType->id)->where('id', '!=', $source->id)->first();+ $this->assertNotEquals($source->id, $account->id);++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = $account->name;+ $action = new SetSourceAccount($ruleAction);+ $result = $action->act($journal);+ $this->assertTrue($result);++ // test journal for new account+ $journal = TransactionJournal::find($journal->id);+ $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();+ $newSource = $sourceTr->account;+ $this->assertNotEquals($source->id, $newSource->id);+ $this->assertEquals($newSource->id, $account->id);+ }++ /**+ * Give withdrawal new asset account (will fail)+ *+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act()+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::findAssetAccount()+ */+ public function testActWithdrawalNew()+ {+ $type = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();+ $journal = TransactionJournal::where('transaction_type_id', $type->id)->first();+ $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();+ $source = $sourceTr->account;++ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = 'Some new asset ' . rand(1, 1000);+ $action = new SetSourceAccount($ruleAction);+ $result = $action->act($journal);+ $this->assertFalse($result);++ // test journal for still having old account+ $journal = TransactionJournal::find($journal->id);+ $sourceTr = $journal->transactions()->where('amount', '<', 0)->first();+ $newSource = $sourceTr->account;+ $this->assertEquals($source->id, $newSource->id);+ }++ /**+ * Test this on a split journal.+ *+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::__construct()+ * @covers \FireflyIII\TransactionRules\Actions\SetSourceAccount::act()+ */+ public function testSplitJournal()+ {+ $transaction = Transaction::orderBy('count', 'DESC')->groupBy('transaction_journal_id')+ ->get(['transaction_journal_id', DB::raw('COUNT(transaction_journal_id) as count')])+ ->first();+ $journal = TransactionJournal::find($transaction->transaction_journal_id);+ // fire the action:+ $ruleAction = new RuleAction;+ $ruleAction->action_value = 'Some new asset ' . rand(1, 1000);+ $action = new SetSourceAccount($ruleAction);+ $result = $action->act($journal);+ $this->assertFalse($result);+ }++}\ No newline at end of file