New tests.

This commit is contained in:
James Cole
2017-10-06 15:41:21 +02:00
parent 29e02ce31f
commit 67ed6e14aa
54 changed files with 3201 additions and 54 deletions

View File

@@ -20,9 +20,10 @@ use FireflyIII\TransactionRules\Actions\ActionInterface;
use Log;
/**
* Interface ActionInterface
* @codeCoverageIgnore
* Class ActionFactory
*
* @package FireflyIII\TransactionRules\Actions
* @package FireflyIII\TransactionRules\Factory
*/
class ActionFactory
{

View File

@@ -21,6 +21,7 @@ use FireflyIII\TransactionRules\Triggers\TriggerInterface;
use Log;
/**
* @codeCoverageIgnore
* Interface TriggerInterface
*
* @package FireflyIII\TransactionRules\Triggers

View File

@@ -38,6 +38,7 @@ class AbstractTrigger
/**
* AbstractTrigger constructor.
* @codeCoverageIgnore
*/
private function __construct()
{
@@ -45,6 +46,7 @@ class AbstractTrigger
}
/**
* @codeCoverageIgnore
* @param string $triggerValue
* @param bool $stopProcessing
*
@@ -60,6 +62,7 @@ class AbstractTrigger
}
/**
* @codeCoverageIgnore
* @param RuleTrigger $trigger
*
* @return AbstractTrigger
@@ -74,21 +77,8 @@ class AbstractTrigger
return $self;
}
/**
* @param RuleTrigger $trigger
* @param TransactionJournal $journal
*/
public static function makeFromTriggerAndJournal(RuleTrigger $trigger, TransactionJournal $journal)
{
$self = new static;
$self->trigger = $trigger;
$self->triggerValue = $trigger->trigger_value;
$self->stopProcessing = $trigger->stop_processing;
$self->journal = $journal;
}
/**
* @codeCoverageIgnore
* @param string $triggerValue
*
* @return AbstractTrigger
@@ -102,6 +92,7 @@ class AbstractTrigger
}
/**
* @codeCoverageIgnore
* @return RuleTrigger
*/
public function getTrigger(): RuleTrigger
@@ -110,6 +101,7 @@ class AbstractTrigger
}
/**
* @codeCoverageIgnore
* @return string
*/
public function getTriggerValue(): string

View File

@@ -66,10 +66,8 @@ final class DescriptionContains extends AbstractTrigger implements TriggerInterf
{
$search = strtolower($this->triggerValue);
$source = strtolower($journal->description ?? '');
$strpos = strpos($source, $search);
$strpos = stripos($source, $search);
if (!($strpos === false)) {
Log::debug(sprintf('RuleTrigger DescriptionContains for journal #%d: "%s" contains "%s", return true.', $journal->id, $source, $search));
return true;

View File

@@ -69,10 +69,11 @@ final class DescriptionEnds extends AbstractTrigger implements TriggerInterface
$searchLength = strlen($search);
// if the string to search for is longer than the description,
// shorten the search string.
// return false.
if ($searchLength > $descriptionLength) {
$search = substr($search, ($descriptionLength * -1));
$searchLength = strlen($search);
Log::debug(sprintf('RuleTrigger DescriptionEnds for journal #%d: "%s" does not end with "%s", return false.', $journal->id, $description, $search));
return false;
}
$part = substr($description, $searchLength * -1);

View File

@@ -44,7 +44,12 @@ final class FromAccountIs extends AbstractTrigger implements TriggerInterface
public static function willMatchEverything($value = null)
{
if (!is_null($value)) {
return false;
$res = strval($value) === '';
if ($res === true) {
Log::error(sprintf('Cannot use %s with "" as a value.', self::class));
}
return $res;
}
Log::error(sprintf('Cannot use %s with a null value.', self::class));

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -57,6 +58,20 @@ final class HasAnyBudget extends AbstractTrigger implements TriggerInterface
return true;
}
// perhaps transactions have budget?
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
$count = $transaction->budgets()->count();
if ($count > 0) {
Log::debug(
sprintf('RuleTrigger HasAnyBudget for journal #%d (transaction #%d): count is %d, return true.', $journal->id, $transaction->id, $count)
);
return true;
}
}
Log::debug(sprintf('RuleTrigger HasAnyBudget for journal #%d: count is %d, return false.', $journal->id, $count));
return false;

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -57,6 +58,20 @@ final class HasAnyCategory extends AbstractTrigger implements TriggerInterface
return true;
}
// perhaps transactions have category?
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
$count = $transaction->categories()->count();
if ($count > 0) {
Log::debug(
sprintf('RuleTrigger HasAnyCategory for journal #%d (transaction #%d): count is %d, return true.', $journal->id, $transaction->id, $count)
);
return true;
}
}
Log::debug(sprintf('RuleTrigger HasAnyCategory for journal #%d: count is %d, return false.', $journal->id, $count));
return false;

View File

@@ -13,6 +13,7 @@ namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\TransactionJournal;
use Log;
class HasAttachment extends AbstractTrigger implements TriggerInterface
{
@@ -50,11 +51,18 @@ class HasAttachment extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal): bool
{
$minimum = intval($this->triggerValue);
$minimum = intval($this->triggerValue);
$attachments = $journal->attachments()->count();
if ($attachments >= $minimum) {
Log::debug(
sprintf(
'RuleTrigger HasAttachment for journal #%d: count is %d, is more than or equal to %d, return true.', $journal->id, $attachments, $minimum
)
);
return true;
}
Log::debug(sprintf('RuleTrigger HasAttachment for journal #%d: count is %d, is less than %d, return true.', $journal->id, $attachments, $minimum));
return false;
}

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -52,11 +53,18 @@ final class HasNoBudget extends AbstractTrigger implements TriggerInterface
public function triggered(TransactionJournal $journal): bool
{
$count = $journal->budgets()->count();
// perhaps transactions have budget?
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
$count += $transaction->budgets()->count();
}
if ($count === 0) {
Log::debug(sprintf('RuleTrigger HasNoBudget for journal #%d: count is %d, return true.', $journal->id, $count));
return true;
}
Log::debug(sprintf('RuleTrigger HasNoBudget for journal #%d: count is %d, return false.', $journal->id, $count));
return false;

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -52,6 +53,13 @@ final class HasNoCategory extends AbstractTrigger implements TriggerInterface
public function triggered(TransactionJournal $journal): bool
{
$count = $journal->categories()->count();
// perhaps transactions have category?
/** @var Transaction $transaction */
foreach ($journal->transactions()->get() as $transaction) {
$count += $transaction->categories()->count();
}
if ($count === 0) {
Log::debug(sprintf('RuleTrigger HasNoCategory for journal #%d: count is %d, return true.', $journal->id, $count));

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -51,9 +52,14 @@ final class NotesAny extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal): bool
{
$notes = $journal->getMeta('notes') ?? '';
/** @var Note $note */
$note = $journal->notes()->first();
$text = '';
if (!is_null($note)) {
$text = $note->text;
}
if (strlen($notes) > 0) {
if (strlen($text) > 0) {
Log::debug(sprintf('RuleTrigger NotesEmpty for journal #%d: strlen > 0, return true.', $journal->id));

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -57,17 +58,22 @@ final class NotesAre extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal): bool
{
$notes = strtolower($journal->getMeta('notes') ?? '');
/** @var Note $note */
$note = $journal->notes()->first();
$text = '';
if (!is_null($note)) {
$text = strtolower($note->text);
}
$search = strtolower($this->triggerValue);
if ($notes === $search) {
if ($text === $search && strlen($text) > 0) {
Log::debug(sprintf('RuleTrigger NotesAre for journal #%d: "%s" is "%s", return true.', $journal->id, $notes, $search));
Log::debug(sprintf('RuleTrigger NotesAre for journal #%d: "%s" is "%s", return true.', $journal->id, $text, $search));
return true;
}
Log::debug(sprintf('RuleTrigger NotesAre for journal #%d: "%s" is NOT "%s", return false.', $journal->id, $notes, $search));
Log::debug(sprintf('RuleTrigger NotesAre for journal #%d: "%s" is NOT "%s", return false.', $journal->id, $text, $search));
return false;
}

View File

@@ -13,6 +13,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -63,18 +64,31 @@ final class NotesContain extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal): bool
{
$search = strtolower($this->triggerValue);
$notes = strtolower($journal->getMeta('notes') ?? '');
$search = trim(strtolower($this->triggerValue));
$strpos = strpos($notes, $search);
if (strlen($search) === 0) {
Log::debug(sprintf('RuleTrigger NotesContain for journal #%d: "%s" is empty, return false.', $journal->id, $search));
return false;
}
/** @var Note $note */
$note = $journal->notes()->first();
$text = '';
if (!is_null($note)) {
$text = strtolower($note->text);
}
$strpos = strpos($text, $search);
if (!($strpos === false)) {
Log::debug(sprintf('RuleTrigger NotesContain for journal #%d: "%s" contains "%s", return true.', $journal->id, $notes, $search));
Log::debug(sprintf('RuleTrigger NotesContain for journal #%d: "%s" contains "%s", return true.', $journal->id, $text, $search));
return true;
}
Log::debug(sprintf('RuleTrigger NotesContain for journal #%d: "%s" does NOT contain "%s", return false.', $journal->id, $notes, $search));
Log::debug(sprintf('RuleTrigger NotesContain for journal #%d: "%s" does NOT contain "%s", return false.', $journal->id, $text, $search));
return false;

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -51,9 +52,14 @@ final class NotesEmpty extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal): bool
{
$notes = $journal->getMeta('notes') ?? '';
/** @var Note $note */
$note = $journal->notes()->first();
$text = '';
if (!is_null($note)) {
$text = $note->text;
}
if (strlen($notes) === 0) {
if (strlen($text) === 0) {
Log::debug(sprintf('RuleTrigger NotesEmpty for journal #%d: strlen is 0, return true.', $journal->id));

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -62,28 +63,34 @@ final class NotesEnd extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal): bool
{
$notes = strtolower($journal->getMeta('notes') ?? '');
$notesLength = strlen($notes);
/** @var Note $note */
$note = $journal->notes()->first();
$text = '';
if (!is_null($note)) {
$text = strtolower($note->text);
}
$notesLength = strlen($text);
$search = strtolower($this->triggerValue);
$searchLength = strlen($search);
// if the string to search for is longer than the description,
// shorten the search string.
// return false
if ($searchLength > $notesLength) {
$search = substr($search, ($notesLength * -1));
$searchLength = strlen($search);
Log::debug(sprintf('RuleTrigger NotesEnd for journal #%d: "%s" does not end with "%s", return false.', $journal->id, $text, $search));
return false;
}
$part = substr($notes, $searchLength * -1);
$part = substr($text, $searchLength * -1);
if ($part === $search) {
Log::debug(sprintf('RuleTrigger NotesEnd for journal #%d: "%s" ends with "%s", return true.', $journal->id, $notes, $search));
Log::debug(sprintf('RuleTrigger NotesEnd for journal #%d: "%s" ends with "%s", return true.', $journal->id, $text, $search));
return true;
}
Log::debug(sprintf('RuleTrigger NotesEnd for journal #%d: "%s" does not end with "%s", return false.', $journal->id, $notes, $search));
Log::debug(sprintf('RuleTrigger NotesEnd for journal #%d: "%s" does not end with "%s", return false.', $journal->id, $text, $search));
return false;
}

View File

@@ -13,6 +13,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Triggers;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use Log;
@@ -63,18 +64,23 @@ final class NotesStart extends AbstractTrigger implements TriggerInterface
*/
public function triggered(TransactionJournal $journal): bool
{
$notes = strtolower($journal->getMeta('notes') ?? '');
/** @var Note $note */
$note = $journal->notes()->first();
$text = '';
if (!is_null($note)) {
$text = strtolower($note->text);
}
$search = strtolower($this->triggerValue);
$part = substr($notes, 0, strlen($search));
$part = substr($text, 0, strlen($search));
if ($part === $search) {
Log::debug(sprintf('RuleTrigger NotesStart for journal #%d: "%s" starts with "%s", return true.', $journal->id, $notes, $search));
Log::debug(sprintf('RuleTrigger NotesStart for journal #%d: "%s" starts with "%s", return true.', $journal->id, $text, $search));
return true;
}
Log::debug(sprintf('RuleTrigger NotesStart for journal #%d: "%s" does not start with "%s", return false.', $journal->id, $notes, $search));
Log::debug(sprintf('RuleTrigger NotesStart for journal #%d: "%s" does not start with "%s", return false.', $journal->id, $text, $search));
return false;

View File

@@ -75,10 +75,11 @@ final class ToAccountEnds extends AbstractTrigger implements TriggerInterface
$searchLength = strlen($search);
// if the string to search for is longer than the account name,
// shorten the search string.
// return false
if ($searchLength > $toAccountNameLength) {
$search = substr($search, ($toAccountNameLength * -1));
$searchLength = strlen($search);
Log::debug(sprintf('RuleTrigger ToAccountEnds for journal #%d: "%s" does not end with "%s", return false.', $journal->id, $toAccountName, $search));
return false;
}

View File

@@ -37,6 +37,7 @@ final class UserAction extends AbstractTrigger implements TriggerInterface
* false.
*
* @param null $value
* @codeCoverageIgnore
*
* @return bool
*/
@@ -49,6 +50,7 @@ final class UserAction extends AbstractTrigger implements TriggerInterface
* This trigger is always triggered, because the rule that it is a part of has been pre-selected on this condition.
*
* @param TransactionJournal $journal
* @codeCoverageIgnore
*
* @return bool
*/