Skip to content

Firefly III: a personal finances manager

Some new tests

James Cole06cc9618babb
diff --git a/app/Helpers/Help/Help.php b/app/Helpers/Help/Help.php
index edb1093e85..19bab089e9 100644
--- a/app/Helpers/Help/Help.php
+++ b/app/Helpers/Help/Help.php
@@ -28,6 +28,7 @@ class Help implements HelpInterface
}
/**
+ * @codeCoverageIgnore
* @param $route
*
* @return array
diff --git a/tests/helpers/ReminderHelperTest.php b/tests/helpers/ReminderHelperTest.php
new file mode 100644
index 0000000000..5775877325
--- /dev/null
+++ b/tests/helpers/ReminderHelperTest.php
@@ -0,0 +1,167 @@
+<?php
+
+use Carbon\Carbon;
+use FireflyIII\Helpers\Reminders\ReminderHelper;
+use FireflyIII\Models\PiggyBankRepetition;
+use FireflyIII\Models\Transaction;
+use League\FactoryMuffin\Facade as FactoryMuffin;
+
+/**
+ * Class ReminderHelperTest
+ */
+class ReminderHelperTest extends TestCase
+{
+
+
+ /**
+ * @var ReminderHelper
+ */
+ protected $object;
+
+ /**
+ * Sets up the fixture, for example, opens a network connection.
+ * This method is called before a test is executed.
+ */
+ public function setUp()
+ {
+ parent::setUp();
+ FactoryMuffin::create('FireflyIII\User');
+ $this->object = new ReminderHelper;
+ }
+
+ /**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ */
+ public function tearDown()
+ {
+ parent::tearDown();
+ }
+
+ public function testCreateReminder()
+ {
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
+ $piggyBank->account_id = $account->id;
+ $start = Carbon::now()->startOfMonth();
+ $end = Carbon::now()->endOfMonth()->startOfDay();
+ $piggyBank->save();
+ $this->be($account->user);
+
+ $result = $this->object->createReminder($piggyBank, $start, $end);
+ $this->assertEquals($piggyBank->targetamount, $result->metadata->leftToSave);
+ }
+
+ public function testCreateReminderHaveAlready()
+ {
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
+ $reminder = FactoryMuffin::create('FireflyIII\Models\Reminder');
+ $piggyBank->account_id = $account->id;
+ $start = Carbon::now()->startOfMonth();
+ $end = Carbon::now()->endOfMonth()->startOfDay();
+ $reminder->remindersable_id = $piggyBank->id;
+ $reminder->startdate = $start;
+ $reminder->enddate = $end;
+ $reminder->user_id = $account->user_id;
+ $reminder->save();
+ $piggyBank->save();
+ $this->be($account->user);
+
+ $result = $this->object->createReminder($piggyBank, $start, $end);
+ $this->assertEquals($reminder->id, $result->id);
+ }
+
+ public function testCreateReminderNoTarget()
+ {
+ $account = FactoryMuffin::create('FireflyIII\Models\Account');
+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
+ $piggyBank->targetdate = null;
+ $piggyBank->account_id = $account->id;
+ $start = Carbon::now()->startOfMonth();
+ $end = Carbon::now()->endOfMonth()->startOfDay();
+ $piggyBank->save();
+ $this->be($account->user);
+
+ $result = $this->object->createReminder($piggyBank, $start, $end);
+ $this->assertEquals(0, $result->metadata->leftToSave);
+ }
+
+ public function testGetReminderRangesNull()
+ {
+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
+ $result = $this->object->getReminderRanges($piggyBank);
+ $this->assertEquals([], $result);
+ }
+
+ public function testGetReminderRangesWithTargetDate()
+ {
+ /** @var \FireflyIII\Models\PiggyBank $piggyBank */
+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
+ $piggyBank->startdate = new Carbon('2015-01-01');
+ $piggyBank->targetdate = new Carbon('2015-12-31');
+ $piggyBank->reminder = 'monthly';
+ $piggyBank->remind_me = true;
+ $piggyBank->save();
+
+ $result = $this->object->getReminderRanges($piggyBank, new Carbon('2015-04-01'));
+ // date is ignored, result should be 12:
+ $this->assertCount(12, $result);
+
+ }
+
+ public function testGetReminderRangesWithoutTargetDate()
+ {
+ /** @var \FireflyIII\Models\PiggyBank $piggyBank */
+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
+ $piggyBank->startdate = new Carbon('2015-01-01');
+ $piggyBank->targetdate = null;
+ $piggyBank->reminder = 'monthly';
+ $piggyBank->remind_me = true;
+ $piggyBank->save();
+ $result = $this->object->getReminderRanges($piggyBank, new Carbon('2015-12-31'));
+ // date is a year later, result should be 12:
+ $this->assertCount(12, $result);
+
+ }
+
+ public function testGetReminderTextDate()
+ {
+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
+ $reminder = FactoryMuffin::create('FireflyIII\Models\Reminder');
+ $piggyBank->targetdate = new Carbon;
+ $reminder->remindersable_id = $piggyBank->id;
+
+ Amount::shouldReceive('format')->andReturn('xx');
+
+ $result = $this->object->getReminderText($reminder);
+ $strpos = strpos($result, 'to fill this piggy bank on ');
+ $this->assertTrue(!($strpos === false));
+
+ }
+
+ public function testGetReminderTextNoPiggy()
+ {
+ $reminder = FactoryMuffin::create('FireflyIII\Models\Reminder');
+ $reminder->remindersable_id = 2;
+ $this->assertEquals('Piggy bank no longer exists.', $this->object->getReminderText($reminder));
+
+ }
+
+ public function testGetReminderTextNullDate()
+ {
+ $piggyBank = FactoryMuffin::create('FireflyIII\Models\PiggyBank');
+ $reminder = FactoryMuffin::create('FireflyIII\Models\Reminder');
+ $piggyBank->targetdate = null;
+ $piggyBank->save();
+ $reminder->remindersable_id = $piggyBank->id;
+ $reminder->save();
+
+ Amount::shouldReceive('format')->andReturn('xx');
+
+ $result = $this->object->getReminderText($reminder);
+ $strpos = strpos($result, 'Add money to this piggy bank to reach your target of');
+ $this->assertTrue(!($strpos === false));
+
+ }
+}
\ No newline at end of file