Update tests

This commit is contained in:
James Cole 2017-10-03 10:41:48 +02:00
parent dd9528f531
commit c000fe4557
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
7 changed files with 133 additions and 12 deletions

View File

@ -61,6 +61,6 @@ class AddTag implements ActionInterface
Log::debug(sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal->id));
return true;
return false;
}
}

View File

@ -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;
@ -44,10 +45,16 @@ class AppendNotes implements ActionInterface
*/
public function act(TransactionJournal $journal): bool
{
$notes = $journal->getMeta('notes');
$dbNote = $journal->notes()->first();
if (is_null($dbNote)) {
$dbNote = new Note;
$dbNote->noteable()->associate($journal);
}
$notes = $dbNote->text;
Log::debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $this->action->action_value, $notes));
$notes = $notes . $this->action->action_value;
$journal->setMeta('notes', $notes);
$notes = $notes . $this->action->action_value;
$dbNote->text = $notes;
$dbNote->save();
$journal->save();
return true;

View File

@ -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;
@ -44,10 +45,16 @@ class PrependNotes implements ActionInterface
*/
public function act(TransactionJournal $journal): bool
{
$notes = $journal->getMeta('notes');
$dbNote = $journal->notes()->first();
if (is_null($dbNote)) {
$dbNote = new Note;
$dbNote->noteable()->associate($journal);
}
$notes = $dbNote->text;
Log::debug(sprintf('RuleAction PrependNotes prepended "%s" with "%s".', $notes, $this->action->action_value));
$notes = $this->action->action_value . $notes;
$journal->setMeta('notes', $notes);
$notes = $this->action->action_value . $notes;
$dbNote->text = $notes;
$dbNote->save();
$journal->save();
return true;

View File

@ -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;
@ -44,9 +45,14 @@ class SetNotes implements ActionInterface
*/
public function act(TransactionJournal $journal): bool
{
$oldNotes = $journal->getMeta('notes');
$journal->setMeta('notes', $this->action->action_value);
$dbNote = $journal->notes()->first();
if (is_null($dbNote)) {
$dbNote = new Note;
$dbNote->noteable()->associate($journal);
}
$oldNotes = $dbNote->text;
$dbNote->text = $this->action->action_value;
$dbNote->save();
$journal->save();
Log::debug(sprintf('RuleAction SetNotes changed the notes of journal #%d from "%s" to "%s".', $journal->id, $oldNotes, $this->action->action_value));

View File

@ -107,6 +107,7 @@ class TagControllerTest extends TestCase
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
$repository->shouldReceive('count')->andReturn(0);
$repository->shouldReceive('tagCloud')->andReturn([]);
$repository->shouldReceive('oldestTag')->andReturn(null)->once();
$this->be($this->user());
$response = $this->get(route('tags.index'));
@ -137,9 +138,7 @@ class TagControllerTest extends TestCase
$repository->shouldReceive('earnedInPeriod')->andReturn('1')->once();
$repository->shouldReceive('sumsOfTag')->andReturn($amounts)->once();
$collector->shouldReceive('removeFilter')->andReturnSelf()->once();
$collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once();
$collector->shouldReceive('setLimit')->andReturnSelf()->once();
$collector->shouldReceive('setPage')->andReturnSelf()->once();

View File

@ -0,0 +1,59 @@
<?php
/**
* AddTagTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* 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\Tag;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\TransactionRules\Actions\AddTag;
use Tests\TestCase;
class AddTagTest extends TestCase
{
/**
* @covers \FireflyIII\TransactionRules\Actions\AddTag::__construct
* @covers \FireflyIII\TransactionRules\Actions\AddTag::act()
*/
public function testActExistingTag()
{
$this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => 2, 'transaction_journal_id' => 1]);
$ruleAction = new RuleAction;
$ruleAction->action_value = 'housing';
$journal = TransactionJournal::find(1);
$action = new AddTag($ruleAction);
$result = $action->act($journal);
$this->assertFalse($result);
$this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => 2, 'transaction_journal_id' => 1]);
}
/**
* @covers \FireflyIII\TransactionRules\Actions\AddTag::act()
*/
public function testActNoTag()
{
$ruleAction = new RuleAction;
$ruleAction->action_value = 'TestTag-' . rand(1, 1000);
$journal = TransactionJournal::find(1);
$action = new AddTag($ruleAction);
$result = $action->act($journal);
$this->assertTrue($result);
// find newly created tag:
$tag = Tag::orderBy('id', 'DESC')->first();
$this->assertDatabaseHas('tag_transaction_journal', ['tag_id' => $tag->id, 'transaction_journal_id' => 1]);
}
}

View File

@ -0,0 +1,43 @@
<?php
/**
* AppendDescriptionTest.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* 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\AppendDescription;
use Tests\TestCase;
class AppendDescriptionTest extends TestCase
{
/**
* @covers \FireflyIII\TransactionRules\Actions\AppendDescription::__construct
* @covers \FireflyIII\TransactionRules\Actions\AppendDescription::act()
*/
public function testActExistingTag()
{
$ruleAction = new RuleAction;
$ruleAction->action_value = 'APPEND';
$journal = TransactionJournal::find(1);
$oldDescription = $journal->description;
$action = new AppendDescription($ruleAction);
$result = $action->act($journal);
$this->assertTrue($result);
$journal = TransactionJournal::find(1);
$this->assertEquals($oldDescription . 'APPEND', $journal->description);
}
}